Mar
Mar

Reputation: 1

UPDATE for add spaces in specific places | SQL Server

I am trying to add spaces in specific places of my string if it's < 143(the normal length of a row Regist) , if it's less of 143 then take the difference and add that quantity of ' ' Spaces. The update doesn't work, when I execute, it appears like affect rows but when i look for the LEN of the row that change it doesn't do anything. I am using this:

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'

Example:

[Name]

Excel

--This row has length 142 (including the spaces after v2008)

[Regist]

ABCDEF12345678910111213411121341SQL SERVER V2008 A1111111111111 1111111111111111111111111111111111111111111111111111111111111111111111

So, i did that update to add a space (Just 1 in this case 143 - 142) in the place 56 (because i know that the name finish in place 57 but the row length is less 143, then 57 - (143-142 ) = 56 and that is the position of where is going to be the space.

Upvotes: 0

Views: 1136

Answers (2)

Aaron Dietz
Aaron Dietz

Reputation: 10277

LEN won't be a good tell of this. It doesn't count trailing blank space.

Returns the number of characters of the specified string expression, excluding trailing blanks.

Documentation

You could do something like:

SELECT LEN(Regist + 'x') - 1

Which would then count your blanks. (Note, you should do this for your length checks in your update too. Otherwise you may be adding way more blank space then you intend, especially if you run multiple times.)

Edit:

Try this:

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

Here is a breakdown you can run to see what's happening:

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))

If you take the final @NEWREGIST line and break it's parts back down, you'll be left with the update above.

The main goal here was is to make the select/update more scale-able. You can change @SEARCHSTRING to anything you want and this will add the correct number of spaces after that string.

Upvotes: 1

Jason A. Long
Jason A. Long

Reputation: 4442

Try it like this...

UPDATE yt SET 
    yt.Regist = LEFT(yt.Regist + REPLICATE(' ', 143), 143)
FROM
    dbo.YourTable yt
WHERE 
    yt.name = 'Excel'
    AND LEN(yt.Regist) < 143;

Upvotes: 0

Related Questions