Get records of current month
This query should work for you:
SELECT *
FROM table
WHERE MONTH(columnName) = MONTH(CURRENT_DATE())
AND YEAR(columnName) = YEAR(CURRENT_DATE())
SQL select records with current month
Here is one option:My goal to select the records with current month (2020-04-XX)
where NewMonth >= datefromparts(year(getdate()), month(getdate()), 1)
If you need an upper bound too:where
NewMonth >= datefromparts(year(getdate()), month(getdate()), 1)
and NewMonth < dateadd(month, 1, datefromparts(year(getdate()), month(getdate()), 1))
If you want the previous month:where
NewMonth >= dateadd(month, -1, datefromparts(year(getdate()), month(getdate()), 1))
and NewMonth < datefromparts(year(getdate()), month(getdate()), 1)
Or two months ago:where
NewMonth >= dateadd(month, -2, datefromparts(year(getdate()), month(getdate()), 1))
and NewMonth < dateadd(month, -1, datefromparts(year(getdate()), month(getdate()), 1))
How to get records created at the current month?
You need to enclose the where in a lamda as well.
scope :from_this_month, lambda { where("votes.created_at > ? AND votes.created_at < ?", Time.now.beginning_of_month, Time.now.end_of_month) }
Otherwise it may appear to work and your tests will all pass, but if your app runs for more than a month you will start to get incorrect results because Time.now is evaluated when the class loads, not when the method is called. Fetch records of current month using PostgreSQL query
You can compare the month and year of a date with the current one. But the index by field will not be used, you can build a separate index by year and month for this.
select *
from your_table
where extract(YEAR FROM createdAt) = extract(YEAR FROM now())
and extract(MONTH FROM createdAt) = extract(MONTH FROM now())
MYSQL query to show current month records count
You're close, COUNT()
doesn't only count TRUE
values, it counts any and all non-NULL
values, so it's counting the FALSE
values too.
Try...
SELECT
COUNT(*),
SUM(DATE_FORMAT(date, '%Y-%m-01') = DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01'))
FROM
orders
When summed, TRUE
is treated as 1
, and FALSE
is treated as 0
I also use DATE_FORMAT()
instead of MONTH()
so you don't accidentally count the values from the same month in previous years.
- If you want that, just change the
DATE_FORMAT()
back toMONTH()
and keep theSUM()
getting records for current month only
Change your CTE to this:
WITH times
AS (
SELECT t1.EmplID
,t3.EmplName
,min(t1.RecTime) AS InTime
,max(t2.RecTime) AS [TimeOut]
,cast(min(t1.RecTime) AS DATETIME) AS InTimeSub
,cast(max(t2.RecTime) AS DATETIME) AS TimeOutSub
,t1.RecDate AS [DateVisited]
FROM AtdRecord t1
INNER JOIN AtdRecord t2 ON t1.EmplID = t2.EmplID
AND t1.RecDate = t2.RecDate
AND t1.RecTime < t2.RecTime
INNER JOIN HrEmployee t3 ON t3.EmplID = t1.EmplID
WHERE MONTH(t1.RecDate)=MONTH(Getdate())
AND YEAR (t1.RecDate)=YEAR(Getdate())
GROUP BY t1.EmplID
,t3.EmplName
,t1.RecDate
)
Please note that if you run this on the first of any month, you will get data only for that day and not for the previous month. How to select just current month data in SQL?
Since you have not specified your DB Product but from your syntax It seems SQL Server. YOu can use MONTH function here -
Select *
FROM CORP.T_LOGI_RN_Planeacion_Entregas
Where MONTH(CORP.T_LOGI_RN_Planeacion_Entregas.Date) = MONTH(GETDATE())
AND YEAR(CORP.T_LOGI_RN_Planeacion_Entregas.Date) = YEAR(GETDATE());
Retrieve MySql Records Within a Specific Month but Less than the current year and Less than the current Day
Use the functions DAY()
, MONTH()
and YEAR()
in your conditions:
SELECT *
FROM my_table
WHERE MONTH(date) = MONTH(CURRENT_DATE)
AND YEAR(date) < YEAR(CURRENT_DATE)
AND DAY(date) < DAY(CURRENT_DATE);
See the demo.
Related Topics
Best Way to Create a Blog with Static Pages in Ruby
Rails3 Scope for Count of Children in Has_Many Relationship
Why Can't The Mail Block See My Variable
Spawning an Independent Thread or Process in Ruby
Ruby 2.0 Bytecode Export/Import
Using Rails with Paperclip and Swfupload
Ruby - Append Content at The End of The Existing S3 File Using Fog
If I Have a Stripe Token from a Charge, How to Get Its Charge Id
How to Properly Manage The Rails Tmp Directory
How to Wait for System Command to End
Ruby: Is It Acceptable to Put More Than One Class in a File
Posting Ruby Data in JSON Format with Net/Http
Include Module in All Minitest Tests Like in Rspec
How to Deploy a Threadsafe Asynchronous Rails App
What Ruby Technique Does Rails Use to Make My Controller Methods Render Views