Null Values Using STR_TO_DATE() MySQL - mysql

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

Related

mysql: change datetime format when select * from table

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.

Mysql: Change date format

I want to change the date format in mm/yyyy. My query like that.
select shipwynum,
IF (s.adjdeldat != '', s.adjdeldat,s.condeldat) as -- adjdeldat and condeldat are date type
deliverydate,
FROM ship
Result of that query come in yyyymmdd fromat but i want result in mm/yyyy format.
I use DATE_FORMAT like that
DATE_FORMAT(s.adjdeldat,'%m/%Y')
DATE_FORMAT(s.condeldat,'%m/%Y')
but it does not work properly.
From the comments and post:
Data type is varchar for adjdeldat and condeldat and it save data in yyyymmdd or yyyymm
I want to change the date format in mm/yyyy
You mean some data is in 20140415 format and some in 201404 format?
Yes, and i want that data in 04/2014 format
Change your query as below:
select
shipwynum,
if( s.adjdeldat != '',
date_format( str_to_date( s.adjdeldat, '%Y%m%d' ), '%m/%Y' ),
date_format( str_to_date( s.condeldat, '%Y%m%d' ), '%m/%Y' )
) deliverydate,
from ship
Even if some the date values miss dd part, MySQL silently replaces them with 00 when converted from str_to_date.
Example:
select
#dt:=str_to_date( '201404', '%Y%m%d' ) dt,
date_format( #dt, '%m/%Y' ) df;
Result:
+------------+---------+
| dt | df |
+------------+---------+
| 2014-04-00 | 04/2014 |
+------------+---------+
1 row in set (0.00 sec)
try this:
DATE_FORMAT(NOW(),'%m-%d-%Y')
http://www.w3schools.com/sql/func_date_format.asp
your code as like:
SELECT DATE_FORMAT(NOW(), '%M %Y');
SELECT DATE_FORMAT(a.date, '%M/%Y') FROM t1 AS a;
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
sqlfiddle: http://sqlfiddle.com/#!2/6a68f/2

Convert a string to date (special format) in MySQL

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')

Is subquery the solution to this?

Here's the table structure and some sample data:
pID.....month.....year
27 .....3 .....2008
27 .....12 .....2012
31 .....6 .....2008
99 .....1 .....2006
42 .....1 .....2009
pID is the practiceID and month and year represent the date period they've entered data for. I need to grab the number of practices that have entered data for the first time in Oct 2012, Nov 2012, Dec 2012 and so on.
I tried the following query for Oct 2012:
SELECT *
FROM
IPIPKDIS
where
practiceID NOT IN (
SELECT practiceID
from
IPIPKDIS
where
year < 2012 and month < 10
)
and year = 2012
and month = 10
and measureCatRecID = 2
ORDER BY year, month;
but it's grabbing months and year less than 10/2012.
If I run the queries isolated (not as subquery) they both work fine.
Any ideas?
This summary query will yield the first (smallest) date in the table for each value of practiceID.
SELECT practiceID,
MIN(STR_TO_DATE( CONCAT(year, ' ', month), '%Y %m')) first_date
FROM IPIPKDIS
GROUP BY practiceID
If you want to retrieve then the whole row for the first reported month, you'd do a nested query like this:
SELECT *
FROM IPIPKDIS I
JOIN (
SELECT practiceID,
MIN(STR_TO_DATE( CONCAT(year, ' ', month), '%Y %m')) first_date
FROM IPIPKDIS
GROUP BY practiceID
) first ON ( first.practiceID = I.practiceID
AND STR_TO_DATE( CONCAT(I.year, ' ', I.month), '%Y %m') = first.first_date)
The trick to the second query is to use the JOIN to extract just the first-month rows from your table. We use date arithmetic to do the date comparisons.

mySql: convert date from Jan 01, 2000 to 2000-01-01

I am trying to 'normalize' some data from a column that has two date formats, one like "Mon dd, yyyy and another "YYYY-mm-dd". I would like to convert all of the first into the second format, and then change the column type to date.
I imagine it is something like this:
UPDATE table SET
`thedate` = DATE_FORMAT(`thedate`, '%Y-%m-%d')
WHERE `thedate` LIKE '%,%'
but the DATE_FORMAT is the wrong function, I think.
Any ideas?
thanks.
I think I got it.
SELECT date_format( str_to_date( thedate, '%M %d, %Y' ) , '%Y-%m-%d' )
newdate, thedate
FROM donor
WHERE `thedate` LIKE '%,%'