sql 把字符创分割成两个字符串
使用指定的字符分割字符串。
思路:1,先计算出指定的分割字符在字符串中的索引,
2.然后再分割字符串。
sql代码如下:
View Code
1 declare @strorder nvarchar(200)
2 declare @ismulorder int
3 declare @firstOrder nvarchar(200)
4 declare @secondOrder nvarchar(200)
5
6 set @strorder='LastModifyDate Desc,OurPrice Asc'
7
8 select CHARINDEX(',',@strorder,0)
9 --select LEN(@strorder)
10 --select SUBSTRING(@strorder,0,CHARINDEX(',',@strorder,0))
11 --select SUBSTRING(@strorder,CHARINDEX(',',@strorder,0)+1,LEN(@strorder))
12 set @ismulorder=CHARINDEX(',',@strorder,0)
13 if @ismulorder>0
14 begin
15 set @firstOrder ='p.'+SUBSTRING(@strorder,0,CHARINDEX(',',@strorder,0))
16 set @secondOrder='p.'+SUBSTRING(@strorder,CHARINDEX(',',@strorder,0)+1,LEN(@strorder))
17 set @strorder=@firstOrder+','+@secondOrder
18 end
19
20
21 select @strorder
2 declare @ismulorder int
3 declare @firstOrder nvarchar(200)
4 declare @secondOrder nvarchar(200)
5
6 set @strorder='LastModifyDate Desc,OurPrice Asc'
7
8 select CHARINDEX(',',@strorder,0)
9 --select LEN(@strorder)
10 --select SUBSTRING(@strorder,0,CHARINDEX(',',@strorder,0))
11 --select SUBSTRING(@strorder,CHARINDEX(',',@strorder,0)+1,LEN(@strorder))
12 set @ismulorder=CHARINDEX(',',@strorder,0)
13 if @ismulorder>0
14 begin
15 set @firstOrder ='p.'+SUBSTRING(@strorder,0,CHARINDEX(',',@strorder,0))
16 set @secondOrder='p.'+SUBSTRING(@strorder,CHARINDEX(',',@strorder,0)+1,LEN(@strorder))
17 set @strorder=@firstOrder+','+@secondOrder
18 end
19
20
21 select @strorder
1,关键函数。charindex,substring,len,
额外函数:right,left