SQL 服务器:SUBSTRING 位置 - 负值
SQL Server : SUBSTRING position - negative value
在 substring
上提供负值是什么意思?
以下代码的输出是什么?
Declare @Strings VARCHAR(20)
Select @Strings ='Lakhan Pal Garg'
Select SUBSTRING(@Strings, -9, 16)
值Lakhan是如何得到答案的?
来自SUBSTRING
:
start
Is an integer or bigint expression that specifies where the returned characters start. If start is less than 1, the returned
expression will begin at the first character that is specified in
expression. In this case, the number of characters that are returned
is the largest value of either the sum of start + length- 1 or 0. If
start is greater than the number of characters in the value
expression, a zero-length expression is returned.
length
Is a positive integer or bigint expression that specifies how many characters of the expression will be returned. If length is negative,
an error is generated and the statement is terminated. If the sum of
start and length is greater than the number of characters in
expression, the whole value expression beginning at start is returned.
Declare @Strings VARCHAR(20)
Select @Strings ='Lakhan Pal Garg'
Select SUBSTRING(@Strings,-9,16)
从开头获取的字符数MAX(-9 + 16 - 1, 0) = 6
所以你的查询是一样的:
Select SUBSTRING(@Strings,1,6)
输出:Lakhan
在 substring
上提供负值是什么意思?
以下代码的输出是什么?
Declare @Strings VARCHAR(20)
Select @Strings ='Lakhan Pal Garg'
Select SUBSTRING(@Strings, -9, 16)
值Lakhan是如何得到答案的?
来自SUBSTRING
:
start
Is an integer or bigint expression that specifies where the returned characters start. If start is less than 1, the returned expression will begin at the first character that is specified in expression. In this case, the number of characters that are returned is the largest value of either the sum of start + length- 1 or 0. If start is greater than the number of characters in the value expression, a zero-length expression is returned.
length
Is a positive integer or bigint expression that specifies how many characters of the expression will be returned. If length is negative, an error is generated and the statement is terminated. If the sum of start and length is greater than the number of characters in expression, the whole value expression beginning at start is returned.
Declare @Strings VARCHAR(20)
Select @Strings ='Lakhan Pal Garg'
Select SUBSTRING(@Strings,-9,16)
从开头获取的字符数MAX(-9 + 16 - 1, 0) = 6
所以你的查询是一样的:
Select SUBSTRING(@Strings,1,6)
输出:Lakhan