Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT row by DATEPART()

I need to get rows from the database where the records are of one month. I tried this SELECT:

$result = mysql_query("SELECT * FROM my_table WHERE DATEPART('month', date_column)=11");

In database is a lot of rows that have a date in the 11. month, but i do not get any results. Can anyone help me? Thank!

like image 352
user1827257 Avatar asked Nov 15 '12 16:11

user1827257


People also ask

How do I select a row in a date range in SQL?

Select rows with date range using DATE() function Date() function will extract the date part from the datetime expression in MySQL.

What is Datepart function in SQL?

Definition and Usage. The DATEPART() function returns a specified part of a date. This function returns the result as an integer value.

Can you select a row in SQL?

You can use SQL statements to select rows from the database to display on your report. Selecting rows limits, or creates a subset of, the data in a table. You select rows by creating a row condition. You can select rows that have no data in a column.

How do I get the latest row in MySQL?

To get the last record, the following is the query. mysql> select *from getLastRecord ORDER BY id DESC LIMIT 1; The following is the output. The above output shows that we have fetched the last record, with Id 4 and Name Carol.


2 Answers

There is no DATEPART function in MySQL. Use MONTH(date_column) or EXTRACT(MONTH FROM date_column) instead.

like image 84
MvG Avatar answered Sep 17 '22 18:09

MvG


    SELECT * FROM my_table WHERE MONTH(date_column)=11
like image 26
Imre L Avatar answered Sep 17 '22 18:09

Imre L