Lill Lansey
Lill Lansey

Reputation: 4915

How to write this tsql datetime code more concisely and or elegantly

I need to get the tsql datetime of the first day of the current month to use in a query.

I have come up with

declare @StartDate datetime
set  @StartDate =  cast( MONTH( getdate()) as char(2))+ '/1/' 
       +  cast( Year( getdate()) as char(4))
select @StartDate

But was wondering about a better way to do this.

Suggestions?

Upvotes: 0

Views: 77

Answers (2)

Mikael Eriksson
Mikael Eriksson

Reputation: 138980

select dateadd(month, datediff(month, 0, getdate()), 0)

Upvotes: 3

Taryn
Taryn

Reputation: 247760

How about this:

SELECT DATEADD(month, DATEDIFF(month, 0, getdate()), 0) AS FirstOfMonth

Upvotes: 4

Related Questions