I have a series of dates in my database that are in String format.
I am trying to convert these to date using the following command:
SELECT STR_TO_DATE(Date_Scanned, '%d/%m/%Y')FROM urls WHERE Date_Scanned
BETWEEN "01/02/2018" AND "05/04/2018"
My problem seems to occur when the day contains a 0, for example the below command returns all desired dates:
SELECT STR_TO_DATE(Date_Scanned, '%d/%m/%Y')FROM urls WHERE Date_Scanned
BETWEEN "01/02/2018" AND "15/04/2018"
However, this command returns zero results:
SELECT STR_TO_DATE(Date_Scanned, '%d/%m/%Y')FROM urls WHERE Date_Scanned
BETWEEN "01/02/2018" AND "05/04/2018"
I believe there is no error in your code but instead it is returning empty row. The reason is because the parameter you are passing is evaluated as a string and not as a date. Your WHERE clause filters the column Date_Scanned in string.
If you wanted to filter the column based on real date datatype, you should be using the STR_TO_DATE() in the WHERE clause and not in the SELECT clause.
SELECT STR_TO_DATE(Date_Scanned, '%d/%m/%Y')
FROM urls
WHERE STR_TO_DATE(Date_Scanned, '%d/%m/%Y') BETWEEN '2018-02-01' AND '2018-04-05'
However, based on your comment, you wanted to stick the format you want so you have to cast the column as well as the value. But why make it more complicated?
WHERE STR_TO_DATE(Date_Scanned, '%d/%m/%Y')
BETWEEN STR_TO_DATE('01/02/2018', '%d/%m/%Y')
AND STR_TO_DATE('05/04/2018', '%d/%m/%Y')
Related
I'm trying to order my results by date, but the dates are strings, not date objects, so the results aren't showing up properly. I tried converting the date string to a date object in the sql request, but now nothing returns. There are no results showing on the screen. Here's my code:
SELECT * FROM tblnewsftb
WHERE Status = 'Active'
ORDER BY CONVERT(datetime, ItemDate, 102) DESC
What am I doing wrong?
In MySQL, CONVERT() only takes two arguments, the expression and a data type. The data type goes second.
Example:
CONVERT(ItemDate, DATETIME)
But this will work only if the expression (your ItemDate column in this example) is convertible as-is to a DATETIME. I would guess you're using some date format that MySQL doesn't support, like 'MM/DD/YYYY' or something else.
You'll have to use STR_TO_DATE() if you have a custom format.
Example:
STR_TO_DATE(ItemDate, '%m/%d/%Y')
It would really be best if you store datetime values in a proper DATETIME column instead of a string column if you want them to sort correctly.
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')
When I am trying to convert varchar to date, I get Null values in return.
I have values as 05-MAR-2015 in my column.
I am running following query.
select STR_TO_DATE('n.Invoice_date','%d-%c-Y') from table n;
Once I run above query I get null values in return.
I want to introduce a new column in date format.
Note that the literal string 'n.invoice_date' is not a valid date. What you mean is:
SELECT STR_TO_DATE(n.invoice_date, '%d-%b-%Y') FROM TABLE n
Your error is in usage of %c instead of %b to get the date. You mixed the formatting of the date with the creation of a date value. This should do it:
SELECT DATE_FORMAT(STR_TO_DATE(n.invoice_date,'%d-%b-%Y'), '%d-%c-%Y') FROM table n;
This results in: 05-3-2015
Here you first create a date with STR_TO_DATE which must match the format in which it is stored in the field. Then you can format it with DATE_FORMAT in the way you want to.
See the MYSQL Docs for more information: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
I am trying folowing on my_table where modifiedtime is of type datetime
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where DATE_FORMAT(modifiedtime,'%d-%m-%Y') between '05-11-2013' and '28-11-2013';
The query gives me some other record too which are not falls between above dates, for example there is a record in result set dated '04-01-2014'
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where DATE_FORMAT(modifiedtime,'%d-%m-%Y')='05-11-2013'
this query works fine and gives all the records for the given date
why the first behaves like that?
How can i correct it?
what is the efficient way to implement it?
such that i can get all the records only between given two dates.
SELECT
DATE_FORMAT(modifiedtime, '%d-%m-%Y')
FROM
my_table
WHERE
modifiedtime BETWEEN STR_TO_DATE('05-11-2013', '%d-%m-%Y') AND STR_TO_DATE('28-11-2013', '%d-%m-%Y');
DATE_FORMAT() returns TEXT type column and dates can't be applied.
Use without DATE_FORMAT in the WHERE
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where modifiedtime between '05-11-2013' and '28-11-2013';
you DATE_FORMAT function converts the column modifiedtime to String.
and hence in your first query you do a string comparison rather then a date comparison.
Also your date literal is not incorrect. It must be of form YYYY-MM-DD
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where cast(modifiedtime as date) between '2013-11-05' and '2013-11-28';
I'm trying to make some vchar values searchable based on a date.
The strings I have to work with look like this:
1/7/2006 12:45:24 AM
1/7/2006 1:18:36 AM
1/7/2006 1:21:43 AM
1/7/2006 1:32:09 AM
3/30/2006 12:32:10 PM
3/30/2006 1:19:30 PM
3/30/2006 1:20:44 PM
So first off let's get rid of the AM.. PHPMyAdmin the sql query is:
SELECT trim('AM' FROM `orderdate`) FROM tblorders
This works to get rid of the AM now let's set the values as a variable and try to wrap a string to str_to_date() around the results:
SELECT trim('AM' FROM `orderdate`) AS `value`, STR_TO_DATE(`value`,'%d,%m,%Y') FROM tblorders
This yields value as an unknown column. How else do you string two function values together so as to then use them to be filtered such as WHERE value > 2/1/2006 ?
You can compose functions like this:
select str_to_date(trim('AM' from orderdate), '%m/%d/%Y')
Note that I also corrected your date format to match your data. You don't actually need to trim those values to use str_to_date on them, just this will work fine:
select str_to_date(orderdate, '%m/%d/%Y')
If you want to use that in your WHERE clause then put the function calls in there:
select trim('AM' from orderdate), str_to_date(orderdate, '%m/%d/%Y')
from your_table
where str_to_date(orderdate, '%m/%d/%Y') > '2006-01-02'
and let the optimizer recognize the repetition or use a derived table:
select value, the_date
from (
select trim('AM' from orderdate) as value, str_to_date(orderdate, '%m/%d/%Y') as the_date
from your_table
) dt
where the_date > '2006-01-02'
Note the use of ISO 8601 dates in the query, that format is unambiguous and any database worth using will understand it regardless of your locale settings.
I'd also recommend that you fix your schema to use real timestamps instead of those strings.