I'm working on a database that stores a date column in a human-readable format. This seems to make it tricky to work out rows where a given date is later than.
The format is stored like 20 November, 2018
I'm trying to return all rows where the collection_date is later than 5 November, 2018
I've tried the following query which throws an error.
SELECT *
FROM `orders`
WHERE collection_date >= CONVERT(datetime, '20181105')
This throws the following error message:
Here's my DB info:
UPDATE 1:
I'm trying the following query. None of the previous queries have worked so far, all producing 0 rows:
SELECT *
FROM `orders`
WHERE STR_TO_DATE('collection_date', '%d %M, %Y') >= 2018-11-05
This also does not work
Actually... you have to apply STR_TO_DATE on the collection_date column because that is where the human readable dates are. As for the input date, just specify it in yyyy-mm-dd format:
SELECT *
FROM `orders`
WHERE STR_TO_DATE(collection_date, '%e %M, %Y') >= '2018-11-05'
In MySQL, you want STR_TO_DATE():
SELECT o.*
FROM `orders` o
WHERE collection_date >= Str_To_Date(datetime, '%e %M, %Y');
CONVERT() with this syntax is a SQL Server function.
The value comes first in the convert() function
WHERE collection_date >= CONVERT('2018-11-05', date)
Demo
You can try this using STR_TO_DATE() function
SELECT *
FROM `orders`
WHERE collection_date >= STR_TO_DATE('20181105', '%Y%m%d')
This query worked for me:
SELECT *
FROM orders
WHERE STR_TO_DATE(collection_date, '%d %M, %Y') >= '2018-11-05'
AND collection_time = '4:00 PM'
ORDER BY `orders`.`collection_date` DESC
Related
I need to extract the month from the date in the database but I need to format it to became a word-written month.
Sample Date in the Database
2017-11-22
Month Query
SELECT MONTH(createdAt) FROM tbltest
The output of this is the numerical month of the createdAt ex: 11
Month with Date_Format Query <----- not working
SELECT DATE_FORMAT(MONTH(createdAt), '%Y-%b-%d') as title
FROM tbltest
Output that I am looking for
Nov
SELECT DATE_FORMAT(createdAt, '%b') as monthShortName
FROM tbltest;
Pls refer : https://www.w3resource.com/mysql/date-and-time-functions/mysql-date_format-function.php
You may try MONTHNAME function
SELECT MONTHNAME(createdAt) FROM tbltest
For short month name you can use
select DATE_FORMAT(createdAt, '%b') from tbltest
Original SQL query
SELECT * FROM date WHERE fecha > now() AND feriado != 1 Limit 10
I want to format returned dates to '%d %b %y' using SQL date_format() but i get error #1241 - Operand should contain 1 column(s) when using
SELECT DATE_FORMAT((SELECT * FROM date WHERE fecha > now() AND feriado != 1 Limit 10), '%d %b %y')
Can you give me advice on how to solve the problem? Thank you
You need to put DATE_FORMAT() around specific column names.
SELECT DATE_FORMAT(column_name, '%d %b %y') AS formatted_date
FROM date
WHERE fecha > NOW() and feriado != 1
LIMIT 1
I have a database where it is stored the date of creation of the record. The problem is that when I try to SELECT all records within 1 year, I get no rows returned.
I created a SQLFiddle to illustrate what I have and what I'm trying to do: http://sqlfiddle.com/#!9/33a32b/8
SELECT aux.description, DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS `formatted_date`
FROM aux
WHERE aux.date_creation BETWEEN 'CURDATE() - INTERVAL 1 YEAR' AND 'CURDATE()'
ORDER BY `formatted_date` DESC;
This is your query:
SELECT aux.description, DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS `formatted_date`
FROM aux
WHERE aux.date_creation BETWEEN 'CURDATE() - INTERVAL 1 YEAR' AND 'CURDATE()'
ORDER BY `formatted_date` DESC;
The single quotes are incorrect. You have a string constant, not a date expression. Presumably, date_creation is in the past, so all you need is:
SELECT aux.description,
DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS formatted_date
FROM aux
WHERE aux.date_creation >= CURDATE() - INTERVAL 1 YEAR
ORDER BY aux.date_creation DESC;
Note that I also changed the ORDER BY. Normally, one wants Jan 2nd to follow Jan 1st, not Feb 1st.
I need to query and fetch a row in a table and change the default datetime format 2015-09-15 00:00:00 to simply Sep 02 2015. Any idea how to achieve this?
I tried
SELECT
*
from tablename
where id=0;
SELECT
DATE_FORMAT(date, '%b %d %Y')
FROM tablename.
It returns two tables.
This should work:
select sub.comments_id, sub.comment, date_format(sub.date, '%b %d %Y'), sub.views
from (select comments_id, comment, date, views from tablename where id=0) as sub;
The power of MySQL is in sub selection, take advantage of it.
Use the following query:
select date_format(str_to_date(date, '%Y-%m-%d %H:%i:%s'), '%b %d %Y') from tablename;
str_to_date converts string representation to date and date_format gets you the required formatted string.
Part of my SQL query
DATE_SUB(dStartDate,INTERVAL 30 DAY) AS dEarlyBirdExipiryDate
It return dEarlyBirdExipiryDate as 2013-05-16
But I want it in 16 May, 2013 format.
What changes I should make in my query to get this?
Use DATE_FORMAT() function:
SELECT DATE_FORMAT( DATE_SUB(dStartDate, INTERVAL 30 DAY)
, '%d %M, %Y'
) AS dEarlyBirdExipiryDate;
See this SQLFiddle
You can use something like this. Em not sure but it would work with a few tweaks.
SELECT DATE_FORMAT(DATE_SUB(DATE('2007-11-30 09:00:00'), INTERVAL 1 DAY), '%d %M, %Y'); //