part, dt_val1, dt_val2)
For example, the following code returns the difference in terms of days between two values.
SELECT DATEDIFF(day, '20080212', '20090212');
This code returns the output 366.
Ready for a bit more sophisticated use of the DATEADD and DATEDIFF functions? You can use the
following code in versions prior to SQL Server 2008 to set the time component of the current system
date and time value to midnight.
SELECT
DATEADD(
day,
DATEDIFF(day, '20010101', CURRENT_TIMESTAMP), '20010101');
This is achieved by first using the DATEDIFF function to calculate the difference in terms of whole
days between an anchor date at midnight (‘20010101’ in this case) and the current date and time (call
that difference diff). Then, the DATEADD function is used to add diff days to the anchor. You get the
current system date at midnight.
Interestingly, if you use this expression with a month part instead of a day, and make sure to use an
anchor that is the first day of a month (as in this example), you get the first day of the current month.
www.it-ebooks.info
Do'stlaringiz bilan baham: |