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
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 have a column where the dates are type varchar. For example:
15-10-2018
16-10-2018
19-10-2018
23-10-2018
29-10-2018
8-11-2018
9-11-2018
10-11-2018
12-11-2018
when I consult with the following query
SELECT DISTINCT date FROM `test` WHERE date BETWEEN '15-10-2018' and '9-11-2018'.
I have the right result.
15-10-2018
16-10-2018
19-10-2018
23-10-2018
29-10-2018
8-11-2018
9-11-2018
but if the query is:
SELECT DISTINCT date FROM `test` WHERE date BETWEEN '15-10-2018' and '10-11-2018'.
or
SELECT DISTINCT date FROM `test` WHERE date BETWEEN '15-10-2018' and '12-11-2018'.
The answer I get is empty.
I think it's only validating the days in the sql.
I need to get the right dates.
I think the problem is the fact that the column is varchar, so it's comparing characters instead of a range of dates. I will recommend convert the column to date type and try again.
Alternative if you cannot change the type of the column you could cast it to date format like this:
SELECT DISTINCT `date` FROM `test` WHERE STR_TO_DATE(`date`,'%d-%m-%Y') BETWEEN '2018-10-15' AND '2018-11-10';
I tested with your data and it works. Of course this could put some extra effort on the database and will not use indexes.
You need to set the datatype to date and update your dates to be using date for a more reliable result. Once done you should be using the database format for the dates in your WHERE clause.
Try
SELECT DISTINCT date FROMtestWHERE date BETWEEN '2018-10-15' and '2018-11-10'
I'm querying
SELECT * FROM tempLog WHERE date BETWEEN '23-03-2017' AND '02-04-2017'
and the result is null. How to fix this. But
SELECT * FROM tempLog WHERE date BETWEEN '23-03-2017' AND '30-03-2017'
giving me the correct result.
Note:- tempLog is the table name.
You should store dates in date format or atleast correctly formatted string (YYYY-MM-DD).
For now you can use str_to_date to convert the string to date and compare:
select *
from tempLog
where str_to_date(date, '%d-%m-%Y') between '2017-03-23' and '2017-04-02';
However note that this will hinder the optimizer from using index on the column if any.
The correct remedy of the situation is fixing the table structure.
According to the documentation, you're supposed to use this format when writing a date: 'YYYY-MM-DD' (although it says it may accept 'YYYYMMDD' or even YYYYMMDD in some contexts).
I have a table with a few columns. One of the column is named flight_date is varchar format.
It has a date stored in it in format like '08/12/2015' which is mm/dd/yyyy
I want to run a query which will return me a list of the records with date in 2016.
Can someone help me out please.
Thanks.
In this case, you can use SUBSTR function for just cut last 4 character.
SELECT * FROM yourtable where SUBSTR(flight_date, -4) = '2016'
Though, it is recommended to store date value in appropriate type column and not in varchar.
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')";