一、SQL Server中截取字符串常用的函数
1、LEFT ( character_expression , integer_expression )
函数说明:LEFT ( '源字符串' , '要截取最左边的字符数' )
返回从字符串左边开始指定个数的字符
select LEFT('SQL_Server_2008',4 );
返回结果:SQL_
2.RIGHT ( character_expression , integer_expression )
函数说明:RIGHT ( '源字符串' , '要截取最右边的字符数' )
返回字符串中从右边开始指定个数的 integer_expression 字符
select RIGHT('SQL_Server_2008',4 );
返回结果:2008
3.SUBSTRING ( character_expression , start , length )
函数说明:SUBSTRING ( '源字符串' , '截取起始位置(下标从1开始,含该位置上的字符)' , '截取长度' )
返回字符、binary、text 或 image 表达式的一部分
select SUBSTRING('SQL_Server_2008',5 ,6);
返回结果:Server
二、SQL Server字符串类型转换
CONVERT(要转换的类型,指定字段)
三、SQL Server查询结果用指定字符串拼接
SELECT STUFF(
(SELECT ','''+t.our_ref + '''' FROM
(select our_ref from tb_fp_case_info where our_ref like '%HK%'
and len(isnull(old_our_ref,'')) > 0 and len(isnull(our_ref_base,'')) = 0
and len(replace(old_our_ref,' ','') ) = len(old_our_ref) and len(replace(old_our_ref,',','') ) = len(old_our_ref)
and len(replace(old_our_ref,'.','') ) = len(old_our_ref)) t
FOR XML PATH('')),1,1,'') as allref
四、SQL连接查询注意事项
在编写SQL查询语句时,多表连接查询应尽可能的根据需要,选择不同的连接方式进行查询,已提升查询效率。
In关键字,在对比数据较大时,查询效率低,应该考虑使用其他方法(待定)。
ON和WHERE的筛选顺序是由右至左,ON优先于WHERE。
同样两个条件,a and b和b and a效果都不一样。
左连接查询数据量较大时,可以压缩一下坐查询的根表,表经常变动的话,指示查询,可能会受到行锁的等待影响,问题不大。
五、SQLSERVER对于字符串中全半角符号的处理问题
select trademark_clascode,case_type_id,*
from tb_ft_case_info
where trademark_clascode isn not NULL and
(case_type_id != 'ZD or case_type_id is null) and
trademark_clascode COLLATE'COLLATE Chinese_PRC_CAWS like '%,%'order by case_type_id