Thursday, April 2, 2020

SQL SERVER - How to align string length to fixed size

With left() function is problem because ending spaces are lost. The solution is little trick with space() function.
select top 1 left( itemid, 25 ) + 'x' from inventitemprice
where
itemid = 'S-144-700-1006'

select top 1 left( itemid + space(100), 25 ) + 'x' from inventitemprice
where
itemid = 'S-144-700-1006'
Output:
--------------------------
S-144-700-1006x

(1 row(s) affected)


--------------------------
S-144-700-1006           x

(1 row(s) affected)

No comments:

Post a Comment