Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Converting dates to number of days

Author  Topic 

Pasi
Posting Yak Master

166 Posts

Posted - 2014-03-06 : 13:28:36
HI ,

I need some help to convert the difference in 2 dates into number of days?

example:
1- from date 2/22/14
2- to date 3/6/14

need to take the difference of these 2 and convert it to number of days.
Thanks,
Pasi

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-03-06 : 14:01:27
Have you looked at the DATEDIFF function?

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Pasi
Posting Yak Master

166 Posts

Posted - 2014-03-06 : 14:37:35
I have but these date field "admit date" and "release dates" are 2 pick list from an application. once picked I need to convert them to a number of days, and I am not sure how to do that>
the DATEDIFF function is looking for some thing like SELECT DATEDIFF('2008-11-30','2008-11-29') AS DiffDate

but my 2 date filed dates are called relase date--> "to_routine" and admit date --->"from routine" how do I do it with these fields name?

Thanks.
Go to Top of Page

maunishq
Yak Posting Veteran

71 Posts

Posted - 2014-03-06 : 14:48:47
SELECT DATEDIFF(dd,from_routine,to_routine) AS diff

=======================
Not an Expert, Just a learner.
!_(M)_!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-03-06 : 14:48:57
SELECT DATEDIFF(dd,release_date,admit_date)
FROM yourtable
WHERE ...

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Pasi
Posting Yak Master

166 Posts

Posted - 2014-03-06 : 16:18:49
Thaks s TKizer. can you alos tell me how to convert a date string like 20140104 to 01/04/2014?
Thanks.
Go to Top of Page

Pasi
Posting Yak Master

166 Posts

Posted - 2014-03-06 : 16:19:35
Thanks!!!
quote:
Originally posted by maunishq

SELECT DATEDIFF(dd,from_routine,to_routine) AS diff

=======================
Not an Expert, Just a learner.
!_(M)_!

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-03-06 : 16:21:16
No need to convert it. SQL Server can handle that format for the DATEDIFF function. If you are asking for presentation purposes, then I'd suggest not doing this conversion in T-SQL. Presentation layer issues should be handled by the application and not by SQL.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Pasi
Posting Yak Master

166 Posts

Posted - 2014-03-06 : 16:24:09
Well, I am using it in crystal report which uses the SQL db. when I bring this date it does not convert it. I did it once before but cant figure it out for some reason! too much in mind! :)

quote:
Originally posted by tkizer

No need to convert it. SQL Server can handle that format for the DATEDIFF function. If you are asking for presentation purposes, then I'd suggest not doing this conversion in T-SQL. Presentation layer issues should be handled by the application and not by SQL.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-03-06 : 16:31:54
Crystal Reports should be the one to do the conversion then, not SQL. I'm not a Crystal Reports designer though, have never used it, but I know it can convert dates.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Pasi
Posting Yak Master

166 Posts

Posted - 2014-03-06 : 16:55:10
Yah I got to figure it out. thnx.
quote:
Originally posted by tkizer

Crystal Reports should be the one to do the conversion then, not SQL. I'm not a Crystal Reports designer though, have never used it, but I know it can convert dates.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-03-10 : 07:27:06
In Crystal reports there is an option called Format Field which you can make use of.

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -