Oracle中的字段拼接 CONCAT和 ||

在Oracle中,字符串拼接有两种方法,分别是CONCAT()函数和“||”拼接

1、CONCAT() 函数拼接

缺点:只支持两个字符串的拼接,超过两个会报错(报的错好像是缺失右括号)

//表中的两个字段拼接
select concat(t1.column_1,t1.column_2) from table t1;

注意:使用concat()函数拼接查询的结果是某一列值使用逗号进行隔开再拼接上第二个值的
例子:
select concat(t1.column_1,t1.column_2) from table t1;
假设t1.column_1、t1.column_2的值分是'oracle''mysql',
那么使用concat(t1.column_1,t1.column_2)后查询的结果就是'oracle,mysql',两个字段之间有一个英文的逗号(,//任意一个字段与任意字符串拼接 (time是取的别名,记住:Oracle 取别名不要用as )
select concat('时间是: ',t1.column_2) time from table t1;
select concat(t1.column_1,' 单位:元') time from table t1;

//超过两个字段,会报错(下面这样写会报错)
select concat(t1.column_1,t1.column_2,t1.column_3) from table t1;

在使用CONCAT() 函数进行字符串拼接时,如果拼接的字段(字符串)中有中文,可能会导致乱码,解决方法把拼接的字段(字符串)加上 to_char()即可:

//如果遇到乱码,加上to_char()
select concat(to_char(t1.column_1),to_char(t1.column_2)) time from table t1;
2、使用 “||” 进行字符串的拼接

使用“||”拼接,就不受限制了

//表中两个字符串拼接,取别名为time
select t1.column_1 || t1.column_2 time from table t1;

//表中三个字符串拼接,取别名为time
//这里可以使用括号将几个要拼接的字段括起来,可读性会好点,好像加不加括号都不影响
select (t1.column_1 || t1.column_2 || t1.column_3) time from table t1;

用“||”拼接的好处,在做模糊查询时,可以利用这个

//这样可以动态进行模糊查询,field是动态值
select t1.* from table t1 where t1.name like '%' || field || '%';

//如果对模糊查询更加细粒度,当然,也可以使用concat()进行模糊查询
select t1.* from table t1 where t1.name like concat('%',field);
select t1.* from table t1 where t1.name like concat(field,'%');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值