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 2012 Forums
 Transact-SQL (2012)
 Date add assistance please?

Author  Topic 

MelissaTsalicoglou
Starting Member

16 Posts

Posted - 2014-06-04 : 09:53:10
Hi All
Would it be possible to explain what the -DAY calculates?

DATEADD(day, -DAY(current_timestamp) +1, current_timestamp) as firstday

Best Regards
Melissa

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-06-04 : 10:26:24
run this and see the result

SELECT current_timestamp,
-DAY(current_timestamp),
DATEADD(day, -DAY(current_timestamp) +1, current_timestamp) as firstday



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2014-06-04 : 10:26:31
to get the first day of the actual month this solution subtracts the actual days and then adds 1 day.

2014-06-04 -4 +1 = 2014-06-01

dateadd() used with a negative value to add is like you have a function datesubtract()

:)


Too old to Rock'n'Roll too young to die.
Go to Top of Page

MelissaTsalicoglou
Starting Member

16 Posts

Posted - 2014-06-04 : 10:48:46
ok so '- Day' subtracts all the days from the current date of that month ? giving you the last day of the previous month? then you add 1 day to get to the first day of the month?

quote:
Originally posted by webfred

to get the first day of the actual month this solution subtracts the actual days and then adds 1 day.

2014-06-04 -4 +1 = 2014-06-01

dateadd() used with a negative value to add is like you have a function datesubtract()

:)


Too old to Rock'n'Roll too young to die.

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2014-06-04 : 10:50:56
yes :)


Too old to Rock'n'Roll too young to die.
Go to Top of Page

MelissaTsalicoglou
Starting Member

16 Posts

Posted - 2014-06-04 : 11:09:50
that seems easy enough ;) thank you!
quote:
Originally posted by webfred

yes :)


Too old to Rock'n'Roll too young to die.

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-06-04 : 13:33:09
Still, the date produces contains the hour, minute and second part.

SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE(), 0)



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
   

- Advertisement -