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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Datedif

Author  Topic 

csphard
Posting Yak Master

113 Posts

Posted - 2007-06-13 : 11:15:37
I want to subtract a date from the current date.

I want to use datediff but I see that the datepart is the date broken down to it parts and not the whole date. What do I use to do the following getdateI() - myDate.

Howard

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-06-13 : 11:16:56
quote:
I want to subtract a date from the current date.

you want the different of 2 dates in terms of days ? hours ? or ?


KH

Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2007-06-13 : 11:21:03
select getdateI() - @myDate
will do the subtraction.
It will give the result as a datetime which you can format as you wish.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

csphard
Posting Yak Master

113 Posts

Posted - 2007-06-13 : 11:28:05
In terms of days. I did not understand date. The following worked.

So by say day I told it to give me the number of days right?

datediff(day,payloc_comp_date,getdate())
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2007-06-13 : 11:36:01
Yep - that will give the number of midnights between the two dates.
Might be different (by 1) from
datepart(dd, getdate()-payloc_comp_date)
if you have a time in the date.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-06-13 : 11:38:19
Yes. datediff(day,payloc_comp_date,getdate()) will give you the number of days different between the 2 dates


KH

Go to Top of Page

csphard
Posting Yak Master

113 Posts

Posted - 2007-06-13 : 11:51:11
Thanks everyone

Howard
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-06-13 : 11:54:33
For more information on date related function, please refer to Books Online
and also
http://www.sqlteam.com/article/datediff-function-demystified
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64762
http://www.sql-server-performance.com/fk_datetime.asp



KH

Go to Top of Page
   

- Advertisement -