Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use greater than operator with date?

People also ask

How can date be greater than condition in SQL?

In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a 'YYYY-MM-DD hh:mm: ss. mmm' pattern.

Where date is greater than in MySQL?

MySQL where date greater than 30 days agoDATE(expression): Will get the date value from the DATE or DATETIME expression passed in as a parameter. CURDATE(): Will return current date either in 'YYYY-MM-DD' or 'YYYYMMDD' format depending on if the curdate() function is used in a string or numeric context.

How do you do greater than in SQL?

You can use the > operator in SQL to test for an expression greater than. In this example, the SELECT statement would return all rows from the customers table where the customer_id is greater than 6000.

How do you write a date in a query?

SQL Server comes with the following data types for storing a date or a date/time value in the database: DATE - format YYYY-MM-DD. DATETIME - format: YYYY-MM-DD HH:MI:SS. SMALLDATETIME - format: YYYY-MM-DD HH:MI:SS.


you have enlosed start_date with single quote causing it to become string, use backtick instead

SELECT * FROM `la_schedule` WHERE `start_date` > '2012-11-18';
  • SQLFiddle Demo

In your statement, you are comparing a string called start_date with the time.
If start_date is a column, it should either be

 
  SELECT * FROM `la_schedule` WHERE start_date >'2012-11-18';
 

(no apostrophe) or


SELECT * FROM `la_schedule` WHERE `start_date` >'2012-11-18';

(with backticks).

Hope this helps.


Try this.

SELECT * FROM la_schedule WHERE `start_date` > '2012-11-18';

Adding this since this was not mentioned.

SELECT * FROM `la_schedule` WHERE date(start_date) > date('2012-11-18');

Because that's what actually works for me. Adding date() function on both comparison values.


In my case my column was a datetime it kept giving me all records. What I did is to include time, see below example

SELECT * FROM my_table where start_date > '2011-01-01 01:01:01';

I have tried but above not working after research found below the solution.

SELECT * FROM my_table where DATE(start_date) > '2011-01-01';

Ref