如果it's < 143
(行Regist的正常长度),我想在字符串的特定位置添加空格,如果它小于143,则求和并添加该数量的''空格。更新不起作用,当我执行时,它看起来像是影响行,但是当我寻找更改的行的LEN
时,它什么也没做。我正在使用这个:
UPDATE TABLE
SET Regist = (SUBSTRING(Regist,1,(57 - (143 - LEN(Regist)))) +
REPLICATE(' ',(143 - LEN(Regist))) + SUBSTRING(Regist,(58 - (143 - LEN(Regist))),(LEN(Regist) - (58 - (143 - LEN(Regist))))))
WHERE
(LEN(Regist) - 143) < 0
AND Name = 'SQL SERVER V2008'
示例:
[名称]
Excel
-此行的长度为142(包括v2008之后的空格)
[注册]
ABCDEF12345678910111213411121341SQL SERVER V2008 A1111111111111 1111111111111111111111111111111111111111111111111111111111111111111111111
因此,我进行了此更新,以在位置56处添加一个空格(在本例中为143-142)(因为我知道名称在位置57处完成,但行长小于143,则为57-( 143-142)= 56,这就是空间的位置。
答案 0 :(得分:1)
LEN
不能很好地说明这一点。它不算尾随空格。
返回指定的字符串表达式的字符数,不包括结尾的空格。
您可以执行以下操作:
SELECT LEN(Regist + 'x') - 1
然后将计算您的空白。 (请注意,您也应该在更新时进行长度检查。否则,您可能会增加超出预期的空白空间,尤其是多次运行时。)
编辑:
尝试一下:
DECLARE @SEARCHSTRING VARCHAR(100) = 'SQL SERVER V2008'
SELECT REGIST,
LEFT(REGIST,CHARINDEX(@SEARCHSTRING,REGIST,1) + LEN(@SEARCHSTRING) -1) + REPLICATE(' ',143 - LEN(REGIST)) + RIGHT(REGIST,143 - LEN(LEFT(REGIST,CHARINDEX(@SEARCHSTRING,REGIST,1) + LEN(@SEARCHSTRING) -1)) - (143 - LEN(REGIST)))
FROM yourTable
WHERE Name = @SearchString
AND LEN(REGIST) < 143
--UPDATE yourTable
--SET Regist = LEFT(REGIST,CHARINDEX(@SEARCHSTRING,REGIST,1) + LEN(@SEARCHSTRING) -1) + REPLICATE(' ',143 - LEN(REGIST)) + RIGHT(REGIST,143 - LEN(LEFT(REGIST,CHARINDEX(@SEARCHSTRING,REGIST,1) + LEN(@SEARCHSTRING) -1)) - (143 - LEN(REGIST)))
--WHERE Name = @SEARCHSTRING --Or whatever conditions you want
--AND LEN(Regist) < 143
以下是您可以查看发生的故障的详细信息:
DECLARE @REGIST VARCHAR(200) = 'ABCDEF12345678910111213411121341SQL SERVER V2008 A1111111111111 1111111111111111111111111111111111111111111111111111111111111111111111'
DECLARE @REGISTLENGTH INT = LEN(@REGIST)
DECLARE @NUMSPACES INT = 143 - LEN(@REGIST)
DECLARE @SEARCHSTRING VARCHAR(100) = 'SQL SERVER V2008'
DECLARE @LOCATION INT = CHARINDEX(@SEARCHSTRING,@REGIST,1) + LEN(@SEARCHSTRING) -1
DECLARE @LEFTPART VARCHAR(200) = LEFT(@REGIST,@LOCATION)
DECLARE @RIGHTPART VARCHAR(200) = RIGHT(@REGIST,143 - LEN(@LEFTPART) - @NUMSPACES)
DECLARE @NEWREGIST VARCHAR(200) = @LEFTPART + REPLICATE(' ',@NUMSPACES) + @RIGHTPART
DECLARE @NEWREGISTLENGTH INT = LEN(@NEWREGIST)
PRINT 'Original string: ' + @REGIST
PRINT 'Original length: ' + CAST(@REGISTLENGTH AS VARCHAR(10))
PRINT 'Spaces to add: ' + CAST(@NUMSPACES AS VARCHAR(10))
PRINT 'Searchstring: ' + @SEARCHSTRING
PRINT 'Left of searchstring: ' + @LEFTPART
PRINT 'Right of searchstring: ' + @RIGHTPART
PRINT 'New string: ' + @NEWREGIST
PRINT 'New string length: ' + CAST(@NEWREGISTLENGTH AS VARCHAR(10))
如果您使用最后的@NEWREGIST
行并将其分解为几部分,则将得到上面的更新。
此处的主要目标是使选择/更新更具伸缩性。您可以将@SEARCHSTRING
更改为所需的任何内容,这将在该字符串之后添加正确的空格数。
答案 1 :(得分:0)
像这样尝试...
#player_box.isFullscreen