简体   繁体   中英

datediff(dd,… returning month data

I've come across this in a query:

[date_start] >= DATEADD(dd, -DAY(GETDATE()) + 1, DATEDIFF(dd, 0, GETDATE()))

I understand that this is in part removing the time element from the field, but can someone explain to me how this is returning the current month data without using the 'month' function?

It takes the current day of the month (day(GetDate())+1) and subtracts it from the number of whole days since day 0.

[Date_start] >= 
DATEADD(dd, --Add Integer X to DateTime Y

-DAY(GETDATE()) + 1,  --X = - Current Day of Month + 1

DATEDIFF(dd, 0, GETDATE()) --Y = The number of days between day 0 and now.
                           --(in whole days to round out the 'time' part of the datetime)

There's no standard function to achieve this.

Yours is not much less intuitive than other methods I've seen, although using a negative number in date add () is confusing, as is the reliance on the result of the datediff() being implicitly converted from int to datetime .

Other Methods Here: SQL Server Helper - Date Functions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM