0 votes
1 view
in SQL by (22.4k points)

I just need to select the first day of the month of a given datetime variable.

I know it's quite easy to do using this kind of code:

select CAST(CAST(YEAR(@mydate) AS VARCHAR(4)) 

+ '/' + CAST(MONTH(@mydate) AS VARCHAR(2)) + '/01' AS DATETIME)

But this is not very elegant, and probably not very fast either.

Is there a better way to do this? I'm using SQL Server 2008.

1 Answer

0 votes
by (40.3k points)

Use the following code:

SELECT DATEADD(month, DATEDIFF(month, 0, @mydate), 0) AS StartOfMonth

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...