SQL截取字段字符串的方法

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


select @str as '字符串'字符串


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


select charindex('-',@str,1) as '第一個逗號的索引值'字符


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


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

相關文章
相關標籤/搜索