I have the following statement, which returns empty values for 'label':
SELECT *,
IF (
`date_to` IS NULL,
CONCAT(DATE_FORMAT(`date_from`, '%D %M %Y'), ' ', `location`),
CONCAT(DATE_FORMAT(`date_from`, '%D %M %Y'), ' - ', DATE_FORMAT(`date_to`, '%D %M %Y'), ' ', `location`)
) AS `label`
FROM `courses`
ORDER BY `date_from` ASC
The date_from and date_to columns are both DATE types, and the second one might contain a default value of 0000-00-00.
Any idea what might be causing it?
DATE_FORMAT(cast('0000-00-00' as date), '%D %M %Y') returns NULL, therefore the entire CONCAT will also become null.
Perhaps you should also compare date_to to the value '0000-00-00' in the IF condition?
Related
I am trying to SELECT the date from my database in the date_format '%e %M %Y':
SELECT date_format(date, '%e %M %Y') AS date FROM table
The output of the date will be like: 1 january 2016.
When I add an ORDER BY to the query I the date will not sort on the date, but it sorts on alphabetical order.
Does someone know how I can fix this problem and sort column: date on the date order?
Here is my full statement:
SELECT date_format(date, '%e %M %Y') AS date FROM table ORDER BY date DESC
You are using a date as alias for column name. Just use another one:
SELECT date_format(date, '%e %M %Y') AS formatted_date
FROM table
ORDER BY date DESC;
For whatever reason, certain dates return null when using STR_TO_DATE() whereas others don't.
CREATE TABLE persons (
personid int,
name varchar(100),
dob date;
insert into persons values
(5,"Gov","1985-04-23"),
(1,"Gov","1993-04-23"),
(2,"Sam","1991-11-19"),
(3,"A","1993-04-23"),
(4,"B","1991-11-19");
SELECT STR_TO_DATE( CONCAT( MONTH(DOB), DAY(DOB), YEAR(CURDATE()) ), '%m %d %Y') FROM persons;
RETURNS
STR_TO_DATE( CONCAT( MONTH(DOB), DAY(DOB), YEAR(CURDATE()) ), '%m %d %Y')
--------------------------------------------------------------------------
(null)
November, 19 2016 00:00:00
(null)
November, 19 2016 00:00:00
(null)
Please see fiddle:
http://sqlfiddle.com/#!9/a98eef/1/0
Your format string says there's a space after the month and day. But you're not putting any spaces in when you concatenate them. Use CONCAT_WS to specify a separator between the items being concatenated.
STR_TO_DATE( CONCAT_WS(' ', MONTH(DOB), DAY(DOB), YEAR(CURDATE()) ), '%m %d %Y')
corrected fiddle
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.
I have a column 'birthDate' which returns a string value of [Day of the Week], [Month] [Day], [Year]
(e.g. Monday, December 30, 2013)
I am using SELECT STR_TO_DATE(birthDate, '%m/%d/%Y') but it returns a null value.
Any ideas?
SOLUTION
SELECT STR_TO_DATE(birthDate, '%W, %M %d, %Y')
You're showing one format of your date ([Day of the Week], [Month] [Day], [Year]) but using another in STR_TO_DATE ([Month]/[Day]/[Year]). You need to provide STR_TO_DATE with the format your column is currently in:
SELECT STR_TO_DATE(birthDate, '%W %m %d %Y')
If you want that result to be in a new format you can then use DATE_FORMAT():
SELECT DATE_FORMAT(STR_TO_DATE(birthDate, '%W %m %d %Y'), '%m/%d/%Y')
i tried to use this sql:
SELECT date_format(signdate , '%Y-%c-%d %h:%i:%s' ) as post_date ,
date_format(NOW(), '%Y-%c-%d %h:%i:%s' ) as post_date_gmt
FROM `article` where outkey = 'Y'
the result of post_date is null, but post_date is working. so how can i use the funciton date_format to format signdate (ex: signdate is 1095959971)?
DATE_FORMAT expects a DATETIME column.
You'd have to cast it:
DATE_FORMAT(CAST(signdate AS DATETIME), ....)
It would be better to have a real DATETIME field, though.