SQL截取字段字符串的方法

set @str=‘WX15-53-H-53-99-15-335-23’;


select @str as '字符串'


select len(@str) as '字符长度'


select charindex('-',@str,1) as '第一个逗号的索引值'


select LEFT(@str,charindex('-',@str,1)-1) as '第一个值'


select SUBSTRING(@str,charindex('-',@str,1)+1,len(@str)) as '从第一逗号开始截取出后面的字符串'


select LEFT(SUBSTRING(@str,charindex('-',@str,1)+1,len(@str)),CHARINDEX('-',SUBSTRING(@str,charindex('-',@str,1)+1,len(@str)),1)-1) as '中间的值'


select SUBSTRING(SUBSTRING(@str,charindex('-',@str,1)+1,len(@str)),charindex('-',SUBSTRING(@str,charindex('-',@str,1)+1,len(@str)),1)+1,len(@str)) as '第2个-后面的值’

 

select SUBSTRING(SUBSTRING(@str,charindex('-',@str,2)+1,len(@str)),charindex('-',SUBSTRING(@str,charindex('-',@str,2)+1,len(@str)),2)+1,len(@str)) as '第2个-后面的值’

select SUBSTRING(SUBSTRING(@str,charindex('-',@str,6)+1,len(@str)),charindex('-',SUBSTRING(@str,charindex('-',@str,6)+1,len(@str)),6)+1,len(@str)) as '第5个-后面的值’

以此类推 取出的值为:15-335-23

 

select SUBSTRING('23*2*3',charindex('*','23*2*3')-2,2)

select charindex('*','223*332*3',1)

select LEFT('223*2*3',charindex('*','223*2*3',1)-1)
select LEFT('223*332*3',charindex('*','223*332*3',1)+1,charindex('*','223*332*3',1)-1)

declare @str varchar(50)
set @str='223*3325*3444'

select LEFT(SUBSTRING(@str,charindex('*',@str,1)+1,len(@str)),CHARINDEX('*',SUBSTRING(@str,charindex('*',@str,1)+1,len(@str)),1)-1) as '中间的值'
select SUBSTRING(SUBSTRING(@str,charindex('*',@str,1)+1,len(@str)),charindex('*',SUBSTRING(@str,charindex('*',@str,1)+1,len(@str)),1)+1,len(@str)) as '第2个-后面的值'

multiply
select b.goods,b.one_cat,b.number,b.Goods_unit_price,b.Goods_sum_price,b.dd_size,
convert(decimal(10,2),convert(decimal(10,2),LEFT(b.dd_size,charindex('*',b.dd_size,1)-1))*convert(decimal(10,2),LEFT(SUBSTRING(b.dd_size,charindex('*',b.dd_size,1)+1,len(b.dd_size)),CHARINDEX('*',SUBSTRING(b.dd_size,charindex('*',b.dd_size,1)+1,len(b.dd_size)),1)-1))*convert(decimal(10,2),SUBSTRING(SUBSTRING(b.dd_size,charindex('*',b.dd_size,1)+1,len(b.dd_size)),charindex('*',SUBSTRING(b.dd_size,charindex('*',b.dd_size,1)+1,len(b.dd_size)),1)+1,len(b.dd_size)))),
convert(decimal(10,2),LEFT(b.dd_size,charindex('*',b.dd_size,1)-1))
,convert(decimal(10,2),LEFT(SUBSTRING(b.dd_size,charindex('*',b.dd_size,1)+1,len(b.dd_size)),CHARINDEX('*',SUBSTRING(b.dd_size,charindex('*',b.dd_size,1)+1,len(b.dd_size)),1)-1))
,convert(decimal(10,2),SUBSTRING(SUBSTRING(b.dd_size,charindex('*',b.dd_size,1)+1,len(b.dd_size)),charindex('*',SUBSTRING(b.dd_size,charindex('*',b.dd_size,1)+1,len(b.dd_size)),1)+1,len(b.dd_size)))
from pmw_packInfo p left join pmw_billcode b on p.goodsID=b.goodsID

 

posted @ 2017-07-14 16:32  LuoCore  阅读(1425)  评论(0编辑  收藏  举报