SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


create a function to remove vowels from a given string


create a function to remove vowels from a given string

Author
Message
byecoliz
byecoliz
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 24
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 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')
davidandrews13
davidandrews13
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1915 Visits: 4641
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','')


Abu Dina
Abu Dina
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3007 Visits: 3325
Arrghh David beat me to it! :-P

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

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
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)

Group: General Forum Members
Points: 95345 Visits: 38968
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




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
byecoliz
byecoliz
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 24
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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)

Group: General Forum Members
Points: 216114 Visits: 41986
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

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Lowell
Lowell
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72230 Visits: 40942
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
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Abu Dina
Abu Dina
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3007 Visits: 3325
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

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
Abu Dina
Abu Dina
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3007 Visits: 3325
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. 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.


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

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
byecoliz
byecoliz
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 24
Thank you all,I will take your advice.
regards
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search