时间:2020-10-24 21:45:47 | 栏目:MsSql | 点击:次
有时候我们会截取字符串中的一些特殊想要的东西,大小写字母、模号、汉字、数字等等,今天先介绍一个获取字符串中小写字母(也是大写字母)的方式,直接上写法:
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
结果如下:
灵活掌握,根据需求应用。
总结