SQL Server 常用系统函数和修改字符约束(collate)的修改

常见函数的使用方法:

1.Convert: 数据类型转化 ,可以格式化日期和数值;

1  select Convert(varchar(5),12345);
2  --'12345'
3  
4  select Convert(varchar(8),getdate(),112)
5  --20161025

 

2.Cast: 比较常用,使用比较方便;

 1 select Cast(int,'12345'); 2 --12345 

3.IsNull:判断表达式是否为NULL,是NULL用指定数值替换;

select IsNull(NULL,10)
--10

select IsNull(20,10)
--20

 

4.Len: 字符串的长度,不计空格数;

select Len('12345   ')
--5

 

5.Substring: 截取表达式的子字符串;

select Substring('abcde',2,3)
--bcd

 

6.Replace:替换指定的字符串

select Replace('abcde','bc','xyz')
--axyzde

 

7.CharIndex:寻找第一个子字符串在表达式中的位置;

select CharIndex('bc',abcde)
--2

 

8.Right: 从右边开始截取字符串个数;

select Right('abcde',3)
--cde

select Left('abcde',3)
--abc

 

9.Left: 从左边开始截取字符串个数;

select Right('abcde',3)
--cde

select Left('abcde',3)
--abc

 

10.Stuff: 用指定的字符串替换指定位置的字符串;

select Stuff('abcde',2,3,'mmmm')
--ammmme

 

修改数据库和表字段的约束 Collation

 1 Alter Database dbName Collate SQL_Latin1_General_SP1_CI_AS

2 Alter table tbName alter column columnName SQL_Latin1_General_SP1_CI_AS 

posted @ 2016-10-25 14:25  Janzen  阅读(1411)  评论(0编辑  收藏  举报