I am working on a TitleCase Function and have found an infuriating issue that I can not figure out.
This
select dbo.lf_titlecase('30600 N. Pima Rd. unit 16','street')
Results in 30600 N Pima Rd Unit 16
But this
select customer_no
,address_no
,street1
,dbo.lf_titlecase(street1,'street') "upd_street1"
from t_address a
where
customer_no = 174242
Spits out 30600 N Pima Rd unit 16
In the first case I am directly copying the string from T_ADDRESS.
This
SELECT DIFFERENCE ((SELECT street1 FROM T_ADDRESS WHERE address_no = 83212), '30600 N. Pima Rd. Lot 16')
Gives me 4 so they should be the same.
I must be missing something!
I've ensured the function is Scalar and should be used in this way. If I change the address first to 30600 N. Pima Rd Unit 16 then I get the correct output. So it seems the . is causing the issue but still that period does not cause a problem when using the function with a hardcoded string.
Here is the main part of the function:
BEGIN
DECLARE
@StrOut NVARCHAR(1024),
@CurrentPosition INT,
@NextSpace INT,
@CurrentWord NVARCHAR(1024),
@StrLen INT,
@LastWord BIT
SET @StrIn = case when @StrIn = '' then null else rtrim(@strIn) end
SET @NextSpace = 1
SET @CurrentPosition = 1
SET @StrOut = ''
SET @StrLen = LEN(@StrIn)
SET @LastWord = 0
WHILE @LastWord = 0
BEGIN
SET @NextSpace = CHARINDEX(' ',isnull(@StrIn,' '), @CurrentPosition+ 1)
IF @NextSpace = 0 -- no more spaces found
BEGIN
SET @NextSpace = @StrLen
SET @LastWord = 1
END
SET @CurrentWord = Upper(SUBSTRING(@StrIn, @CurrentPosition, 1))
SET @CurrentWord = @CurrentWord + Lower(SUBSTRING(@StrIn, @CurrentPosition+1, @NextSpace - @CurrentPosition))
--SET @CurrentWord = case
--when @NextSpace = 0 and len(@CurrentWord) = 1
--then @CurrentWord + SUBSTRING(@StrIn, @CurrentPosition+1, @NextSpace - @CurrentPosition)
--else @CurrentWord + Lower(SUBSTRING(@StrIn, @CurrentPosition+1, @NextSpace - @CurrentPosition))
--end
SET @StrOut = @StrOut + @CurrentWord
SET @CurrentPosition = @NextSpace + 1
END
IF CHARINDEX(',',@StrOut)> 0 and @type = 'city'
BEGIN
SET @StrOut = replace(@strout,',','')
END
T_ADDRESS.street1 is varchar(64)