Convert date in where clause MySQL - mysql

I'm looking to convert a date from 'd/m/Y' to 'Y/m/d' inside where clause like this:
SELECT * FROM myTable WHERE DATE_FORMAT(myDate,'%Y/%m/%d') = '2020/08/01'
this query always returns an empty result
There is a way to do it right ?
Thank you.

Simply convert you data to a date and then format it
SELECT * FROM myTable WHERE DATE_FORMAT(STR_TO_DATE(myDate,'%d/%m/%Y'),'%Y/%m/%d') = '2020/08/01'
Or make them both to dates, and then you can use Date fucntions
SELECT * FROM myTable WHERE STR_TO_DATE(myDate,'%d/%m/%Y') = STR_TO_DATE('2020/08/01','%Y/%m/%d')
The best is to save date as Date/Datetime/timestamp, so that you can with out problem, calculate with them and you always can convert them into any output

Related

mysql date_format() or str_to_date() in where clause

I've view named Sales View
select * from SalesView;
I want to fetch records from SalesView having order_date 9-OCT-2019.
Value for Order date is supplied from GUI in format DD-MM-YYYY.
I tried,
select *
from salesView
where str_to_date(order_date,'%d-%m-%Y') = str_to_date('09-10-2019','%d-%m-%Y')
order by oID;
I've also tried date_format() instead of str_to_date(), but It is returning nothing.
Check your where clause
where str_to_date(order_date,'%d-%m-%Y') = str_to_date('09-10-2019','%d-%m-%Y')
Replace order_date's format with the format in which database stored it. YYYY-MM-DD
where str_to_date(order_date,'%Y-%m-%d') = str_to_date('09-10-2019','%d-%m-%Y')

SQL select order by date in varchar

I have my date in database in varchar column and i can't change it. However i want to sort things from newest to latest. My date in database looks like:
2014-09-22 10:28:28
So what i try is something like:
$sql = "SELECT * FROM axnmrs_cases WHERE vin = :vin ORDER BY STR_TO_DATE(date_created,'%b-%e-%Y') ASC LIMIT 30";
But unfortunately this not change anything for me , even if i change ASC to DESC , nothing changeing in result
and also something like:
$sql = "SELECT * FROM axnmrs_cases WHERE vin = :vin ORDER BY CONVERT(date_created, date, 103)";
This throw syntax SQL error and I have no idea why.
Is here anybody who can show me the right way?
Date stored in varchar is not a real date and hence the order by also does not give you what you want. The best approach would be store date always in mysql native data types. However in your case you can use str_to_date() function to convert the varchar dates to real date and then use it for sort something as
order by str_to_date(date_created,'%Y-%m-%d %H:%i:%s');
$sql = "SELECT * FROM `axnmrs_cases` WHERE `vin` = ':vin' ORDER BY `date_created` ASC LIMIT 30";
Already tried something like this?
You are using the wrong format in your STR_TO_DATE function, if the date is in the format:
2014-09-22 10:28:28
Then you need to use
STR_TO_DATE(date_created, '%Y-%m-%d %H:%i:%s')
i.e. the format you give should match the format your varchar is in.
Example on SQL Fiddle
In your case you are using '%b-%e-%Y', so you are looking for a date like Jan-1-2014, for a full list of the specifiers in the format defintion see the My SQL Docs for DATE_FORMAT
Also, CONVERT(date_created, date, 103) does not work because it is SQL Server Syntax.
Finally, I would really, really try and change the column data type. Storing dates as a varchar is never a good idea. Anything else is just a workaround, not a solution.

mysql select timestamps between a and b returning all or 0 timestamps

Tried this
select * from table where timestamp_field between 1330560000 and 1336170420
and this
select * from table where timestamp_field >=1330560000 and timestamp_field<=1336170420
both returning empty result set.
But this
select * from table where timestamp_field >= 1330560000
returns all the rows
To make things more absurd
select * from table where timestamp_field <= 1336170420
returns empty result set.
Of course, there exists timestamp values before, between and after 1336170420=4.may 2012. and 1330560000=1.march 2012.
Timestamp values are ok, at least phpmyadmin shows correct (human-readable) date-time values.
I created timestamps by parsing strings, with
UPDATE table SET timestamp_field = STR_TO_DATE(timestamp_string, '%d.%m.%Y')
Guess I'm missing something, but can't find what!?
MySQL expects date literals, not integer ones:
SELECT *
FROM table
WHERE DATE(timestamp_field) BETWEEN '2012-03-01' AND '2012-05-04'
To use integers (assuming that they are seconds since the UNIX epoch), first convert them using MySQL's FROM_UNIXTIME() function:
SELECT *
FROM table
WHERE timestamp_field BETWEEN FROM_UNIXTIME(1330560000)
AND FROM_UNIXTIME(1336170420)
Or else use UNIX_TIMESTAMP() to convert your column to its UNIX representation:
SELECT *
FROM table
WHERE UNIX_TIMESTAMP(timestamp_field) BETWEEN 1330560000 AND 1336170420
why not convert the timestamps from_unixtime(timestamp/1000) as dateTime while using your sql code? have you tried that?

How to select rows within some date range

I want to select rows that falls between some date range, I tried the following query but it didn't work.
SELECT * FROM tbl WHERE DATE_FORMAT(date_col, '%2011-%c-%e') BETWEEN '2011-11-28' AND '2011-12-5'
It doesn't seem like the BETWEEN keyword works on date. Please how do I get the results? Thanks
You don't need to use DATE_FORMAT if you want to compare dates.
SELECT *
FROM tbl
WHERE DATE(date_col) BETWEEN '2011-11-28' AND '2011-12-05'
Your code compares strings, assuming you use DATE_FORMAT(date_col, '%Y-%c-%e')
SELECT * FROM tbl WHERE DATE_FORMAT(date_col, '%Y-%m-%d') BETWEEN '2011-11-28' AND '2011-12-5'

MySQL select date range issue

I'm getting some strange results from using BETWEEN in my sql query. Wondering if anyone can help me to understand why i am getting these results.
I'm searching a date range in the format of dd/mm/yyyy. So i want to select all entries within a certain date range.
$dbSearchRecords_result = "SELECT * FROM $tbl_name WHERE Date BETWEEN '$DateFrom_order' AND '$DateTo_order'";
$dbSearchRecords_result = mysql_query($dbSearchRecords_result);
I am then calling the results in a while statement from an array
while ($row = mysql_fetch_array($dbSearchRecords_result)){
Now if I search BETWEEN 12/02/2011 14/02/2011 there is a date returned from 13/12/2010.
Yet if I search 12/02/2011 13/02/201 I do not get the result of 13/12/2010.
Any ideas would be most appreciated.
Thanks.
The BETWEENoperator is most likely reading your ranges as strings. From the book:
For best results when using BETWEEN
with date or time values, use CAST()
to explicitly convert the values to
the desired data type. Examples: If
you compare a DATETIME to two DATE
values, convert the DATE values to
DATETIME values. If you use a string
constant such as '2001-1-1' in a
comparison to a DATE, cast the string
to a DATE.
So, try:
SELECT * FROM `mytable`
WHERE `date` BETWEEN CAST('2011-01-02' AS DATE) AND CAST('2011-12-02' AS DATE)
MySQL's needs values in this format to do a proper comparison:
YYYY-MM-DD
you could use STR_TO_DATE to convert your string into the right format.
Plus obviously, the Date field needs to be of the DATE or DATETIME type.
try to format the values as DATE.. as in
$dbSearchRecords_result = "SELECT * FROM $tbl_name WHERE Date BETWEEN DATE('$DateFrom_order') AND DATE('$DateTo_order')";