2
2
# Test of cast function
5
select CAST(1-2 AS UNSIGNED);
6
select CAST(CAST(1-2 AS UNSIGNED) AS SIGNED INTEGER);
7
select CAST('10 ' as integer);
8
select cast(-5 as) | 1, cast(-5 as) & -1;
9
select cast(-5 as) -1, cast(-5 as) + 1;
10
select ~5, cast(~5 as signed);
11
explain extended select ~5, cast(~5 as signed);
12
select cast(5 as) -6.0;
13
select cast(NULL as signed), cast(1/0 as signed);
15
6
# Bug #28250: Run-Time Check Failure #3 - The variable 'value' is being used
18
9
# The following line causes Run-Time Check Failure on
19
10
# binaries built with Visual C++ 2005
20
select cast(NULL as), cast(1/0 as);
21
11
select cast("A" as binary) = "a", cast(BINARY "a" as CHAR) = "A";
22
12
select cast("2001-1-1" as DATE), cast("2001-1-1" as DATETIME);
23
13
select cast("1:2:3" as TIME);
29
19
# The following cast creates warnings
31
select CONVERT(DATE "2004-01-22 21:45:33" USING latin1);
32
21
select CONVERT(DATE "2004-01-22 21:45:33",CHAR);
33
22
select CONVERT(DATE "2004-01-22 21:45:33",CHAR(4));
34
23
select CONVERT(DATE "2004-01-22 21:45:33",BINARY(4));
35
24
select CAST(DATE "2004-01-22 21:45:33" AS BINARY(4));
36
select CAST(0xb3 as signed);
37
select CAST(0x8fffffffffffffff as signed);
38
select CAST(0xffffffffffffffff as);
39
select CAST(0xfffffffffffffffe as signed);
40
select cast('-10a' as signed integer);
41
select cast('a10' as integer);
43
26
select 10.0+cast('a' as decimal);
47
select cast('18446744073709551616' as);
48
select cast('18446744073709551616' as signed);
49
select cast('9223372036854775809' as signed);
51
select cast('abc' as signed);
52
select cast('1a' as signed);
53
select cast('' as signed);
56
# Character set conversion
59
select cast(_latin1'test' as char character set latin2);
60
select cast(_koi8r'����' as char character set cp1251);
61
create table t1 select cast(_koi8r'����' as char character set cp1251) as t;
66
30
# CAST to CHAR with/without length
69
cast(_latin1'ab' AS char) as c1,
70
cast(_latin1'a ' AS char) as c2,
71
cast(_latin1'abc' AS char(2)) as c3,
72
cast(_latin1'a ' AS char(2)) as c4,
73
hex(cast(_latin1'a' AS char(2))) as c5;
33
cast('ab' AS char) as c1,
34
cast('a ' AS char) as c2,
35
cast('abc' AS char(2)) as c3,
36
cast('a ' AS char(2)) as c4,
37
hex(cast('a' AS char(2))) as c5;
74
38
select cast(1000 as CHAR(3));
40
# Should throw an error about 'abc' being too large for a char(2)
76
42
create table t1 select
77
cast(_latin1'ab' AS char) as c1,
78
cast(_latin1'a ' AS char) as c2,
79
cast(_latin1'abc' AS char(2)) as c3,
80
cast(_latin1'a ' AS char(2)) as c4,
81
cast(_latin1'a' AS char(2)) as c5;
82
select c1,c2,c3,c4,hex(c5) from t1;
43
cast('ab' AS char) as c1,
44
cast('a ' AS char) as c2,
45
cast('abc' AS char(2)) as c3,
46
cast('a ' AS char(2)) as c4,
47
cast('a' AS char(2)) as c5;
87
50
# CAST to NCHAR with/without length
90
cast(_koi8r'��' AS nchar) as c1,
91
cast(_koi8r'� ' AS nchar) as c2,
92
cast(_koi8r'���' AS nchar(2)) as c3,
93
cast(_koi8r'� ' AS nchar(2)) as c4,
94
cast(_koi8r'�' AS nchar(2)) as c5;
96
create table t1 select
97
cast(_koi8r'��' AS nchar) as c1,
98
cast(_koi8r'� ' AS nchar) as c2,
99
cast(_koi8r'���' AS nchar(2)) as c3,
100
cast(_koi8r'� ' AS nchar(2)) as c4,
101
cast(_koi8r'�' AS nchar(2)) as c5;
103
show create table t1;
108
# CAST from BINARY to non-BINARY and from non-BINARY to BINARY
110
create table t1 (a binary(4), b char(4) character set koi8r);
111
insert into t1 values (_binary'����',_binary'����');
112
select a,b,cast(a as char character set cp1251),cast(b as binary) from t1;
114
select a,b,cast(a as char character set cp1251),cast(b as binary) from t1;
116
select a,b,cast(a as char character set cp1251),cast(b as binary) from t1;
53
cast('��' AS char) as c1,
54
cast('� ' AS char) as c2,
55
cast('���' AS char(2)) as c3,
56
cast('� ' AS char(2)) as c4,
57
cast('�' AS char(2)) as c5;
59
# BUG in drizzletest - can't handle these chars right
60
# # Should throw an error about incorrect
62
#create table t1 select
63
# cast('��' AS char) as c1,
64
# cast('� ' AS char) as c2,
65
# cast('���' AS char(2)) as c3,
66
# cast('� ' AS char(2)) as c4,
67
# cast('�' AS char(2)) as c5;
121
70
# The following should be fixed in 4.1
151
98
select timediff(cast('1 12:00:00' as time), '12:00:00');
154
# Bug #7036: Casting from string to would cap value of result at
155
# maximum signed value instead of maximum value
157
select cast(18446744073709551615 as);
158
select cast(18446744073709551615 as signed);
159
select cast('18446744073709551615' as);
160
select cast('18446744073709551615' as signed);
161
select cast('9223372036854775807' as signed);
163
select cast(concat('184467440','73709551615') as);
164
select cast(concat('184467440','73709551615') as signed);
166
select cast(repeat('1',20) as);
167
select cast(repeat('1',20) as signed);
170
# Bug #13344: cast of large decimal to signed int not handled correctly
172
select cast(1.0e+300 as signed int);
175
# Bugs: #15098: CAST(column double TO signed int), wrong result
177
CREATE TABLE t1 (f1 double);
178
INSERT INTO t1 SET f1 = -1.0e+30 ;
179
INSERT INTO t1 SET f1 = +1.0e+30 ;
180
SELECT f1 AS double_val, CAST(f1 AS SIGNED INT) AS cast_val FROM t1;
184
101
# Bug #23938: cast(NULL as DATE)
187
104
select isnull(date(NULL)), isnull(cast(NULL as DATE));
190
# Bug#23656: Wrong result of CAST from DATE to int
192
SELECT CAST(cast('01-01-01' as date) AS UNSIGNED);
193
SELECT CAST(cast('01-01-01' as date) AS SIGNED);
195
--echo End of 4.1 tests
198
108
#decimal-related additions
199
109
select cast('1.2' as decimal(3,2));
200
110
select 1e18 * cast('1.2' as decimal(3,2));
201
select cast(cast('1.2' as decimal(3,2)) as signed);
203
112
select cast(@v1 as decimal(22, 2));
204
113
select cast(-1e18 as decimal(22,2));