Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I have orderid and orderdate column. orderdate containing date format like 'yyyy-mm-dd'.
i want to display the order placed in the month of February.
How can we achieve this in the SQL?

Waiting for quick response.

What I have tried:

I have tried with month and datepart function in sql but it does not work.
Posted
Updated 15-Jul-22 3:02am
Comments
Richard MacCutchan 15-Jul-22 3:25am    
"orderdate containing date format like 'yyyy-mm-dd'."
Which is completely wrong. SQL has perfectly useful Date/DateTime types, which allow you to extract the month without complicated programming.
prasad patil Jul2022 15-Jul-22 3:28am    
give me solution to fetch only February month

Thank U all for your response.
Below is the solution for the problem.

SELECT * from orders where (DATEPART(mm, orderdate)) = 2
 
Share this answer
 
SQL
SELECT ... WHERE DATEPART(m, MyDateColumn) = 2
 
Share this answer
 
Comments
prasad patil Jul2022 15-Jul-22 4:10am    
I was trying the solution query u have given in w3school site database but it is showing error like, m is not defined.
CHill60 15-Jul-22 4:32am    
try month instead. Or look up DATEPART in w3schools SQL Server DATEPART() Function[^]
prasad patil Jul2022 15-Jul-22 4:51am    
i am trying to execute the below query in w3school sql server.

SELECT * from orders WHERE DATEPART(month, orderdate) = 2

but it is showing error like month is not defined
0x01AA 15-Jul-22 8:52am    
Have look here, SQLite: How to Extract the Day, Month, and Year from a Date in SQLite[^]
This should work: SELECT * from orders WHERE strftime('%m', orderdate) = '02'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900