chazpe
chazpe

Reputation: 1

Get the first day of the month from a datetime column

I am trying to convert this Oracle statement to SQL Server:

trunc(e.evt_created) - (to_number(to_char(e.evt_created, 'DD')) - 1)As "Month Raised"

This gets the first day of the month, so if e.evt_created holds 2009-05-11 10:19:27.0 then I need 2009-05-01.

What is the SQL Server equivalent to get that result?

Upvotes: 0

Views: 490

Answers (2)

Slava N.
Slava N.

Reputation: 596

If you use SQL Server 2012 you could do this:

declare @d datetime = getdate();
select datefromparts(year(@d),month(@d),1);

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269503

You seem to want the first day of the month without a time component. To do this in SQL Server:

select dateadd(day,
               1 - day(e.evt_create),
               cast(e.evt_created as date)
              ) as FirstDayOfMonth

Upvotes: 1

Related Questions