January 23, 2010 at 9:01 am
Hi,
how can i extract the days,hour,min,sec from a datetime value when it compare to current date like this example?(final results like that
27 Days, 7 Hours, 19 Minutes, 7 Seconds)
THX
DECLARE @SQL_START_TIME DATETIME
DECLARE @CURRENT_DATETIME DATETIME
SET @CURRENT_DATETIME = GETDATE()
SELECT @SQL_START_TIME = create_date FROM master.sys.databases where name = 'tempdb'
SELECT datediff(dd,@SQL_START_TIME,@CURRENT_DATETIME)
January 23, 2010 at 10:16 am
Look into the DATEPART function. That may be what you need.
January 23, 2010 at 10:32 am
Mad-Dog (1/23/2010)
Hi,how can i extract the days,hour,min,sec from a datetime value when it compare to current date like this example?(final results like that
27 Days, 7 Hours, 19 Minutes, 7 Seconds)
THX
DECLARE @SQL_START_TIME DATETIME
DECLARE @CURRENT_DATETIME DATETIME
SET @CURRENT_DATETIME = GETDATE()
SELECT @SQL_START_TIME = create_date FROM master.sys.databases where name = 'tempdb'
SELECT datediff(dd,@SQL_START_TIME,@CURRENT_DATETIME)
Homebrew has the right idea but let me ask... why do you need to do this? What is the business reason for this?
--Jeff Moden
Change is inevitable... Change for the better is not.
January 23, 2010 at 1:50 pm
I'm working on a report info.
can you give me an example of ths datepart?
THX
January 23, 2010 at 2:52 pm
select datepart(hour, getdate()) -- Get just the hour ... in "military" hours
January 23, 2010 at 3:31 pm
Thanks but i found another way
DECLARE @TEMP_TABLE_HOLD_TOTAL_SECOND TABLE (TOTAL_SECOND BIGINT)
DECLARE @SQL_START_TIME DATETIME
DECLARE @CURRENT_DATETIME DATETIME
SET @CURRENT_DATETIME = GETDATE()
SELECT @SQL_START_TIME = create_date FROM master.sys.databases where name = 'tempdb'
INSERT INTO @TEMP_TABLE_HOLD_TOTAL_SECOND
SELECT DATEDIFF (ss,@SQL_START_TIME,@CURRENT_DATETIME)AS 'TOTAL_SECOND'
select cast((TOTAL_SECOND / 86400)as nvarchar (5)) + ' Days, '
+ cast((TOTAL_SECOND % 86400) / 3600 as nvarchar (5)) + ' Hours, '
+ cast((TOTAL_SECOND % 3600) / 60 as nvarchar (5)) + ' Minutes, '
+ cast(TOTAL_SECOND % 60 as nvarchar (5)) + ' Seconds'
from @TEMP_TABLE_HOLD_TOTAL_SECOND
January 23, 2010 at 10:14 pm
I'd say you've made it hard on yourself...
SELECT STR(DATEDIFF(dd,0,GETDATE()-@Sql_Start_Time),5) + ' Days, '
+ DATENAME(hh,GETDATE()-@Sql_Start_Time) + ' Hours, '
+ DATENAME(mi,GETDATE()-@Sql_Start_Time) + ' Minutes, '
+ DATENAME(ss,GETDATE()-@Sql_Start_Time) + ' Seconds '
--Jeff Moden
Change is inevitable... Change for the better is not.
January 25, 2010 at 2:35 am
Even simpler:
cast(datediff(dd,0,getdate()) as datetime)
lx
January 25, 2010 at 5:32 am
lisbon (1/25/2010)
Even simpler:cast(datediff(dd,0,getdate()) as datetime)
lx
Not exactly what the op asked for but I agree.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 25, 2010 at 9:26 am
Mad-Dog (1/23/2010)
I'm working on a report info.
An SQL Reporting Services report, a Crystal Report, or something else...?
It usually better to do this sort of thing in the reporting tool itself.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply