sql server - Dateadd returning the wrong days in the month -
i have stored procedure runs pull data sales in given month. not return 31 days on months have 31 days. need understanding breakdown of following string
(dateadd(dd,-(datepart(dd,getdate())),convert(char(8),getdate(),112)))+'23:59:59')
i understand convert(char(8),getdate(),112)
taking system date , converting yyyymmdd
, datepart(dd,getdate())
takes system date , takes day part, cannot decipher entire string.
there issue expression have given. bracket after 23:59:59
not have opening brace.
however expression intends is: (datepart(dd,getdate()))
getting the current date's day part
convert(char(8),getdate(),112))
getting current date in yyymmdd
(dateadd(dd,-(datepart(dd,getdate())),convert(char(8),getdate(),112)))
subtracting day part today's date (see negative sign).
thus trying first day of current month. in case result of above expression crosses last day of previous month, adds 23 hours 59 minutes.
the logic intended last day of previous month. adding of ~24 hours creating confusion.
Comments
Post a Comment