时间:2021-07-01 10:21:17 帮助过:4人阅读
| 
 
 
 
 
 
 
 
 
 
 
 insert into table1(col1,col2)
 select col1,col2 from table2 where table2.id=1 
 
 //第一个参数是要截取的对象,第二个参数是截取的起始位置(从1开始),第三个参数是截取的长度 select Substring(‘1234567890‘,-1,3) 
 //第一个参数是要查找的字符,第二个参数是查找的对象,字符串的索引是从1开始的 select charindex(‘.‘,‘132.12.3‘) 
 //获取字符串的长度,参数为要查找的对象 len(‘123456‘) 
 //将一个字符串反转 select reverse(‘hello,world‘) 将得到如下的输出:dlrow,olleh 
 //获取最后一次”-“出现的位置 charindex(‘-‘,reverse(@str)) //获取最后一个”-“后面的字符 reverse(substring(reverse(@str),1,charindex(‘-‘,reverse(@str))-1)) 
 //类型转换 CAST(@XX AS char(20)) CONVERT(char(20), @XX) cast(0 as bit) 
 --简单case函数 case sex when ‘1‘ then ‘男‘ when ‘2‘ then ‘女‘ else ‘其他‘ end 
 --case搜索函数 case when sex = ‘1‘ then ‘男‘ when sex = ‘2‘ then ‘女‘ else ‘其他‘ end 
 //申明及使用变量 declare @OldItemPath nvarchar(500); select @OldItemPath=‘123’; //申明及使用表变量 declare @table1 table (Id int); insert into @table1 select Id from table1; select * from @table1; 
 //关键字distinct,去除重复项 select distinct ip,city from table2//关键字distinct,去除重复项 | 
SQL变量、Substring、charindex、case函数、去除重复
标签: