all groups > sql server programming > december 2006 >
You're in the

sql server programming

group:

date difference in day,hours and minutes



date difference in day,hours and minutes vanitha
12/14/2006 10:06:01 PM
sql server programming: hi,

i want to find the date difference in days,hours and minutes format.

please help me to solve this.

thank you
RE: date difference in day,hours and minutes Krishnakumar S
12/14/2006 10:14:00 PM
Refer DATEDIFF in BOL. Or If you need a clear solution please post the DDL
and sampe data

--
Krishnakumar S


[quoted text, click to view]
RE: date difference in day,hours and minutes vanitha
12/14/2006 10:39:00 PM
date difference between 2006-12-14 18:27:43.173 and getdate() in days, hours
and minutes

thank you

[quoted text, click to view]
RE: date difference in day,hours and minutes Krishnakumar S
12/14/2006 10:56:00 PM
There is a good article in www.aspfaq.com. Go to
http://classicasp.aspfaq.com/date-time-routines-manipulation/how-do-i-convert-a-datediff-to-days-hours-and-minutes.html
--
Krishnakumar S


[quoted text, click to view]
Re: date difference in day,hours and minutes Gert-Jan Strik
12/15/2006 12:00:00 AM
Nice solution. However, it returns NULL in certain situations (less than
a day for example). Besides, you don't need the CASE expressions:

SELECT
CAST(dt.Days AS VARCHAR(5)) + ' Day(s) ' +
CAST(dt.Hours AS VARCHAR(2)) + ' Hour(s) ' +
CAST(dt.Minutes AS VARCHAR(2)) + ' Minute(s)'
FROM
(
SELECT t.Minutes / (24 * 60) AS Days,
(t.Minutes % (24 * 60)) / 60 AS Hours,
(t.Minutes % (24 * 60)) % 60 AS Minutes
FROM
(
SELECT ABS(DATEDIFF(MINUTE, '2006-12-12 18:27:43.173',
GETDATE())) AS Minutes
) AS t
) AS dt

Gert-Jan



[quoted text, click to view]
RE: date difference in day,hours and minutes vanitha
12/15/2006 12:36:00 AM
i should achive this without writing function that isdirectly in the query

[quoted text, click to view]
RE: date difference in day,hours and minutes Krishnakumar S
12/15/2006 1:03:02 AM
Try this

SELECT
CAST(dt.Days AS VARCHAR(5)) + ' Day(s) ' +
CAST(dt.Hours AS VARCHAR(2)) + ' Hour(s) ' +
CAST(dt.Minutes AS VARCHAR(2)) + ' Minute(s)'
FROM
(
SELECT
CASE
WHEN t.Minutes = 0 THEN 0
WHEN t.Minutes >= (24 * 60) THEN (t.Minutes / (24 * 60))
END AS Days,
CASE
WHEN t.Minutes = 0 THEN 0
WHEN t.Minutes % (24 * 60) >= 60 THEN ((t.Minutes % (24 * 60)) / 60)
END AS Hours,
(t.Minutes % (24 * 60)) % 60 AS Minutes
FROM
(
SELECT ABS(DATEDIFF(MINUTE, '2006-12-12 18:27:43.173', GETDATE())) AS
Minutes
) AS t
) AS dt

--
Krishnakumar S


[quoted text, click to view]
AddThis Social Bookmark Button