September 9, 2013 at 9:12 am
Hello all.
I need a query that will give me the number of leading spaces in a string. For instance in this string ' =10 02=5608= '
i need to know how many spaces are in the front of it. Since there are spaces in the string in the middle and the end i cannot use count since it will give me the number of all the spaces.
Thank you for your help
September 9, 2013 at 9:21 am
DECLARE @s-2 VARCHAR(20)
SET @s-2 = ' =10 02=5608= '
SELECT LEN(@s) - LEN(LTRIM(@s))
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537September 9, 2013 at 9:24 am
This may seem rather simplistic but you could try D-ECLARE @String char(17)
SET @String = ' =10 02=5608= '
SELECT LEN(@String) - LEN(LTRIM(@String))Perhaps create a simple scalar function to return it for you?
______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience
September 9, 2013 at 7:04 pm
Another option is
SELECT PATINDEX('%[^ ]%',@s) - 1
Not sure if it'll be any quicker, but you could add in tabs into the pattern as well
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply