Select records from the beginning of the month to the current date

I am trying to select records that have been added to the database between the beginning of the current month and the current day - I more or less know how to get records from the current day and for a certain period of time, but how can I get it so that it started at the beginning of the current calendar month?

+1
source share
5 answers
DECLARE @sm DATETIME; SET @sm = DATEADD(DAY, 1-DAY(GETDATE()), DATEDIFF(DAY, 0, GETDATE())); SELECT columns FROM dbo.foo WHERE datetime_column >= @sm; 
+5
source
 WHERE YEAR([Date])=YEAR(GETDATE()) AND MONTH([Date])=MONTH(GETDATE()) AND DAY([Date])<=DAY(GETDATE()) 
+2
source
 select * from YourTable where DateCol >= dateadd(month, datediff(month, 0, getdate()), 0) 
+2
source

Basically what you are doing here is a month, adding '/ 01' and then adding a year. Select it as a string to handle the addition, and then produce it as a DateTime.

So it's a little more important than doing some math, but I'm reading this.

 DECLARE @firstOfMonth DATETIME SET @firstOfMonth = CAST(CAST(DATEPART(mm, GetDate()) as varchar) + '/01/' + cast(DATEPART(yyyy, Getdate()) as varchar) as datetime) WHERE DateToCheck BETWEEN @firstOfMonth and GetDate() 
0
source
 WHERE DateToCheck > LAST_DAY(CURDATE()-INTERVAL 1 MONTH)) 

http://www.sqlfiddle.com/#!2/3d673/2/0

0
source

Source: https://habr.com/ru/post/912583/


All Articles