gpt4 book ai didi

sql-server - SQL 服务器 : SUBSTRING position - negative value

转载 作者:行者123 更新时间:2023-12-02 00:57:06 27 4
gpt4 key购买 nike

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)

LiveDemo

输出:Lakhan

关于sql-server - SQL 服务器 : SUBSTRING position - negative value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33462061/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com