I am trying to compare dates in this format : 'dd-mm-yyyy' that are actually VARCHAR in the DB.
example of dates i have :
26-11-2011
14-04-2009
27-02-2010
03-11-2020
04-06-2021
my query :
SELECT table
FROM database
WHERE STR_TO_DATE(expiration, '%d-%m-%Y') <= DATE_FORMAT(NOW(), '%d-%m-%Y');
when I run this query it returns 0 rows when I know I have dates corresponding to the condition.
(as shown above, should returns rows like 26-11-2011
Thing is, when i run the query with >= it returns some good results :
03-05-2021
05-05-2021
01-05-2021
Thanks!
You are mixing DATE types and Strings holding a date representation.
You are converting your VARCHAR date to a DATE type using STR_TO_DATE() so use CURDATE() which also returns a valid DATE type so the comparison is comparing Apples with Apples
WHERE STR_TO_DATE(expiration, '%d-%m-%Y') <= CURDATE();
Related
i have airbnb data and i want to cast column last_reviews (which datatype is int) to date
this is my sql code http://sqlfiddle.com/#!9/b5ea42/31
how do i cast int > date?
or how do i create last_reviews column as datatype date?
The last_review date seems to be the number of days since 1900-01-01 so in MySQL you would:
SELECT '1900-01-01' + INTERVAL last_review DAY AS last_review_date
FROM ...
The result checks out for the sample data (which you should delete).
last_review values looks like VB(A) numeric representation for dates. If so then test this:
SELECT last_review, '1900-01-01' + INTERVAL last_review DAY
FROM airbnb
Adjust constant part (maybe it must be '1899-12-31'?) if needed.
I hava a sql query that says:
SELECT * FROM t_coun_student_sign_1018954616644580 signStu WHERE signStu.is_deleted = 0 AND signStu.student_wid = 1000363408 and signStu.attendance_date >= "2019-12-26 13:00:00.0"
The column attendance_date is a date type and the sql result does not contain attendance_date = 2019-12-26. So I thought that 2019-12-26 13:00:00.0 is not converted to date type like 2019-12-26.
But attendance_date is also an index and the sql query can use the index.
So if 2019-12-26 00:00:00.0 is not converted to date type, how could I make it happen?
What I want to know is that 2019-12-26 00:00:00.0 is to be converted to which type to compare with attendance_date, or what does mysql do when date, datetime, timeStamp type are compared with yyyy-MM-dd HH:mm:ss pattern string format.
what i what know "2019-12-26 00:00:00.0" is convert to which type to compare with attendance_date or what will mysql do when date datetime timeStamp type compare with the 'yyyy-MM-dd HH:mm:ss' pattern string
When operands have different but compatible datatypes in comparing operation then they are converted to the same datatype which guarantees that data loss or change will not occur.
So DATE type value is converted to DATETIME type in such case, and not backward (converting '2019-12-26 13:00:00.0' to DATE will alter the value).
To obtain the result which you need (does the literal is a parameter really?) you may convert the literal to DATA type explicitly:
and signStu.attendance_date >= DATE('2019-12-26 13:00:00.0')
If attendance_date be a date only column, then in the context of comparing it against a timestamp literal, it will behave as a timestamp on midnight of that date. So, assuming there were a record which had an attendance date value of 2019-12-27, then the WHERE clause would become:
WHERE ... AND '2019-12-27 00:00:00' > '2019-12-26 13:00:00.0'
^^^ the attendance "date"
Depending on the logic you want, you may have to change the WHERE clause to use some derivative of the attendance date.
Use "Date" function with column then you are able to compare.
Like- Date(signStu.attendance_date)
Use mysql DATE_FORMAT function for the query like below
DATE_FORMAT(signStu.attendance_date, '%Y-%m-%d %H.%i.%s') >= DATE_FORMAT("2019-12-26 13:00:00.0", '%Y-%m-%d %H.%i.%s')
I have got timestamps in epoch UNIX format. I want to run a query by directly giving date and not timestamp. How is that possible?
SELECT FROM_UNIXTIME(timestamp)
FROM report_data
WHERE timestamp = '1399376713'
I used this to convert to human readable format.
My database is something like this
timestamp event_type flags
1399357862 701 null
I want to give a particular date in my query and get the result.
It's possible using the FROM_UNIXTIME function.
This assumes that your table contains columns in DATETIME or TIMESTAMP, and you are wanting to supply 32-bit integer values in the query.
For example:
SELECT ...
FROM mytable t
WHERE t.datetime_col >= FROM_UNIXTIME( ? )
AND t.datetime_col < FROM_UNIXTIME( ? )
The integer values supplied as arguments to the FROM_UNIXTIME function will be interpreted as unix-style "seconds since epoch" integer values, and be converted to a DATETIME value using the current timezone setting of the client connection.
This approach will enable MySQL to use a range scan operation using an index with a leading column of datetime_col.
What's not at all clear is what the datatype of your column is, and what values you want to supply in the query. If the columns is datatype DATE, DATETIME or TIMESTAMP (which would be the normative pattern for storing date/time data), then you can specify date literals in standard MySQL format, 'YYYY-MM-DD HH:MM:SS'.
WHERE t.timestamp_col >= '2015-02-11 07:00'
AND t.timestamp_col < '2015-02-11 23:30:00'
If you are storing the "timestamp" as an integer value, then you will need the right side of the predicates to return an integer value, e.g.
WHERE t.integer_col >= UNIX_TIMESTAMP('2015-02-10')
AND t.integer_col < UNIX_TIMESTAMP('2015-02-10' + INTERVAL 24 HOUR)
I have stored the dates as string in my database.I know it is not good,but project already has been half developed before i take over and where dates were stored as string,so i was continuing the same way.
Now i want to select dates from table where date is greater than a specific date.
I tried the following query
SELECT
*
FROM
dates
where
STR_TO_DATE(date, '%Y-%m-%d') > "2014-01-01"
but it is not returning only greater values.
Please help me to solve problem.
Demo
Your dates are not in YYYY-MM-DD format. Use the right format!
SELECT *
FROM dates
where STR_TO_DATE(date, '%m-%d-%Y') > date('2014-01-01')
If you are going to store dates as strings, then the best way is in the ISO format of YYYY-MM-DD.
You should read the documentation on str_to_date() (here).
Convert everything to date and it should be fine. Now you are comparing date and string.
What type has the date? I'd prefer a ' instead of " for strings in SQL. Let's assume that date is a VARCHAR or TEXT field (depending on which database you are using):
SELECT *
FROM dates
WHERE STR_TO_DATE(date, '%Y-%m-%d') > STR_TO_DATE('2014-01-01', '%Y-%m-%d')
If date is a real DATE
SELECT *
FROM dates
WHERE trim(date) > STR_TO_DATE('2014-01-01', '%Y-%m-%d')
Or you just convert it into a number format date_format(date,'%Y%m%d') > 20140101
I have the following where clause
AND DATE_FORMAT( l.created_on, "%d/%m/%Y" ) BETWEEN '06/02/2013' AND '07/02/2013'
where created_on is a timestamp.
Now for some reason the query returns rows from previous months as well. anyone knows why?
NOTE :
I need the date to be in that specific format
Mysql string date format follows pattern yyyy-mm-dd. Do not convert to dates if you have timestamps, just compare the timestamps.
WHERE l.created_on
BETWEEN UNIX_TIMESTAMP('2012/02/06') AND UNIX_TIMESTAMP('2013/02/07')
if created_on is already a date (datatype),
you can directly query it using,
WHERE created_on BETWEEN '2013-02-06' AND '2013-02-07'
but if date is a string, use STR_TO_DATE
WHERE STR_TO_DATE(l.created_on, '%d-%m-%Y') BETWEEN '2013-02-06' AND '2013-02-07'
UPDATE 1
since you don't want to change the format of your inputted date, then you need to format it using STR_TO_DATE
WHERE l.created_on BETWEEN STR_TO_DATE('06/02/2013','%d/%m/%Y') AND
STR_TO_DATE('07/02/2013','%d/%m/%Y')