sql server - MS SQl string alphanumeric conversion -
i need convert strings stored in table lower case alphanumeric. after search found way
select lower(cast(replace(cname, substring(cname, patindex('%[^a-za-z0-9]%', cname), 1), '') varchar(15))), nnameid namestable
where names table namestable(nnameid int indentity, cname varchar(100))
its working fine strings
'abc pqr' , 'abc pqr 123', 'abc@pqr' , 'abc-pqr' , 'abc*pqr'
, , returning
abcpqr, abcpqr123, abcpqr , abcpqr , abcpqr
respectively.
but strings
'**abc pqr*' , 'abc pqr 123.' , '&abc@ pqr$' , '@abc- pqr( ' , '*abc*pqr]'
it returning values
abc pqr , abcpqr123. , abc@pqr$ , abc-pqr( , abcpqr]
.
whats going wrong?
also, created following function n t-sql same task
create function [dbo].[ufnremovespecialchars] (@s varchar(256)) returns varchar(256) schemabinding begin if @s null return null declare @s2 varchar(256) set @s2 = '' declare @l int set @l = len(@s) declare @p int set @p = 1 while @p <= @l begin declare @c int set @c = ascii(substring(@s, @p, 1)) if @c between 48 , 57 or @c between 65 , 90 or @c between 97 , 122 set @s2 = @s2 + char(@c) set @p = @p + 1 end if len(@s2) = 0 return null return @s2 end
i calling code using,
select lower(dbo.ufnremovespecialchars(cname)) namestable
, working fine.
which 1 better approach out of two?
Comments
Post a Comment