February 15, 2013 at 8:57 am
Hi,I am creating a function to remove vowels from a given string but its not working,help me to correct it or give me the correct answer.thank you.
CREATE FUNCTION FN_REMOVEL_VOWELS (@STRING VARCHAR(max))
returns varchar
as
begin
declare @v-2 varchar(max)='AEIOUY' ,@startpoint int =1,@letter varchar(max)
select @letter=SUBSTRING(@v,@startpoint,1)
while @startpoint<LEN(@v)
begin
select @STRING=REPLACE(@STRING,@letter,'')
set @startpoint=@startpoint+1
end
return @STRING
end
SELECT DBO.FN_REMOVEL_VOWELS('INFORMATION SYSTEM')
February 15, 2013 at 9:04 am
doesn't this do it?
DECLARE @noVowels [varchar](50)
SET @noVowels = 'Information System'
SELECT
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(@noVowels,'A','')
,'E','')
,'I','')
,'O','')
,'U','')
February 15, 2013 at 9:06 am
Arrghh David beat me to it! 😛
CREATE FUNCTION dbo.FN_REMOVEL_VOWELS (@STRING VARCHAR(max))
returns varchar(max)
as
begin
RETURN REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@STRING, 'A', ''), 'E', ''), 'O', ''), 'U', ''), 'I', '')
END
SELECT DBO.FN_REMOVEL_VOWELS('INFORMATION SYSTEM')
Quick note about your original function. Don't RETURN VARCHAR unless you know your string is never more than 8 characters long.
---------------------------------------------------------
It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens[/url]
Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
February 15, 2013 at 9:10 am
I'd do it this way and use it in a CROSS APPLY in the FROM clause:
create function dbo.RemoveVowels(
@pString varchar(max)
)
returns table
as
return (select replace(replace(replace(replace(replace(replace(@pString,'Y',''),'U',''),'O',''),'I',''),'E',''),'A','') NoVowels);
go
February 15, 2013 at 9:35 am
thank you all for that new code. Abu Dina, you're right on the return correction. Now when i do that correction the function removes only the 'A' vowel., meaning the increment is not working.how can I organize these statements so that the increment works. thank
February 15, 2013 at 9:37 am
byecoliz (2/15/2013)
thank you all for that new code. Abu Dina, you're right on the return correction. Now when i do that correction the function removes only the 'A' vowel., meaning the increment is not working.how can I organize these statements so that the increment works. thank
Forget the code that increments. Do it the way that Lynn suggested. It will be much faster than any scalar function that you can write.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 15, 2013 at 9:40 am
and a simple cross apply example:
with myCTE(val)
AS
(
SELECT 'Heavy rains that fell across the Upper Mississipp' UNION ALL
SELECT 'i River Basin in the summer of 2007 were responsi' UNION ALL
SELECT 'ble for the Federal Emergency Management Agency (' UNION ALL
SELECT 'FEMA) 1771-DR-IL disaster declaration. These sam' UNION ALL
SELECT 'e rains caused significant flooding in southeaste' UNION ALL
SELECT 'rn Minnesota, eastern Iowa, southern Wisconsin an' UNION ALL
SELECT 'd northern Illinois. Large portions of northern ' UNION ALL
SELECT 'Illinois received between 125 and 175 inches of r' UNION ALL
SELECT 'ain during this period, and this, combined '
)
select val,a.val2
FROM MyCTE
CROSS APPLY (SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(val, 'A', ''), 'E', ''), 'O', ''), 'U', ''), 'I', '') as val2
) a
Lowell
February 15, 2013 at 9:57 am
Take the others' advice and ditch the loop for the inline table valued function.
---------------------------------------------------------
It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens[/url]
Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
February 15, 2013 at 10:01 am
Jeff Moden (2/15/2013)
byecoliz (2/15/2013)
thank you all for that new code. Abu Dina, you're right on the return correction. Now when i do that correction the function removes only the 'A' vowel., meaning the increment is not working.how can I organize these statements so that the increment works. thankForget the code that increments. Do it the way that Lynn suggested. It will be much faster than any scalar function that you can write.
Unless it's a SQL CLR C# UDF surely?
Well I guess it depends on what the UDF needs to do. I recently posted a question about a SQL scalar function I had which contained over 200 IF statements. It was performing like a pig and I couldn't see a way of converting it to an iTVF so I write it in C#. The result? the C# version was 100 times faster than the SQL equivalent.
---------------------------------------------------------
It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens[/url]
Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
February 15, 2013 at 10:08 am
Thank you all,I will take your advice.
regards
February 15, 2013 at 10:13 am
Abu Dina (2/15/2013)
Jeff Moden (2/15/2013)
byecoliz (2/15/2013)
thank you all for that new code. Abu Dina, you're right on the return correction. Now when i do that correction the function removes only the 'A' vowel., meaning the increment is not working.how can I organize these statements so that the increment works. thankForget the code that increments. Do it the way that Lynn suggested. It will be much faster than any scalar function that you can write.
Unless it's a SQL CLR C# UDF surely?
Well I guess it depends on what the UDF needs to do. I recently posted a question about a SQL scalar function I had which contained over 200 IF statements. It was performing like a pig and I couldn't see a way of converting it to an iTVF so I write it in C#. The result? the C# version was 100 times faster than the SQL equivalent.
I agree... properly written CLRs are usually much better at string handling. But they're not a panacea of performance and, speaking as a consultant, they're absolutely worthless if the shop you happen to be working in doesn't allow CLR. In this case (the vowel removal problem), you might find that the properly written iTVF function will be very nearly or just as performant as a CLR function.
I've also found that if a function has over 200 IF statements, that it's time to go back to the drawing board. Many such "hopeless" functions frequently do have a much more effective and easy to code solution. there may also be the case where a stored procedure is more appropriate than a function.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 15, 2013 at 10:28 am
I've also found that if a function has over 200 IF statements, that it's time to go back to the drawing board. Many such "hopeless" functions frequently do have a much more effective and easy to code solution. there may also be the case where a stored procedure is more appropriate than a function.
See when people like you say stuff like this I get really worried! :crying:
The frunction is part of a record linkage application I'm developing. It joins a dataset on itself say on postcode and surname then individual elements of the duplicate pairs are compared and assigend scores for the name part, address, email etc...
This particualr function calculates the likelyhood of two records having the same name details. See XML below. This is saying that
IF Surname the same AND Firstname the same then the score will depend on the cmparison of middlename
But as you can see there are many combinations of this like
if Surname the same AND forename sounds the same then middlename outcome gives different score.
Now repeat this for when the surnames sounds the same and when surnames are approximately the same etc and this is how I ended up with so many IFs
How else to implement this without IF or CASE ststements?!
Just realised this is so rude.. me hijacking the thread for my own needs. Sorry!
<lastnames match="equal">
<firstnames match="equal">
<middlenames match="equal">sure</middlenames>
<middlenames match="both_empty">sure</middlenames>
<middlenames match="one_empty">sure</middlenames>
<middlenames match="approx">likely</middlenames>
<middlenames match="contains">likely</middlenames>
<middlenames match="unequal">possible</middlenames>
</firstnames>
<firstnames match="sounds_equal">
<middlenames match="equal">sure</middlenames>
<middlenames match="both_empty">likely</middlenames>
<middlenames match="one_empty">likely</middlenames>
<middlenames match="approx">possible</middlenames>
<middlenames match="contains">possible</middlenames>
<middlenames match="unequal">zero</middlenames>
</firstnames>
---------------------------------------------------------
It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens[/url]
Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply