Convert City Name to Upper/Lower

In the database, most of our cities are stored in all upper case.  For reporting purposes, I need to have them returned as upper/lower.   I used the below function, which works great for one word cities.   However I cant figure out how to get it to capitalize the 1st letter of each word, for addresses containing multiple names such as Rancho Santa Margarita. 

Upper(left(CR_MEMBER_ALLMEMBERDETAILS.ADDRESSCITY,1))+lower(substring(CR_MEMBER_ALLMEMBERDETAILS.ADDRESSCITY, 2, LEN(CR_MEMBER_ALLMEMBERDETAILS.ADDRESSCITY)))As ADDRESSCITY

This returns back: Rancho santa margarita; I need it to return Rancho Santa Margarita.  Is this possible to do at the query level?


July 7th, 2015 10:53pm

Hi Michelle949,

You can reference the user defined function below written by Itzik Ben-Gan.

CREATE FUNCTION dbo.fn_capitalize
(
@str AS nvarchar(100)
)
RETURNS nvarchar(100)
AS
BEGIN

DECLARE
@ret_str AS varchar(100),
@pos AS int,
@len AS int

SELECT
@ret_str = N' ' + LOWER(@str),
@pos = 1,
@len = LEN(@str) + 1

WHILE @pos > 0 AND @pos < @len
BEGIN
SET @ret_str = STUFF(@ret_str,
@pos + 1,
1,
UPPER(SUBSTRING(@ret_str,@pos + 1, 1)))
SET @pos = CHARINDEX(N' ', @ret_str, @pos + 1)
END

RETURN RIGHT(@ret_str, @len - 1)

END
 
GO

SELECT dbo.fn_capitalize('Rancho santa margarita')

If you have any question, feel free to let me know.
Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 10:14pm

Often it's easiest to do this type of formatting in the reporting software.

If you are using SSRS you can use an expression like =StrConv(Fields!ADDRESSCITY.Value, vbProperCase)

July 8th, 2015 11:32pm

Hi,

Use the below function 

CREATE FUNCTION [dbo].[ConvertToCamelCase]
(@Str varchar(8000))
RETURNS varchar(8000) AS
BEGIN
  DECLARE @Result varchar(2000)
  SET @Str = LOWER(@Str) + ' '
  SET @Result = ''
  WHILE 1=1
  BEGIN
    IF PATINDEX('% %',@Str) = 0 BREAK
    SET @Result = @Result + UPPER(Left(@Str,1))+
    SubString  (@Str,2,CharIndex(' ',@Str)-1)
    SET @Str = SubString(@Str,
      CharIndex(' ',@Str)+1,Len(@Str))
  END
  SET @Result = Left(@Result,Len(@Result))
  RETURN @Result
END  

Please mark as answer if the post solved your problem.

Regards,

Free Windows Admin Tool Kit Click here and download it now
July 9th, 2015 12:27am

Hai,

you can try function

CREATE FUNCTION [dbo].[InitCap] ( @InputString varchar(4000) ) 
RETURNS VARCHAR(4000)
AS
BEGIN

DECLARE @Index          INT
DECLARE @Char           CHAR(1)
DECLARE @PrevChar       CHAR(1)
DECLARE @OutputString   VARCHAR(255)

SET @OutputString = LOWER(@InputString)
SET @Index = 1

WHILE @Index <= LEN(@InputString)
BEGIN
    SET @Char     = SUBSTRING(@InputString, @Index, 1)
    SET @PrevChar = CASE WHEN @Index = 1 THEN ' '
                         ELSE SUBSTRING(@InputString, @Index - 1, 1)
                    END

    IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')
    BEGIN
        IF @PrevChar != '''' OR UPPER(@Char) != 'S'
            SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))
    END

    SET @Index = @Index + 1
END

RETURN @OutputString

END


-- select [dbo].[InitCap] ('TAMIL NADU')

THEN THE OUT PUT IS Tamil Nadu


July 9th, 2015 1:24am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics