1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
drop table if exists t1;
create table t1 (t datetime);
insert into t1 values (101);
insert into t1 values (691231);
insert into t1 values (700101);
insert into t1 values (991231);
insert into t1 values (10000101);
insert into t1 values (99991231);
insert into t1 values (101000000);
insert into t1 values (691231000000);
insert into t1 values (700101000000);
insert into t1 values (991231235959);
insert into t1 values (10000101000000);
insert into t1 values (99991231235959);
insert into t1 values (20030100000000);
ERROR HY000: Received an invalid datetime value '20030100000000'.
insert into t1 values (20030000000000);
ERROR HY000: Received an invalid datetime value '20030000000000'.
select * from t1;
t
2000-01-01 00:00:00
2069-12-31 00:00:00
1970-01-01 00:00:00
1999-12-31 00:00:00
1000-01-01 00:00:00
9999-12-31 00:00:00
2000-01-01 00:00:00
2069-12-31 00:00:00
1970-01-01 00:00:00
1999-12-31 23:59:59
1000-01-01 00:00:00
9999-12-31 23:59:59
delete from t1 where t > 0;
ERROR HY000: Received an invalid datetime value '0'.
delete from t1 where t > "0001-01-01 00:00:00";
alter table t1 engine="default";
check table t1;
Table Op Msg_type Msg_text
test.t1 check status OK
delete from t1;
insert into t1 values("000101");
insert into t1 values("691231");
insert into t1 values("700101");
insert into t1 values("991231");
insert into t1 values("00000101");
ERROR HY000: Received an invalid datetime value '00000101'.
insert into t1 values("00010101");
insert into t1 values("99991231");
insert into t1 values("00101000000");
ERROR HY000: Received an invalid datetime value '00101000000'.
insert into t1 values("691231000000");
ERROR HY000: Received an invalid datetime value '691231000000'.
insert into t1 values("700101000000");
ERROR HY000: Received an invalid datetime value '700101000000'.
insert into t1 values("991231235959");
ERROR HY000: Received an invalid datetime value '991231235959'.
insert into t1 values("10000101000000");
insert into t1 values("99991231235959");
insert into t1 values("20030100000000");
ERROR HY000: Received an invalid datetime value '20030100000000'.
insert into t1 values("20030000000000");
ERROR HY000: Received an invalid datetime value '20030000000000'.
insert into t1 values ("2003-003-03");
ERROR HY000: Received an invalid datetime value '2003-003-03'.
insert into t1 values ("20030102T131415");
ERROR HY000: Received an invalid datetime value '20030102T131415'.
insert into t1 values ("2001-01-01T01:01:01");
insert into t1 values ("2001-1-1T1:01:01");
ERROR HY000: Received an invalid datetime value '2001-1-1T1:01:01'.
select * from t1;
t
2000-01-01 00:00:00
2069-12-31 00:00:00
1970-01-01 00:00:00
1999-12-31 00:00:00
0001-01-01 00:00:00
9999-12-31 00:00:00
1000-01-01 00:00:00
9999-12-31 23:59:59
2001-01-01 01:01:01
truncate table t1;
insert into t1 values("2003-0303 12:13:14");
ERROR HY000: Received an invalid datetime value '2003-0303 12:13:14'.
select * from t1;
t
drop table t1;
CREATE TABLE t1 (a datetime not null);
insert into t1 values ("2009-02-11 00:00:00");
select * from t1 where a is null;
a
drop table t1;
create table t1 (id int, dt datetime);
insert into t1 values (1,"2001-08-14 00:00:00"),(2,"2001-08-15 00:00:00"),(3,"2001-08-16 00:00:00"),(4,"2003-09-15 01:20:30");
select * from t1 where dt='2001-08-14 00:00:00' and dt = if(id=1,'2001-08-14 00:00:00','1999-08-15');
id dt
1 2001-08-14 00:00:00
create index dt on t1 (dt);
select * from t1 where dt > 20021020;
id dt
4 2003-09-15 01:20:30
select * from t1 ignore index (dt) where dt > 20021020;
id dt
4 2003-09-15 01:20:30
drop table t1;
CREATE TEMPORARY TABLE `t1` (
`date` datetime,
`numfacture` int NOT NULL default '0',
`expedition` datetime,
PRIMARY KEY (`numfacture`),
KEY `date` (`date`),
KEY `expedition` (`expedition`)
) ENGINE=MyISAM;
INSERT INTO t1 (expedition) VALUES ('0001-01-01 00:00:00');
SELECT * FROM t1 WHERE expedition='0001-01-01 00:00:00';
date numfacture expedition
NULL 0 0001-01-01 00:00:00
INSERT INTO t1 (numfacture,expedition) VALUES ('1212','0001-01-01 00:00:00');
SELECT * FROM t1 WHERE expedition='0001-01-01 00:00:00';
date numfacture expedition
NULL 0 0001-01-01 00:00:00
NULL 1212 0001-01-01 00:00:00
EXPLAIN SELECT * FROM t1 WHERE expedition='0001-01-01 00:00:00';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ref expedition expedition 9 const 1 Using where
drop table t1;
create table t1 (a datetime not null, b datetime not null);
insert into t1 values (now(), now());
insert into t1 values (now(), now());
select * from t1 where a is null or b is null;
a b
drop table t1;
create table t1 (t datetime);
insert into t1 values (20030102030460),(20030102036301),(20030102240401),
(20030132030401),(20031302030401),(100001202030401);
ERROR HY000: Received an invalid datetime value '20030102036301'.
select * from t1;
t
delete from t1;
insert into t1 values
("2003-01-02 03:04:60"),("2003-01-02 03:63:01"),("2003-01-02 24:04:01"),
("2003-01-32 03:04:01"),("2003-13-02 03:04:01"), ("10000-12-02 03:04:00");
ERROR HY000: Received an invalid datetime value '2003-01-02 03:63:01'.
select * from t1;
t
delete from t1;
insert into t1 values ("0000-00-00 00:00:00 some trailer"),("2003-01-01 00:00:00 some trailer");
ERROR HY000: Received an invalid datetime value '0000-00-00 00:00:00 some trailer'.
select * from t1 order by t;
t
drop table t1;
create table t1 (dt datetime);
insert into t1 values ("12-01-01"), ("01-01-01 01:00:00");
insert into t1 values ("00-00-00"), ("00-00-00 00:00:00");
ERROR HY000: Received an invalid datetime value '00-00-00'.
select * from t1;
dt
2012-01-01 00:00:00
2001-01-01 01:00:00
drop table t1;
select cast('2006-12-05 22:10:10' as datetime) + 0;
cast('2006-12-05 22:10:10' as datetime) + 0
20061205221010.000000
CREATE TABLE t1(a DATETIME NOT NULL);
INSERT INTO t1 VALUES ('20060606155555');
SELECT a FROM t1 WHERE a=(SELECT MAX(a) FROM t1) AND (a="20060606155555");
a
2006-06-06 15:55:55
DROP TABLE t1;
SELECT CAST(CAST('2006-08-10' AS DATE) AS DECIMAL(20,6));
CAST(CAST('2006-08-10' AS DATE) AS DECIMAL(20,6))
20060810.000000
SELECT CAST(CAST('2006-08-10 10:11:12' AS DATETIME) AS DECIMAL(20,6));
CAST(CAST('2006-08-10 10:11:12' AS DATETIME) AS DECIMAL(20,6))
20060810101112.000000
SELECT CAST(CAST('2006-08-10 10:11:12' AS DATETIME) + INTERVAL 14 MICROSECOND AS DECIMAL(20,6));
CAST(CAST('2006-08-10 10:11:12' AS DATETIME) + INTERVAL 14 MICROSECOND AS DECIMAL(20,6))
20060810101112.000014
create table t1 (da date default '1962-03-03 23:33:34', dt datetime default '1962-03-03');
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`da` DATE DEFAULT '1962-03-03',
`dt` DATETIME DEFAULT '1962-03-03 00:00:00.000000'
) ENGINE=DEFAULT COLLATE = utf8_general_ci
insert into t1 values ();
insert into t1 values ('2007-03-23 13:49:38','2007-03-23 13:49:38');
insert into t1 values ('2007-03-23 13:49:38','2007-03-23 13:49:38');
insert into t1 set dt='2007-03-23 13:49:38',da=dt;
insert into t1 values ('2007-03-32','2007-03-23 13:49:38');
ERROR HY000: Received an invalid DATE value '2007-03-32'.
select * from t1;
da dt
1962-03-03 1962-03-03 00:00:00
2007-03-23 2007-03-23 13:49:38
2007-03-23 2007-03-23 13:49:38
2007-03-23 2007-03-23 13:49:38
drop table t1;
create table t1 (da date default '1962-03-32 23:33:34', dt datetime default '1962-03-03');
ERROR HY000: Received an invalid datetime value '1962-03-32 23:33:34'.
create table t1 (f1 date, f2 datetime, f3 timestamp);
insert into t1 values('2001-01-01','2001-01-01 01:01:01','2001-01-01 01:01:01');
insert into t1 values('2001-02-05','2001-02-05 00:00:00','2001-02-05 01:01:01');
insert into t1 values('2001-03-10','2001-03-09 01:01:01','2001-03-10 01:01:01');
insert into t1 values('2001-04-15','2001-04-15 00:00:00','2001-04-15 00:00:00');
insert into t1 values('2001-05-20','2001-05-20 01:01:01','2001-05-20 01:01:01');
select f2 from t1 where f2 between '2001-2-5' and '01-04-14';
f2
2001-02-05 00:00:00
2001-03-09 01:01:01
select f1, f2, f3 from t1 where f1 between f2 and f3;
f1 f2 f3
2001-02-05 2001-02-05 00:00:00 2001-02-05 01:01:01
2001-03-10 2001-03-09 01:01:01 2001-03-10 01:01:01
2001-04-15 2001-04-15 00:00:00 2001-04-15 00:00:00
select f1, f2, f3 from t1 where cast(f1 as datetime) between f2 and
cast(f3 as date);
f1 f2 f3
2001-02-05 2001-02-05 00:00:00 2001-02-05 01:01:01
2001-03-10 2001-03-09 01:01:01 2001-03-10 01:01:01
2001-04-15 2001-04-15 00:00:00 2001-04-15 00:00:00
select f2 from t1 where '2001-04-10 12:34:56' between f2 and '01-05-01';
f2
2001-01-01 01:01:01
2001-02-05 00:00:00
2001-03-09 01:01:01
select f2, f3 from t1 where '01-03-10' between f2 and f3;
f2 f3
2001-03-09 01:01:01 2001-03-10 01:01:01
select f2 from t1 where DATE(f2) between "2001-4-15" AND "01-4-15";
f2
2001-04-15 00:00:00
drop table t1;
create table t1 (f1 date);
insert into t1 values('01-01-01'),('01-01-02'),('01-01-03');
select * from t1 where f1 in ('01-01-01','2001-01-02','2001-01-03 00:00:00');
f1
2001-01-01
2001-01-02
2001-01-03
create table t2(f2 datetime);
insert into t2 values('01-01-01 00:00:00'),('01-02-03 12:34:56'),('02-04-06 11:22:33');
select * from t2 where f2 in ('01-01-01','01-02-03 12:34:56','01-02-03');
f2
2001-01-01 00:00:00
2001-02-03 12:34:56
select * from t1,t2 where '01-01-02' in (f1, cast(f2 as date));
f1 f2
2001-01-02 2001-01-01 00:00:00
2001-01-02 2001-02-03 12:34:56
2001-01-02 2002-04-06 11:22:33
select * from t1,t2 where '01-01-01' in (f1, '01-02-03');
f1 f2
2001-01-01 2001-01-01 00:00:00
2001-01-01 2001-02-03 12:34:56
2001-01-01 2002-04-06 11:22:33
select * from t1,t2 where if(1,'01-02-03 12:34:56','') in (f1, f2);
f1 f2
2001-01-01 2001-02-03 12:34:56
2001-01-02 2001-02-03 12:34:56
2001-01-03 2001-02-03 12:34:56
create table t3(f3 varchar(20));
insert into t3 select * from t2;
select * from t2,t3 where f2 in (f3,'03-04-05');
f2 f3
2001-01-01 00:00:00 2001-01-01 00:00:00
2001-02-03 12:34:56 2001-02-03 12:34:56
2002-04-06 11:22:33 2002-04-06 11:22:33
select f1,f2,f3 from t1,t2,t3 where (f1,'1') in ((f2,'1'),(f3,'1'));
f1 f2 f3
2001-01-01 2001-01-01 00:00:00 2001-01-01 00:00:00
2001-01-01 2001-01-01 00:00:00 2001-02-03 12:34:56
2001-01-01 2001-01-01 00:00:00 2002-04-06 11:22:33
2001-01-01 2001-02-03 12:34:56 2001-01-01 00:00:00
2001-01-01 2002-04-06 11:22:33 2001-01-01 00:00:00
select f1 from t1 where ('1',f1) in (('1','01-01-01'),('1','2001-1-1 0:0:0'),('1','02-02-02'));
f1
2001-01-01
drop table t1,t2,t3;
select least(cast('01-01-01' as date), '01-01-02');
least(cast('01-01-01' as date), '01-01-02')
2001-01-01
select greatest(cast('01-01-01' as date), '01-01-02');
greatest(cast('01-01-01' as date), '01-01-02')
01-01-02
select least(cast('01-01-01' as date), '01-01-02') + 0;
least(cast('01-01-01' as date), '01-01-02') + 0
20010101
select greatest(cast('01-01-01' as date), '01-01-02') + 0;
greatest(cast('01-01-01' as date), '01-01-02') + 0
20010102
select least(cast('01-01-01' as datetime), '01-01-02') + 0;
least(cast('01-01-01' as datetime), '01-01-02') + 0
20010101000000
select cast(least(cast('01-01-01' as datetime), '01-01-02') as decimal(16,2));
cast(least(cast('01-01-01' as datetime), '01-01-02') as decimal(16,2))
20010101000000.00
create table t1 (f1 date);
insert into t1 values (curdate());
select left(f1,10) = curdate() from t1;
left(f1,10) = curdate()
1
drop table t1;
create table t1(f1 date);
insert into t1 values('01-01-01'),('02-02-02'),('01-01-01'),('02-02-02');
set @bug28261='';
select if(@bug28261 = f1, '', @bug28261:= f1) from t1;
if(@bug28261 = f1, '', @bug28261:= f1)
2001-01-01
2002-02-02
2001-01-01
2002-02-02
Warnings:
Warning 1292 Incorrect date value: '' for column 'f1' at row 1
select if(@bug28261 = f1, '', @bug28261:= f1) from t1;
if(@bug28261 = f1, '', @bug28261:= f1)
2001-01-01
2002-02-02
2001-01-01
2002-02-02
select if(@bug28261 = f1, '', @bug28261:= f1) from t1;
if(@bug28261 = f1, '', @bug28261:= f1)
2001-01-01
2002-02-02
2001-01-01
2002-02-02
drop table t1;
create table t1(f1 datetime);
insert into t1 values('2001-01-01'),('2002-02-02');
select * from t1 where f1 between 20020101 and 20070101000000;
f1
2002-02-02 00:00:00
select * from t1 where f1 between 2002010 and 20070101000000;
ERROR HY000: Received an invalid datetime value '2002010'.
select * from t1 where f1 between 20020101 and 2007010100000;
ERROR HY000: Received an invalid datetime value '2007010100000'.
drop table t1;
#
# Bug#27216: functions with parameters of different date types may
# return wrong type of the result.
#
create table t1 (f1 date, f2 datetime, f3 varchar(20));
create table t2 as select coalesce(f1,f1) as f4 from t1;
desc t2;
Field Type Null Default Default_is_NULL On_Update
f4 DATE YES YES
create table t3 as select coalesce(f1,f2) as f4 from t1;
desc t3;
Field Type Null Default Default_is_NULL On_Update
f4 DATETIME YES YES
create table t4 as select coalesce(f2,f2) as f4 from t1;
desc t4;
Field Type Null Default Default_is_NULL On_Update
f4 DATETIME YES YES
create table t5 as select coalesce(f1,f3) as f4 from t1;
desc t5;
Field Type Null Default Default_is_NULL On_Update
f4 VARBINARY YES YES
create table t6 as select coalesce(f2,f3) as f4 from t1;
desc t6;
Field Type Null Default Default_is_NULL On_Update
f4 VARBINARY YES YES
create table t7 as select coalesce(makedate(1997,1),f2) as f4 from t1;
desc t7;
Field Type Null Default Default_is_NULL On_Update
f4 DATETIME YES YES
create table t8 as select coalesce(cast('01-01-01' as datetime),f2) as f4
from t1;
desc t8;
Field Type Null Default Default_is_NULL On_Update
f4 DATETIME YES YES
create table t9 as select case when 1 then cast('01-01-01' as date)
when 0 then cast('01-01-01' as date) end as f4 from t1;
desc t9;
Field Type Null Default Default_is_NULL On_Update
f4 DATE YES YES
create table t10 as select case when 1 then cast('01-01-01' as datetime)
when 0 then cast('01-01-01' as datetime) end as f4 from t1;
desc t10;
Field Type Null Default Default_is_NULL On_Update
f4 DATETIME YES YES
create table t11 as select if(1, cast('01-01-01' as datetime),
cast('01-01-01' as date)) as f4 from t1;
desc t11;
Field Type Null Default Default_is_NULL On_Update
f4 DATE YES YES
create table t12 as select least(cast('01-01-01' as datetime),
cast('01-01-01' as date)) as f4 from t1;
desc t12;
Field Type Null Default Default_is_NULL On_Update
f4 DATE YES YES
create table t13 as select ifnull(cast('01-01-01' as datetime),
cast('01-01-01' as date)) as f4 from t1;
desc t13;
Field Type Null Default Default_is_NULL On_Update
f4 DATE YES YES
drop tables t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13;
###################################################################
create table t1 (a int);
insert into t1 values (), (), ();
select sum(a) from t1 group by convert(a, datetime);
sum(a)
NULL
drop table t1;
create table t1 (id int not null, cur_date datetime not null);
create table t2 (id int not null, cur_date date not null);
insert into t1 (id, cur_date) values (1, '2007-04-25 18:30:22');
insert into t2 (id, cur_date) values (1, '2007-04-25');
explain extended
select * from t2
where id in (select id from t2 as x1 where (t2.cur_date is null));
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 1 100.00 Using where
2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
Warnings:
Note 1276 Field or reference 'test.t2.cur_date' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t2`.`id` AS `id`,`test`.`t2`.`cur_date` AS `cur_date` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`id`,<exists>(select 1 AS `Not_used` from `test`.`t2` `x1` where 0))
select * from t2
where id in (select id from t2 as x1 where (t2.cur_date is null));
id cur_date
insert into t1 (id, cur_date) values (2, '2007-04-26 18:30:22');
insert into t2 (id, cur_date) values (2, '2007-04-26');
explain extended
select * from t1
where id in (select id from t1 as x1 where (t1.cur_date is null));
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
Warnings:
Note 1276 Field or reference 'test.t1.cur_date' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`id` AS `id`,`test`.`t1`.`cur_date` AS `cur_date` from `test`.`t1` where <in_optimizer>(`test`.`t1`.`id`,<exists>(select 1 AS `Not_used` from `test`.`t1` `x1` where 0))
select * from t1
where id in (select id from t1 as x1 where (t1.cur_date is null));
id cur_date
explain extended
select * from t2
where id in (select id from t2 as x1 where (t2.cur_date is null));
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
Warnings:
Note 1276 Field or reference 'test.t2.cur_date' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t2`.`id` AS `id`,`test`.`t2`.`cur_date` AS `cur_date` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`id`,<exists>(select 1 AS `Not_used` from `test`.`t2` `x1` where 0))
select * from t2
where id in (select id from t2 as x1 where (t2.cur_date is null));
id cur_date
drop table t1,t2;
End of 5.0 tests
create table t1 (da date default '1962-03-03 23:33:34', dt datetime default '1962-03-03');
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`da` DATE DEFAULT '1962-03-03',
`dt` DATETIME DEFAULT '1962-03-03 00:00:00.000000'
) ENGINE=DEFAULT COLLATE = utf8_general_ci
insert into t1 values ();
insert into t1 values ('2007-03-23 13:49:38','2007-03-23 13:49:38');
insert into t1 values ('2007-03-23 13:49:38','2007-03-23 13:49:38');
insert into t1 set dt='2007-03-23 13:49:38',da=dt;
insert into t1 values ('2007-03-32','2007-03-23 13:49:38');
ERROR HY000: Received an invalid DATE value '2007-03-32'.
select * from t1;
da dt
1962-03-03 1962-03-03 00:00:00
2007-03-23 2007-03-23 13:49:38
2007-03-23 2007-03-23 13:49:38
2007-03-23 2007-03-23 13:49:38
drop table t1;
create table t1 (da date default '1962-03-32 23:33:34', dt datetime default '1962-03-03');
ERROR HY000: Received an invalid datetime value '1962-03-32 23:33:34'.
|