Click here to Skip to main content
15,924,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m using a stored procedure and there i m having a column Name timespent which i want to show like HH:MM format only, the procedure is working fine but the only problem is that currently the time spent is coming as hh:mm:ss format. Guide me how i ll get time spent in HH:MM format and correct my procedure if possible. The procedure is like that:
SQL
CREATE procedure St_Proc_GetUserReportforCurrentDayTask              
@userID int              
as              
Begin              
    set NoCount on;              
    DECLARE @TODAY DATE                
    SET @TODAY = CONVERT(VARCHAR(10), GETDATE(), 111)              
    select CONVERT(VARCHAR,production.CalendarDate,101) + RIGHT (CONVERT(VARCHAR,production.CalendarDate , 100 ) ,7) as Date,               
    RegionAndProjectInfo.RegionProjectName as Region ,              
    County.CountyName as County,              
    WorkType.WorkTypeName as WorkType,              
    Task.TaskName as Task,  
    Production.VolumeProcessed as 'Volumes Processed',              
    Production.TimeSpent as 'Duration (HH:MM)'              
    from Production               
    inner join RegionAndProjectInfo              
    on              
    RegionAndProjectInfo.RegionProjectID=Production.RegionProjectID              
    inner join County              
    on               
    County.CountyID=Production.CountyID              
    inner join WorkType              
    on              
    WorkType.WorkTypeID=Production.WorkTypeID              
    inner join Task              
    on              
    Task.TaskID=Production.TaskID              
    where Production.UserID=@userID and CalendarDate >= @TODAY              
End 
Posted

Use the below code:
SQL
CREATE procedure St_Proc_GetUserReportforCurrentDayTask              
@userID int              
as              
Begin              
    set NoCount on;              
    DECLARE @TODAY DATE                
    SET @TODAY = CONVERT(VARCHAR(10), GETDATE(), 111)              
    select CONVERT(VARCHAR,production.CalendarDate,101) + RIGHT (CONVERT(VARCHAR,production.CalendarDate , 100 ) ,7) as Date,               
    RegionAndProjectInfo.RegionProjectName as Region ,              
    County.CountyName as County,              
    WorkType.WorkTypeName as WorkType,              
    Task.TaskName as Task,  
    Production.VolumeProcessed as 'Volumes Processed',  
convert(varchar(5),Production.TimeSpent ,108) as 'Duration (HH:MM)'   
    from Production               
    inner join RegionAndProjectInfo              
    on              
    RegionAndProjectInfo.RegionProjectID=Production.RegionProjectID              
    inner join County              
    on               
    County.CountyID=Production.CountyID              
    inner join WorkType              
    on              
    WorkType.WorkTypeID=Production.WorkTypeID              
    inner join Task              
    on              
    Task.TaskID=Production.TaskID              
    where Production.UserID=@userID and CalendarDate >= @TODAY              
End
 
Share this answer
 
v2
look this example i have tested it is working

SQL
DECLARE @TODAY DATEtime
    SET @TODAY = CONVERT(VARCHAR(10), GETDATE(), 108)


select CAST
(
(SUM (datepart(hh, convert (varchar, @TODAY, 108))) +
(sum(datepart(mi, convert (varchar, @TODAY, 108)))/60) ) AS VARCHAR(2)
)
+ ':' +
CAST
(
sum(datepart(mi, convert (varchar, @TODAY, 108))) - 60 * (sum(datepart(mi, convert (varchar, @TODAY, 108)))/60)
 as VARCHAR(2))
 
Share this answer
 
You can try this :

select convert(varchar(5),GETDATE(),108) as 'HH:MM'

Thanks - Girijesh Kumar
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900