summaryrefslogtreecommitdiff
path: root/mysql-test/t/cast.test
blob: fab35bb334a5f6507e7e9a1b050185c2e9a89ab9 (plain)
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
#
# Test of cast function
#

select CAST(1-2 AS UNSIGNED);
select CAST(CAST(1-2 AS UNSIGNED) AS SIGNED INTEGER);
select CONVERT('-1',UNSIGNED);
select cast(-5 as unsigned) | 1, cast(-5 as unsigned) & -1;
select cast(-5 as unsigned) -1, cast(-5 as unsigned) + 1;
select ~5, cast(~5 as signed);
explain extended select ~5, cast(~5 as signed);
select cast(5 as unsigned) -6.0;
select cast(NULL as signed), cast(1/0 as signed); 
select cast(NULL as unsigned), cast(1/0 as unsigned); 
select cast("A" as binary) = "a", cast(BINARY "a" as CHAR) = "A";
select cast("2001-1-1" as DATE), cast("2001-1-1" as DATETIME);
select cast("1:2:3" as TIME);
select CONVERT("2004-01-22 21:45:33",DATE);
select CONVERT(DATE "2004-01-22 21:45:33" USING latin1);
select CONVERT(DATE "2004-01-22 21:45:33",CHAR);
select CONVERT(DATE "2004-01-22 21:45:33",CHAR(4));
select CONVERT(DATE "2004-01-22 21:45:33",CHAR(4) BINARY);
select CAST(DATE "2004-01-22 21:45:33" AS CHAR(4) BINARY);

#
# Character set convertion
#
set names binary;
select cast(_latin1'test' as char character set latin2);
select cast(_koi8r'ÔÅÓÔ' as char character set cp1251);
create table t1 select cast(_koi8r'ÔÅÓÔ' as char character set cp1251) as t;
show create table t1;
drop table t1;

#
# CAST to CHAR with/without length
#
select
  cast(_latin1'ab'  AS char)    as c1,
  cast(_latin1'a '  AS char)    as c2,
  cast(_latin1'abc' AS char(2)) as c3,
  cast(_latin1'a  ' AS char(2)) as c4,
  cast(_latin1'a'   AS char(2)) as c5;

create table t1 select
  cast(_latin1'ab'  AS char)    as c1,
  cast(_latin1'a '  AS char)    as c2,
  cast(_latin1'abc' AS char(2)) as c3,
  cast(_latin1'a  ' AS char(2)) as c4,
  cast(_latin1'a'   AS char(2)) as c5;
select * from t1;
show create table t1;
drop table t1;

#
# CAST to NCHAR with/without length
#
select
  cast(_koi8r'ÆÇ'  AS nchar)    as c1,
  cast(_koi8r'Æ '  AS nchar)    as c2,
  cast(_koi8r'ÆÇÈ' AS nchar(2)) as c3,
  cast(_koi8r'Æ  ' AS nchar(2)) as c4,
  cast(_koi8r'Æ'   AS nchar(2)) as c5;

create table t1 select
  cast(_koi8r'ÆÇ'  AS nchar)    as c1,
  cast(_koi8r'Æ '  AS nchar)    as c2,
  cast(_koi8r'ÆÇÈ' AS nchar(2)) as c3,
  cast(_koi8r'Æ  ' AS nchar(2)) as c4,
  cast(_koi8r'Æ'   AS nchar(2)) as c5;
select * from t1;
show create table t1;
drop table t1;

#
# Bug 2202
# CAST from BINARY to non-BINARY and from non-BINARY to BINARY
#
create table t1 (a binary(10), b char(10) character set koi8r);
insert into t1 values (_binary'ÔÅÓÔ',_binary'ÔÅÓÔ');
select a,b,cast(a as char character set cp1251),cast(b as binary) from t1;
set names koi8r;
select a,b,cast(a as char character set cp1251),cast(b as binary) from t1;
set names cp1251;
select a,b,cast(a as char character set cp1251),cast(b as binary) from t1;
drop table t1;
set names binary;

#
# The following should be fixed in 4.1
#

select cast("2001-1-1" as date) = "2001-01-01";
select cast("2001-1-1" as datetime) = "2001-01-01 00:00:00";
select cast("1:2:3" as TIME) = "1:02:03";
select cast(NULL as DATE);
select cast(NULL as BINARY);