December 29, 2011 at 9:53 pm
hi,
@time ='20:10:10'
how can i get total second from the time value is there any function in sql 2005 or 2008...
Thanks,
Giri.
December 30, 2011 at 12:39 am
DECLARE @time TIME = '20:10:10'
SELECT total_seconds =
DATEPART(SECOND, @time) +
60 * DATEPART(MINUTE, @time) +
3600 * DATEPART(HOUR, @time)
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
December 30, 2011 at 3:30 am
DECLARE @time TIME = '20:10:10'
select total_seconds =DATEDIFF(second,0,cast(@time as datetime))
December 30, 2011 at 8:11 pm
VIG (12/30/2011)
DECLARE @time TIME = '20:10:10'
select total_seconds =DATEDIFF(second,0,cast(@time as datetime))
😉
--Jeff Moden
Change is inevitable... Change for the better is not.
December 30, 2011 at 10:47 pm
VIG (12/30/2011)
DECLARE @time TIME = '20:10:10'
select total_seconds =DATEDIFF(second,0,cast(@time as datetime))
The time string can be directly assigned to a datetime variable as 1900-01-01 20:10:10:
DECLARE @time datetime = '20:10:10'
select total_seconds =DATEDIFF(second,0,@time)
And the cast of the string to a date can be implicit in the DATEDIFF function:
select total_seconds =DATEDIFF(second,0,'20:10:10')
December 30, 2011 at 10:52 pm
All very good. And yes, a single DATEDIFF is 'better' than three DATEPARTs.
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
January 2, 2012 at 5:37 am
SELECT DATEDIFF(SECOND,0,'20:10:10')
Thanks & Regards
Syed Sami Ur Rehman
SQL-Server (Developer)
Hyderabad
Email-sami.sqldba@gmail.com
January 3, 2012 at 2:10 pm
Keep in mind that the TIME datatype is not available in SQL Server 2005, only 2008 and newer. I realize that this is the 2008 forum, but you did ask for a function in 2005 or 2008.
For best compatibility, I would recommend the following:
DECLARE @baseline datetime, @testvalue datetime
SET @baseline = CONVERT(datetime, '1900-01-01T00:00:00', 126)
SET @testvalue= CONVERT(datetime, '2012-01-03T12:30:45', 126)
SELECT CAST(DATEDIFF(Minute, @baseline, @testvalue) AS bigint) * 60 + DATEPART(second, @testvalue)
--Or, depending on the baseline you want, you can simply do
SELECT DATEDIFF(second, @baseline, @testvalue)
--Caution, Too great a difference between your baseline and your value could cause an integer overflow
Rather than implicitly convert the baseline value to a datetime, I explicitly converted it with a specified style format. The reason for doing this is that any view/computed column/TVF will be non-deterministic if it includes an implicit datetime conversion like that. Different SQL servers have different default date formats. As a result, SQL cannot guarantee that the implicit conversion will always end up with the same result. Converting the string to a datetime with a specific file format takes care of that problem.
The biggest disadvantage of non-deterministic methods is that they limit what can be indexed. You may not need to index these particular results, but if you want to reuse this code, you may not want to limit your options.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply