Question

I want to create a table from qry result. The following qry runs OK:

create table x select 
date_format(TERMINATION_DT,'%Y-%m-%d') as Term_Date
, date_format(BIRTH_DT,'%Y-%m-%d') as DOB
from dashboard_04102014
where status = 'withdrawn';

There are some termination_dt values are empty ''. the where status = 'withdrawn' filters out the empty values. However, when I tried to perform date calc with the following qry, I got an error message:

create table x select 
date_format(TERMINATION_DT,'%Y-%m-%d') as Term_Date
, round(datediff(date_format(TERMINATION_DT,'%Y-%m-%d'), date_format(BIRTH_DT,'%Y-%m-%d'))/365,0) as Age
from dashboard_04102014
where status = 'withdrawn';

Here is the error msg:

incorrect datetime value" '0000-00-00'

I am guessing that those empty termination_dt values were converted to 0000-00-00 that throws off the datediff? But those values are filtered out with the where clause in the first place.

Also, I can run the select part perfectly fine and it returns the results I want. But when I wanted to create a table from it, then I got the error message with the 2nd qry. Why I can create table with the 1st qry but not the 2nd?

Thanks!

ADD Original Table strip:

CREATE TABLE dashboard_04102014
   (`status` varchar(9), `termination_dt` int, `birth_dt` int);

INSERT INTO Table1
(`status`, `termination_dt`, `birth_dt`)
VALUES
('Active', 0, 19560329),
('Withdrawn', 20070208, 19690131),
('Withdrawn', 20090702, 19441219),
('Active', 0, 19520912),
('Withdrawn', 20130730, 19480404);
Était-ce utile?

La solution

If you are not selecting NULL or empty entries from the date columns, then

drop table if exists x;

create table x 
select 
  cast( date_format( BIRTH_DT,'%Y-%m-%d' ) as date ) as BIRTH_DT
  , cast( date_format( TERMINATION_DT,'%Y-%m-%d' ) as date ) as Term_Date
  , cast( round( datediff( date_format( TERMINATION_DT, '%Y-%m-%d' ), 
                     date_format( BIRTH_DT, '%Y-%m-%d' ) 
                 ) / 365, 0 ) as decimal( 6, 2 ) ) as Age
from dashboard_04102014
where 
      status = 'withdrawn'
      -- un comment following line if required
      -- AND ( BIRTH_DT is not null and BIRTH_DT = '' and BIRTH != 0 )
  and ( TERMINATION_DT is not null 
        and TERMINATION_DT != '' 
        and TERMINATION_DT != 0 )
;

Result:

desc x;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| BIRTH_DT  | date         | YES  |     | NULL    |       |
| Term_Date | date         | YES  |     | NULL    |       |
| Age       | decimal(6,2) | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+

select * from x;
+------------+------------+-------+
| BIRTH_DT   | Term_Date  | Age   |
+------------+------------+-------+
| 1992-07-31 | 2050-07-31 | 58.00 |
| 1971-02-24 | 2029-02-24 | 58.00 |
+------------+------------+-------+

Demo 1 @ MySQL 5.5.32 Fiddle
Demo 2 @ MySQL 5.5.32 Fiddle

Autres conseils

i think you should mention the datetime datatype

Illegal DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00', '0000-00-00 00:00:00', or 00000000000000).

https://dev.mysql.com/doc/refman/5.1/en/datetime.html

if your mention datetime means you should mention like:

0000-00-00 00:00:00

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top