SQL字符串截取(SubString)
作用:返回第一个参数中从第二个参数指定的位置开始、第三个参数指定的长度的子字符串。
有时候我们会截取字符串中的一些特殊想要的东西,大小写字母、模号、汉字、数字等等,今天先介绍一个获取字符串中小写字母(也是大写字母)的方式,直接上写法:
DECLARE @s VARCHAR(MAX)= 'AbcdEf奥奥' --待截取字符串
DECLARE @temp VARCHAR(MAX)='' --临时变量
SELECT @temp=@temp+SUBSTRING(ch, sv.number, 1)
FROM (SELECT @s AS ch) t
CROSS APPLY [master].dbo.spt_values AS sv
WHERE sv.type = 'P'
AND sv.number BETWEEN 1 AND LEN(ch)
AND ASCII(SUBSTRING(ch, sv.number, 1)) BETWEEN ASCII('a') AND ASCII('z')
SELECT @temp
这样我们就获取到了小写字母,当然我们还可以获取大写字母:
SELECT @temp=@temp+SUBSTRING(ch, sv.number, 1)
FROM (SELECT @s AS ch) t
CROSS APPLY [master].dbo.spt_values AS sv
WHERE sv.type = 'P'
AND sv.number BETWEEN 1 AND LEN(ch)
AND ASCII(SUBSTRING(ch, sv.number, 1)) BETWEEN ASCII('A') AND ASCII('Z')
SELECT @temp
https://www.jb51.net/article/140131.htm