SQL Server Query select where date between "1st day of current month" and "current day"

pvabu6sv  于 2023-04-19  发布在  其他
关注(0)|答案(5)|浏览(145)

I need to select data from SQL Server database between the 1st day of the current month and current day.

SELECT *
      FROM table_name 
     WHERE date BETWEEN "1st day of current month" AND "current day"
fdbelqdn

fdbelqdn1#

You can make use of EOMONTH() to jump to the last day of the month, of the previous month. Then, just add 1 day, by using DATEADD() with 1 as the increment for the day, to get the 1st day of the current month.

SELECT *
FROM <table>
WHERE <date> BETWEEN DATEADD(DAY, 1, EOMONTH(GETDATE(),  -1)) and GETDATE()
7eumitmz

7eumitmz2#

I think this should work,

SELECT * 
FROM   TABLE_NAME 
 WHERE date BETWEEN DATEADD(mm, DATEDIFF(mm,0,date), 0) AND GETDATE()
h43kikqp

h43kikqp3#

May this will help you,

select * from TABLE_NAME 
where date between 
dateadd (DAY, -(datepart (DAY, getdate ())) + 1, convert (date, GETDATE ())) 
and 
convert (date, getdate ())
z9smfwbn

z9smfwbn4#

I don't recommend using between for this purpose. I prefer to have code that works on both date s and datetime s without modification.

In addition, SQL Server has the convenient datefromparts() function. So, I recomend:

WHERE date >= DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 1) AND
      date < CONVERT(DATE, DATEADD(DAY, 1, GETDATE()))
dffbzjpn

dffbzjpn5#

WHERE date >= DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 1) AND
       date < CONVERT(DATE, DATEADD(DAY, 1, GETDATE()))

相关问题