Rich Yarger (6/14/2011)
OK - what if I need to make certain that there isn't just 1 space, but say 2 or 3 or 10 spaces? How would I incorporate LTRIM and RTRIM with your example?
like this
nullif(CASE LEFT(PhoneAlt,1)
WHEN '(' THEN SUBSTRING(PhoneAlt,2,3)
ELSE LTRIM(LEFT(PhoneAlt,3))
END, '') As [Provider!2!SecondaryAreaCode!element]
Remember that string comparisons in sql will trim unless it for comparisons.
Look at the following:
declare @varchar varchar(10), @char char(10)
set @varchar = 'a'
set @char = 'a'
select DATALENGTH(@varchar), DATALENGTH(@Char)
where @varchar = @char
Here you can see that the actual data is not the same but when comparing they are equal. 😉
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/