1
select CAST(1-2 AS UNSIGNED);
4
select CAST(CAST(1-2 AS UNSIGNED) AS SIGNED INTEGER);
5
CAST(CAST(1-2 AS UNSIGNED) AS SIGNED INTEGER)
7
select CAST('10 ' as unsigned integer);
8
CAST('10 ' as unsigned integer)
11
Warning 1292 Truncated incorrect INTEGER value: '10 '
12
select cast(-5 as unsigned) | 1, cast(-5 as unsigned) & -1;
13
cast(-5 as unsigned) | 1 cast(-5 as unsigned) & -1
14
18446744073709551611 18446744073709551611
15
select cast(-5 as unsigned) -1, cast(-5 as unsigned) + 1;
16
cast(-5 as unsigned) -1 cast(-5 as unsigned) + 1
17
18446744073709551610 18446744073709551612
18
select ~5, cast(~5 as signed);
20
18446744073709551610 -6
21
explain extended select ~5, cast(~5 as signed);
22
id select_type table type possible_keys key key_len ref rows filtered Extra
23
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL No tables used
25
Note 1003 select ~(5) AS `~5`,cast(~(5) as signed) AS `cast(~5 as signed)`
26
select cast(5 as unsigned) -6.0;
27
cast(5 as unsigned) -6.0
29
select cast(NULL as signed), cast(1/0 as signed);
30
cast(NULL as signed) cast(1/0 as signed)
32
select cast(NULL as unsigned), cast(1/0 as unsigned);
33
cast(NULL as unsigned) cast(1/0 as unsigned)
35
select cast("A" as binary) = "a", cast(BINARY "a" as CHAR) = "A";
36
cast("A" as binary) = "a" cast(BINARY "a" as CHAR) = "A"
38
select cast("2001-1-1" as DATE), cast("2001-1-1" as DATETIME);
39
cast("2001-1-1" as DATE) cast("2001-1-1" as DATETIME)
40
2001-01-01 2001-01-01 00:00:00
41
select cast("1:2:3" as TIME);
44
select CONVERT("2004-01-22 21:45:33",DATE);
45
CONVERT("2004-01-22 21:45:33",DATE)
56
select CONVERT(DATE "2004-01-22 21:45:33" USING latin1);
57
CONVERT(DATE "2004-01-22 21:45:33" USING latin1)
59
select CONVERT(DATE "2004-01-22 21:45:33",CHAR);
60
CONVERT(DATE "2004-01-22 21:45:33",CHAR)
62
select CONVERT(DATE "2004-01-22 21:45:33",CHAR(4));
63
CONVERT(DATE "2004-01-22 21:45:33",CHAR(4))
66
Warning 1292 Truncated incorrect CHAR(4) value: '2004-01-22 21:45:33'
67
select CONVERT(DATE "2004-01-22 21:45:33",BINARY(4));
68
CONVERT(DATE "2004-01-22 21:45:33",BINARY(4))
71
Warning 1292 Truncated incorrect BINARY(4) value: '2004-01-22 21:45:33'
72
select CAST(DATE "2004-01-22 21:45:33" AS BINARY(4));
73
CAST(DATE "2004-01-22 21:45:33" AS BINARY(4))
76
Warning 1292 Truncated incorrect BINARY(4) value: '2004-01-22 21:45:33'
77
select CAST(0xb3 as signed);
80
select CAST(0x8fffffffffffffff as signed);
81
CAST(0x8fffffffffffffff as signed)
83
select CAST(0xffffffffffffffff as unsigned);
84
CAST(0xffffffffffffffff as unsigned)
86
select CAST(0xfffffffffffffffe as signed);
87
CAST(0xfffffffffffffffe as signed)
89
select cast('-10a' as signed integer);
90
cast('-10a' as signed integer)
93
Warning 1292 Truncated incorrect INTEGER value: '-10a'
94
select cast('a10' as unsigned integer);
95
cast('a10' as unsigned integer)
98
Warning 1292 Truncated incorrect INTEGER value: 'a10'
103
Warning 1292 Truncated incorrect DOUBLE value: 'a'
104
select 10.0+cast('a' as decimal);
105
10.0+cast('a' as decimal)
108
Warning 1292 Truncated incorrect DECIMAL value: 'a'
113
Warning 1292 Truncated incorrect DOUBLE value: 'a'
114
select cast('18446744073709551616' as unsigned);
115
cast('18446744073709551616' as unsigned)
118
Warning 1292 Truncated incorrect INTEGER value: '18446744073709551616'
119
select cast('18446744073709551616' as signed);
120
cast('18446744073709551616' as signed)
123
Warning 1292 Truncated incorrect INTEGER value: '18446744073709551616'
124
select cast('9223372036854775809' as signed);
125
cast('9223372036854775809' as signed)
128
Warning 1105 Cast to signed converted positive out-of-range integer to it's negative complement
129
select cast('-1' as unsigned);
130
cast('-1' as unsigned)
133
Warning 1105 Cast to unsigned converted negative integer to it's positive complement
134
select cast('abc' as signed);
135
cast('abc' as signed)
138
Warning 1292 Truncated incorrect INTEGER value: 'abc'
139
select cast('1a' as signed);
143
Warning 1292 Truncated incorrect INTEGER value: '1a'
144
select cast('' as signed);
148
Warning 1292 Truncated incorrect INTEGER value: ''
150
select cast(_latin1'test' as char character set latin2);
151
cast(_latin1'test' as char character set latin2)
153
select cast(_koi8r'����' as char character set cp1251);
154
cast(_koi8r'����' as char character set cp1251)
156
create table t1 select cast(_koi8r'����' as char character set cp1251) as t;
157
show create table t1;
159
t1 CREATE TABLE `t1` (
160
`t` varchar(4) CHARACTER SET cp1251 NOT NULL DEFAULT ''
161
) ENGINE=MyISAM DEFAULT CHARSET=latin1
164
cast(_latin1'ab' AS char) as c1,
165
cast(_latin1'a ' AS char) as c2,
166
cast(_latin1'abc' AS char(2)) as c3,
167
cast(_latin1'a ' AS char(2)) as c4,
168
hex(cast(_latin1'a' AS char(2))) as c5;
172
Warning 1292 Truncated incorrect BINARY(2) value: 'abc'
173
Warning 1292 Truncated incorrect BINARY(2) value: 'a '
174
select cast(1000 as CHAR(3));
175
cast(1000 as CHAR(3))
178
Warning 1292 Truncated incorrect BINARY(3) value: '1000'
179
create table t1 select
180
cast(_latin1'ab' AS char) as c1,
181
cast(_latin1'a ' AS char) as c2,
182
cast(_latin1'abc' AS char(2)) as c3,
183
cast(_latin1'a ' AS char(2)) as c4,
184
cast(_latin1'a' AS char(2)) as c5;
186
Warning 1292 Truncated incorrect BINARY(2) value: 'abc'
187
Warning 1292 Truncated incorrect BINARY(2) value: 'a '
188
select c1,c2,c3,c4,hex(c5) from t1;
191
show create table t1;
193
t1 CREATE TABLE `t1` (
194
`c1` varbinary(2) NOT NULL DEFAULT '',
195
`c2` varbinary(2) NOT NULL DEFAULT '',
196
`c3` varbinary(2) NOT NULL DEFAULT '',
197
`c4` varbinary(2) NOT NULL DEFAULT '',
198
`c5` varbinary(2) NOT NULL DEFAULT ''
199
) ENGINE=MyISAM DEFAULT CHARSET=latin1
202
cast(_koi8r'��' AS nchar) as c1,
203
cast(_koi8r'� ' AS nchar) as c2,
204
cast(_koi8r'���' AS nchar(2)) as c3,
205
cast(_koi8r'� ' AS nchar(2)) as c4,
206
cast(_koi8r'�' AS nchar(2)) as c5;
210
Warning 1292 Truncated incorrect CHAR(4) value: 'фгх'
211
Warning 1292 Truncated incorrect CHAR(3) value: 'ф '
212
create table t1 select
213
cast(_koi8r'��' AS nchar) as c1,
214
cast(_koi8r'� ' AS nchar) as c2,
215
cast(_koi8r'���' AS nchar(2)) as c3,
216
cast(_koi8r'� ' AS nchar(2)) as c4,
217
cast(_koi8r'�' AS nchar(2)) as c5;
219
Warning 1292 Truncated incorrect CHAR(4) value: 'фгх'
220
Warning 1292 Truncated incorrect CHAR(3) value: 'ф '
224
show create table t1;
226
t1 CREATE TABLE `t1` (
227
`c1` varchar(2) CHARACTER SET utf8 NOT NULL DEFAULT '',
228
`c2` varchar(2) CHARACTER SET utf8 NOT NULL DEFAULT '',
229
`c3` varchar(2) CHARACTER SET utf8 NOT NULL DEFAULT '',
230
`c4` varchar(2) CHARACTER SET utf8 NOT NULL DEFAULT '',
231
`c5` varchar(2) CHARACTER SET utf8 NOT NULL DEFAULT ''
232
) ENGINE=MyISAM DEFAULT CHARSET=latin1
234
create table t1 (a binary(4), b char(4) character set koi8r);
235
insert into t1 values (_binary'����',_binary'����');
236
select a,b,cast(a as char character set cp1251),cast(b as binary) from t1;
237
a b cast(a as char character set cp1251) cast(b as binary)
240
select a,b,cast(a as char character set cp1251),cast(b as binary) from t1;
241
a b cast(a as char character set cp1251) cast(b as binary)
244
select a,b,cast(a as char character set cp1251),cast(b as binary) from t1;
245
a b cast(a as char character set cp1251) cast(b as binary)
249
select cast("2001-1-1" as date) = "2001-01-01";
250
cast("2001-1-1" as date) = "2001-01-01"
252
select cast("2001-1-1" as datetime) = "2001-01-01 00:00:00";
253
cast("2001-1-1" as datetime) = "2001-01-01 00:00:00"
255
select cast("1:2:3" as TIME) = "1:02:03";
256
cast("1:2:3" as TIME) = "1:02:03"
258
select cast(NULL as DATE);
261
select cast(NULL as BINARY);
264
CREATE TABLE t1 (a enum ('aac','aab','aaa') not null);
265
INSERT INTO t1 VALUES ('aaa'),('aab'),('aac');
266
SELECT a, CAST(a AS CHAR) FROM t1 ORDER BY CAST(a AS UNSIGNED) ;
271
SELECT a, CAST(a AS CHAR(3)) FROM t1 ORDER BY CAST(a AS CHAR(2)), a;
277
Warning 1292 Truncated incorrect BINARY(2) value: 'aaa'
278
Warning 1292 Truncated incorrect BINARY(2) value: 'aab'
279
Warning 1292 Truncated incorrect BINARY(2) value: 'aac'
280
SELECT a, CAST(a AS UNSIGNED) FROM t1 ORDER BY CAST(a AS CHAR) ;
281
a CAST(a AS UNSIGNED)
285
SELECT a, CAST(a AS CHAR(2)) FROM t1 ORDER BY CAST(a AS CHAR(3)), a;
291
Warning 1292 Truncated incorrect BINARY(2) value: 'aaa'
292
Warning 1292 Truncated incorrect BINARY(2) value: 'aab'
293
Warning 1292 Truncated incorrect BINARY(2) value: 'aac'
295
select date_add(cast('2004-12-30 12:00:00' as date), interval 0 hour);
296
date_add(cast('2004-12-30 12:00:00' as date), interval 0 hour)
298
select timediff(cast('2004-12-30 12:00:00' as time), '12:00:00');
299
timediff(cast('2004-12-30 12:00:00' as time), '12:00:00')
301
select timediff(cast('1 12:00:00' as time), '12:00:00');
302
timediff(cast('1 12:00:00' as time), '12:00:00')
304
select cast(18446744073709551615 as unsigned);
305
cast(18446744073709551615 as unsigned)
307
select cast(18446744073709551615 as signed);
308
cast(18446744073709551615 as signed)
310
select cast('18446744073709551615' as unsigned);
311
cast('18446744073709551615' as unsigned)
313
select cast('18446744073709551615' as signed);
314
cast('18446744073709551615' as signed)
317
Warning 1105 Cast to signed converted positive out-of-range integer to it's negative complement
318
select cast('9223372036854775807' as signed);
319
cast('9223372036854775807' as signed)
321
select cast(concat('184467440','73709551615') as unsigned);
322
cast(concat('184467440','73709551615') as unsigned)
324
select cast(concat('184467440','73709551615') as signed);
325
cast(concat('184467440','73709551615') as signed)
328
Warning 1105 Cast to signed converted positive out-of-range integer to it's negative complement
329
select cast(repeat('1',20) as unsigned);
330
cast(repeat('1',20) as unsigned)
332
select cast(repeat('1',20) as signed);
333
cast(repeat('1',20) as signed)
336
Warning 1105 Cast to signed converted positive out-of-range integer to it's negative complement
337
select cast(1.0e+300 as signed int);
338
cast(1.0e+300 as signed int)
340
CREATE TABLE t1 (f1 double);
341
INSERT INTO t1 SET f1 = -1.0e+30 ;
342
INSERT INTO t1 SET f1 = +1.0e+30 ;
343
SELECT f1 AS double_val, CAST(f1 AS SIGNED INT) AS cast_val FROM t1;
345
-1e30 -9223372036854775808
346
1e30 9223372036854775807
348
Warning 1292 Truncated incorrect INTEGER value: '-1e30'
349
Warning 1292 Truncated incorrect INTEGER value: '1e30'
351
select isnull(date(NULL)), isnull(cast(NULL as DATE));
352
isnull(date(NULL)) isnull(cast(NULL as DATE))
354
SELECT CAST(cast('01-01-01' as date) AS UNSIGNED);
355
CAST(cast('01-01-01' as date) AS UNSIGNED)
357
SELECT CAST(cast('01-01-01' as date) AS SIGNED);
358
CAST(cast('01-01-01' as date) AS SIGNED)
361
select cast('1.2' as decimal(3,2));
362
cast('1.2' as decimal(3,2))
364
select 1e18 * cast('1.2' as decimal(3,2));
365
1e18 * cast('1.2' as decimal(3,2))
367
select cast(cast('1.2' as decimal(3,2)) as signed);
368
cast(cast('1.2' as decimal(3,2)) as signed)
371
select cast(@v1 as decimal(22, 2));
372
cast(@v1 as decimal(22, 2))
373
1000000000000000000.00
374
select cast(-1e18 as decimal(22,2));
375
cast(-1e18 as decimal(22,2))
376
-1000000000000000000.00
377
create table t1(s1 time);
378
insert into t1 values ('11:11:11');
379
select cast(s1 as decimal(7,2)) from t1;
380
cast(s1 as decimal(7,2))
383
Error 1264 Out of range value for column 'cast(s1 as decimal(7,2))' at row 1
385
CREATE TABLE t1 (v varchar(10), tt tinytext, t text,
386
mt mediumtext, lt longtext);
387
INSERT INTO t1 VALUES ('1.01', '2.02', '3.03', '4.04', '5.05');
388
SELECT CAST(v AS DECIMAL), CAST(tt AS DECIMAL), CAST(t AS DECIMAL),
389
CAST(mt AS DECIMAL), CAST(lt AS DECIMAL) from t1;
390
CAST(v AS DECIMAL) CAST(tt AS DECIMAL) CAST(t AS DECIMAL) CAST(mt AS DECIMAL) CAST(lt AS DECIMAL)
393
select cast(NULL as decimal(6)) as t1;
397
select hex(cast('a' as char(2) binary));
398
hex(cast('a' as char(2) binary))
400
select hex(cast('a' as binary(2)));
401
hex(cast('a' as binary(2)))
403
select hex(cast('a' as char(2) binary));
404
hex(cast('a' as char(2) binary))
406
CREATE TABLE t1 (d1 datetime);
407
INSERT INTO t1(d1) VALUES ('2007-07-19 08:30:00'), (NULL),
408
('2007-07-19 08:34:00'), (NULL), ('2007-07-19 08:36:00');
409
SELECT cast(date(d1) as signed) FROM t1;
410
cast(date(d1) as signed)
417
CREATE TABLE t1 (f1 DATE);
418
INSERT INTO t1 VALUES ('2007-07-19'), (NULL);
422
HOUR(f1) MINUTE(f1) SECOND(f1)
425
SELECT HOUR(CAST('2007-07-19' AS DATE)),
426
MINUTE(CAST('2007-07-19' AS DATE)),
427
SECOND(CAST('2007-07-19' AS DATE));
428
HOUR(CAST('2007-07-19' AS DATE)) MINUTE(CAST('2007-07-19' AS DATE)) SECOND(CAST('2007-07-19' AS DATE))
430
SELECT HOUR(CAST(NULL AS DATE)),
431
MINUTE(CAST(NULL AS DATE)),
432
SECOND(CAST(NULL AS DATE));
433
HOUR(CAST(NULL AS DATE)) MINUTE(CAST(NULL AS DATE)) SECOND(CAST(NULL AS DATE))
438
HOUR(NULL) MINUTE(NULL) SECOND(NULL)