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';
Related
I want to sort a date-of-birth column in a table which has date stored in format of %m/%d/%Y . When I use order by , the column values are being treated and sorted as string and not date. I have tried UNIX_TIMESTAMP() function but it also seems to be not working. What are the possible solutions ?
Use STR_TO_DATE() Function
SELECT * FROM yourtable
ORDER BY STR_TO_DATE(DueDate,'%m/%d/%Y ') DESC
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'
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 facing a silly problem while converting datetime column into y-m-d format.
I am running following query :
SELECT STR_TO_DATE(dateadded, '%y-%m-%d') FROM my_table
Note : The dateadded column is a type of datetime.
Whenever I am running above query it always shows (NULL).
Can somebody solve this issue as this is irritating me ?
Since your column is of datetime type rather than string type you should use DATE_FORMAT function as below:
SELECT DATE_FORMAT(dateadded, '%y-%m-%d')
FROM my_table
This query will work for a four digit year in table
SELECT STR_TO_DATE(dateadded, '%Y-%m-%d')
FROM my_table
while this will work for a two digit year
SELECT STR_TO_DATE(dateadded, '%y-%m-%d')
FROM my_table
you can try this by following query
SELECT
STR_TO_DATE('99-12-12', '%y-%m-%dd'),
STR_TO_DATE('1999-12-12', '%Y-%m-%dd')
both output will be
1999-12-12
for further details
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.