我们如果要在sql server中,使用截取字符串的方法要怎样使用呢?
sql server提供了3个常用截取字符串方法,LEFT()、RIGHT()、SUBSTRING()
/****** Sql Server中截取字符串的常用方法 ******/ --1、LEFT()方法---
--函数说明---
--1)语法:LEFT(character,integer)
--2)介绍:参数1:要截取的字符串,参数2:截取字符个数
--3)使用:
--返回从字符串左边开始指定个数的字符--
select LEFT('SqlServer_2008',3)
--4)返回:Sql --1、RIGHT()方法---
-- right()函数说明---
--1)语法:RIGHT(character,integer)
--2)介绍:参数1:要截取的字符串,参数2:截取字符个数
--3)使用:
--返回从字符串右边开始指定个数的字符--
select LEFT('SqlServer_2008',4)
--4)返回:2008 --1、SUBSTRING()方法---
-- substring()函数说明---
--1)语法:SUBSTRING(character,start,length)
--2)介绍:参数1:要截取的字符串,参数2:开始截取的下标,参数3:截取的字符长度
--3)使用:
--返回从字符串中间的字符--
select SUBSTRING('SqlServer_2008',4,6)
--4)返回:Server
实例如下:
--截取字符串左边3个字符--
select LEFT('Welcome to China!',7) as 结果1
--截取字符串右边4个字符--
select RIGHT('Welcome to China!',6) as 结果2
--截取字符串中间6个字符(第二个从哪个字符下标开始)
select SUBSTRING('Welcome to China!',9,2) as 结果3
实例结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIMAAADECAIAAAAzogXlAAAIw0lEQVR4nO2dQWvyShSG5z+lCu1/EW8380MEocuuhNm5KIJc78YLXbuUOh9dyqW4Kh98SAvaVXdzF5OMMcnMJJo4p+Z9GCRNox3PkzOT2HjCdrvdeDze5fgrIf8rzS9QK8xmwisjdM+vDZcJt4zQPb82PCYchO75tQETVIAJKsAEFU43AeolNgGCw0LvCiAGJqgAE1SACSqw3W73348ldPTqJDahfiAwQYXA+ViOtphoav+siTpNcCELW8Sz7ZIONPn3uVgsFovF/Jjx+KnO6FahZhNCqrgJxS3NbYIx5vixRhNCZLux3W65EHVGtwrN5kQ+GyIuo+gsE8zOaSb2+738UPJDTV52nF+FifRetlqtzPJisdAL39/fQ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QQWYoALmCSqtpdeKE+lGGpigAkxQgYoJZvma6YW7EZAfY4LZMduYLwJ7/9sOEw1WFVLPPPkStv/KB5i4TFUhf1EhmLhIVaESRYVgovGqQvrrBZgn/DRdVUgpVSYpYKLZqkIG70QBEw1WFapUkREmGqwqpFLnE96SQjBBpaoQTFCpKgQTVEJApBtpYIIKMEGFllYVChhxG228av/8Cb8J2njVPnJCKeSEHeQEFQh9Cxg5QaCq0Llla5AT1SnOicua+NE5oWmoWsTroMMYS2oLHGqheCoMICfOwpcTr4NOXDFoOeyy/gw5UUjzVYUKKwYhJ3I0nxMXMYGcUMp/7HSJ0Qk5oZT1fGLWM1O0mbEbO5q6gpyoAZxj28DnTlRATlABOUEF5AQVWlpVKGDEbaBuBxVgggowQQWYoAJMUAEmqAATVIAJKsAEFf4Hr2UQk8ytB14AAAAASUVORK5CYII=" alt="" />
PS:
参考来源地址:http://blog.csdn.net/hu_shengyang/article/details/10536881