cheryl9063
asked on
T-SQL Function for string UpperCase
Someone created the function below but it need to do one more thing.. When the @input is the word O'Hara Mirror it changes it to O'hara Mirror.. It's not recognizing the ' as a space I guess.. How to fix that using the code below? Can you see in this function where it is doing that?
Purpose: To convert a given string to proper case
Written by: Tim Coggins
Date modified: Sept-12-2008
Examples:
To convert the string 'william h gates' to proper case:
SELECT dbo.PROPERCASE('william h gates')
*************************************************************************************************/
ALTER FUNCTION [dbo].[PROPERCASE]
(
--The string to be converted to proper case
@input varchar(8000)
)
--This function returns the proper case string of varchar type
RETURNS varchar(8000)
AS
BEGIN
IF @input IS NULL
BEGIN
--Just return NULL if input string is NULL
RETURN NULL
END
--Character variable declarations
DECLARE @output varchar(8000)
--Integer variable declarations
DECLARE @ctr int, @len int, @found_at int
--Constant declarations
DECLARE @LOWER_CASE_a int, @LOWER_CASE_z int, @Delimiter char(3), @UPPER_CASE_A int, @UPPER_CASE_Z int
--Variable/Constant initializations
SET @ctr = 1
SET @len = LEN(@input)
SET @output = ''
SET @LOWER_CASE_a = 97
SET @LOWER_CASE_z = 122
SET @Delimiter = ' ,-'
SET @UPPER_CASE_A = 65
SET @UPPER_CASE_Z = 90
WHILE @ctr <= @len
BEGIN
--This loop will take care of reccuring white spaces
WHILE CHARINDEX(SUBSTRING(@input,@ctr,1), @Delimiter) > 0
BEGIN
SET @output = @output + SUBSTRING(@input,@ctr,1)
SET @ctr = @ctr + 1
END
IF ASCII(SUBSTRING(@input,@ctr,1)) BETWEEN @LOWER_CASE_a AND @LOWER_CASE_z
BEGIN
--Converting the first character to upper case
SET @output = @output + UPPER(SUBSTRING(@input,@ctr,1))
END
ELSE
BEGIN
SET @output = @output + SUBSTRING(@input,@ctr,1)
END
SET @ctr = @ctr + 1
WHILE CHARINDEX(SUBSTRING(@input,@ctr,1), @Delimiter) = 0 AND (@ctr <= @len)
BEGIN
IF ASCII(SUBSTRING(@input,@ctr,1)) BETWEEN @UPPER_CASE_A AND @UPPER_CASE_Z
BEGIN
--added to handle exception.... A = 65, V = 86, L = 76, T = 84, C = 67, I = 73
if ascII(substring(@input,@ctr,1)) = 65 or ascII(substring(@input,@ctr,1)) = 86
begin
if ascII(substring(@input,@ctr - 1 ,1)) = 76 and substring(@input,@ctr + 1 ,1) = ' ' and substring(@input,@ctr -2 ,1) = ' ' or
ascII(substring(@input,@ctr - 1 ,1)) = 84 and substring(@input,@ctr + 1 ,1) = ' ' or
ascII(substring(@input,@ctr - 2 ,1)) <> 73 and ascII(substring(@input,@ctr - 1 ,1)) = 67 and substring(@input,@ctr + 1 ,1) = ' '
begin
SET @output = @output + UPPER(SUBSTRING(@input,@ctr,1))
end
Else
begin
SET @output = @output + LOWER(SUBSTRING(@input,@ctr,1))
end
end
else
SET @output = @output + LOWER(SUBSTRING(@input,@ctr,1))
END
ELSE
BEGIN
SET @output = @output + SUBSTRING(@input,@ctr,1)
END
SET @ctr = @ctr + 1
END
END
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I made the two changes
@Delimiter char(4)
SET @Delimiter = ' ,-''"'
I'm getting this error message
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string 'Hara Mirror)
'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'Hara Mirror)
'.
When I put in SELECT dbo.PROPERCASE(O'Hara Mirror)
or when I put in
SELECT dbo.PROPERCASE('O'Hara Mirror')
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'Hara'.
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string ')
'.
You need to escape the ' character in O'Hara Mirror.
SELECT dbo.PROPERCASE('O''Hara Mirror')
SELECT dbo.PROPERCASE('O''Hara Mirror')
I think the function suggested in the link below will handle this better.
http://blog.sqlauthority.com/2007/02/01/sql-server-udf-function-to-convert-text-string-to-title-case-proper-case/
http://blog.sqlauthority.com/2007/02/01/sql-server-udf-function-to-convert-text-string-to-title-case-proper-case/
ASKER
I love you guys.. imnorie works great and ralmada I'm sure patels works great as well.. One more and I can put this in a new question if I need to.. What about a /? What is the data is O/hara Mirror
ASKER
I would want O/Harra Mirror
Just change the declaration to use 5 instead of 4 and add / to the delimiter's string.
By the way, if you are going to add any more characters it might be worth looking at the function in the link ralmada posted.
I only had a brief look but I think it uses an array for the delimiters, which would be easier to change.
By the way, if you are going to add any more characters it might be worth looking at the function in the link ralmada posted.
I only had a brief look but I think it uses an array for the delimiters, which would be easier to change.
ASKER
I thought that is what I needed to do but it didnt work.. See the h is not cap
ALTER FUNCTION [dbo].[PROPERCASE]
(
--The string to be converted to proper case
@input varchar(8000)
)
--This function returns the proper case string of varchar type
RETURNS varchar(8000)
AS
BEGIN
IF @input IS NULL
BEGIN
--Just return NULL if input string is NULL
RETURN NULL
END
--Character variable declarations
DECLARE @output varchar(8000)
--Integer variable declarations
DECLARE @ctr int, @len int, @found_at int
--Constant declarations
DECLARE @LOWER_CASE_a int, @LOWER_CASE_z int, @Delimiter char(5), @UPPER_CASE_A int, @UPPER_CASE_Z int
--Variable/Constant initializations
SET @ctr = 1
SET @len = LEN(@input)
SET @output = ''
SET @LOWER_CASE_a = 97
SET @LOWER_CASE_z = 122
--SET @Delimiter = ' ,-'
SET @Delimiter = ' ,-''"/'
SET @UPPER_CASE_A = 65
SET @UPPER_CASE_Z = 90
WHILE @ctr <= @len
BEGIN
--This loop will take care of reccuring white spaces
WHILE CHARINDEX(SUBSTRING(@input ,@ctr,1), @Delimiter) > 0
BEGIN
SET @output = @output + SUBSTRING(@input,@ctr,1)
SET @ctr = @ctr + 1
END
IF ASCII(SUBSTRING(@input,@ct r,1)) BETWEEN @LOWER_CASE_a AND @LOWER_CASE_z
BEGIN
--Converting the first character to upper case
SET @output = @output + UPPER(SUBSTRING(@input,@ct r,1))
END
ELSE
BEGIN
SET @output = @output + SUBSTRING(@input,@ctr,1)
END
SET @ctr = @ctr + 1
WHILE CHARINDEX(SUBSTRING(@input ,@ctr,1), @Delimiter) = 0 AND (@ctr <= @len)
BEGIN
IF ASCII(SUBSTRING(@input,@ct r,1)) BETWEEN @UPPER_CASE_A AND @UPPER_CASE_Z
BEGIN
--added to handle exception.... A = 65, V = 86, L = 76, T = 84, C = 67, I = 73
if ascII(substring(@input,@ct r,1)) = 65 or ascII(substring(@input,@ct r,1)) = 86
begin
if ascII(substring(@input,@ct r - 1 ,1)) = 76 and substring(@input,@ctr + 1 ,1) = ' ' and substring(@input,@ctr -2 ,1) = ' ' or
ascII(substring(@input,@ct r - 1 ,1)) = 84 and substring(@input,@ctr + 1 ,1) = ' ' or
ascII(substring(@input,@ct r - 2 ,1)) <> 73 and ascII(substring(@input,@ct r - 1 ,1)) = 67 and substring(@input,@ctr + 1 ,1) = ' '
begin
SET @output = @output + UPPER(SUBSTRING(@input,@ct r,1))
end
Else
begin
SET @output = @output + LOWER(SUBSTRING(@input,@ct r,1))
end
end
else
SET @output = @output + LOWER(SUBSTRING(@input,@ct r,1))
END
ELSE
BEGIN
SET @output = @output + SUBSTRING(@input,@ctr,1)
END
SET @ctr = @ctr + 1
END
END
if @output = 'mar-a-lago'
begin
set @output = 'Mar-a-Lago'
end
if @output = 'LA Bella Chair'
begin
set @output = 'La Bella Chair'
end
if @output = 'LA Bella Love Seat'
begin
set @output = 'La Bella Love Seat'
end
if @output = 'LA Bella Ottoman'
begin
set @output = 'La Bella Ottoman'
end
if @output = 'LA Bella Sofa'
begin
set @output = 'La Bella Sofa'
end
if @output = 'LA Mancha Chair'
begin
set @output = 'La Mancha Chair'
end
if @output = 'LA Palma Chair'
begin
set @output = 'La Palma Chair'
end
if @output = 'BocA End Table'
begin
set @output = 'Boca End Table'
end
RETURN @output
END
--SELECT dbo.PROPERCASE('O/hara Mirror')
ALTER FUNCTION [dbo].[PROPERCASE]
(
--The string to be converted to proper case
@input varchar(8000)
)
--This function returns the proper case string of varchar type
RETURNS varchar(8000)
AS
BEGIN
IF @input IS NULL
BEGIN
--Just return NULL if input string is NULL
RETURN NULL
END
--Character variable declarations
DECLARE @output varchar(8000)
--Integer variable declarations
DECLARE @ctr int, @len int, @found_at int
--Constant declarations
DECLARE @LOWER_CASE_a int, @LOWER_CASE_z int, @Delimiter char(5), @UPPER_CASE_A int, @UPPER_CASE_Z int
--Variable/Constant initializations
SET @ctr = 1
SET @len = LEN(@input)
SET @output = ''
SET @LOWER_CASE_a = 97
SET @LOWER_CASE_z = 122
--SET @Delimiter = ' ,-'
SET @Delimiter = ' ,-''"/'
SET @UPPER_CASE_A = 65
SET @UPPER_CASE_Z = 90
WHILE @ctr <= @len
BEGIN
--This loop will take care of reccuring white spaces
WHILE CHARINDEX(SUBSTRING(@input
BEGIN
SET @output = @output + SUBSTRING(@input,@ctr,1)
SET @ctr = @ctr + 1
END
IF ASCII(SUBSTRING(@input,@ct
BEGIN
--Converting the first character to upper case
SET @output = @output + UPPER(SUBSTRING(@input,@ct
END
ELSE
BEGIN
SET @output = @output + SUBSTRING(@input,@ctr,1)
END
SET @ctr = @ctr + 1
WHILE CHARINDEX(SUBSTRING(@input
BEGIN
IF ASCII(SUBSTRING(@input,@ct
BEGIN
--added to handle exception.... A = 65, V = 86, L = 76, T = 84, C = 67, I = 73
if ascII(substring(@input,@ct
begin
if ascII(substring(@input,@ct
ascII(substring(@input,@ct
ascII(substring(@input,@ct
begin
SET @output = @output + UPPER(SUBSTRING(@input,@ct
end
Else
begin
SET @output = @output + LOWER(SUBSTRING(@input,@ct
end
end
else
SET @output = @output + LOWER(SUBSTRING(@input,@ct
END
ELSE
BEGIN
SET @output = @output + SUBSTRING(@input,@ctr,1)
END
SET @ctr = @ctr + 1
END
END
if @output = 'mar-a-lago'
begin
set @output = 'Mar-a-Lago'
end
if @output = 'LA Bella Chair'
begin
set @output = 'La Bella Chair'
end
if @output = 'LA Bella Love Seat'
begin
set @output = 'La Bella Love Seat'
end
if @output = 'LA Bella Ottoman'
begin
set @output = 'La Bella Ottoman'
end
if @output = 'LA Bella Sofa'
begin
set @output = 'La Bella Sofa'
end
if @output = 'LA Mancha Chair'
begin
set @output = 'La Mancha Chair'
end
if @output = 'LA Palma Chair'
begin
set @output = 'La Palma Chair'
end
if @output = 'BocA End Table'
begin
set @output = 'Boca End Table'
end
RETURN @output
END
--SELECT dbo.PROPERCASE('O/hara Mirror')
>>What about a /? What is the data is O/hara Mirror <<
patel's function takes the / character into consideration. :)
patel's function takes the / character into consideration. :)
ASKER
I know, but THE BOSS wants to use the existing function...I know I know but I'm not the boss..
ASKER
I need O/harra mirror to be O/Harra Mirror
You have too many 's in the delimiter string.
It should be this:
SET @Delimiter = ' ,-''/'
It should be this:
SET @Delimiter = ' ,-''/'
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Just a note, if you don't change the declaration then only the first X delimiters will be used, where X is the value used in the declaration.
eg if the declaration is char(3) only the first 3 characters will be recogninsed.
eg if the declaration is char(3) only the first 3 characters will be recogninsed.
SET @Delimiter = ' ,-'
to
SET @Delimiter = ' ,-'''