Unix Technical Forum

Create User Function w/Case

This is a discussion on Create User Function w/Case within the SQL Server forums, part of the Microsoft SQL Server category; --> I keep getting an error message "incorrect syntax near keyword case" when trying to run this: USE DEDUPE GO ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:07 AM
EoRaptor013
 
Posts: n/a
Default Create User Function w/Case

I keep getting an error message "incorrect syntax near keyword case"
when trying to run this:

USE DEDUPE
GO

CREATE FUNCTION fnCleanString
(
@mString varchar (255)
)
RETURNS varchar(255)
AS
BEGIN
DECLARE
@mChar char(1),
@msTemp varchar(255),
@miLen int,
@i int,
@iAsc int
BEGIN
set @mChar = ''
set @msTemp = ''
set @miLen = Len(@mString)
set @i = 1
while @i <= @miLen
begin
set @mChar = substring(@mString,@i,1)
set @iAsc = Ascii(@mChar)
case
when @iAsc >= 87 And iAsc <= 122 Then set @mChar = @mChar
when iAsc >= 65 And iAsc <= 90 Then set @mChar = @mChar
when iAsc >= 49 And iAsc <= 57 Then set @mChar = @mChar
else @mChar = ""
end
set @msTemp = @msTemp & @mChar
set @i = @i + 1
end
END
RETURN @msTemp
END

Can anybody point out what I'm doing wrong?
Thanks.
Randy

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:07 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Create User Function w/Case

EoRaptor013 (rchrismon@patmedia.net) writes:
> I keep getting an error message "incorrect syntax near keyword case"
> when trying to run this:
>...
> case
> when @iAsc >= 87 And iAsc <= 122 Then set @mChar = @mChar
> when iAsc >= 65 And iAsc <= 90 Then set @mChar = @mChar
> when iAsc >= 49 And iAsc <= 57 Then set @mChar = @mChar
> else @mChar = ""
> end
> Can anybody point out what I'm doing wrong?


A general advice when you have problem with syntax, look it up in
Books Online, instead of just making wild guesses. Books Online
says about CASE: "Evaluates a list of conditions and returns one of multiple
possible result expressions. ". A little further it says: "The simple CASE
function compares ...". If that doesn't ring a bell, you will get some
guidance from the Examples section.

CASE is an expression. Not a statement.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:07 AM
Simon Hayes
 
Posts: n/a
Default Re: Create User Function w/Case


"EoRaptor013" <rchrismon@patmedia.net> wrote in message
news:1108159891.406126.35640@c13g2000cwb.googlegro ups.com...
>I keep getting an error message "incorrect syntax near keyword case"
> when trying to run this:
>
> USE DEDUPE
> GO
>
> CREATE FUNCTION fnCleanString
> (
> @mString varchar (255)
> )
> RETURNS varchar(255)
> AS
> BEGIN
> DECLARE
> @mChar char(1),
> @msTemp varchar(255),
> @miLen int,
> @i int,
> @iAsc int
> BEGIN
> set @mChar = ''
> set @msTemp = ''
> set @miLen = Len(@mString)
> set @i = 1
> while @i <= @miLen
> begin
> set @mChar = substring(@mString,@i,1)
> set @iAsc = Ascii(@mChar)
> case
> when @iAsc >= 87 And iAsc <= 122 Then set @mChar = @mChar
> when iAsc >= 65 And iAsc <= 90 Then set @mChar = @mChar
> when iAsc >= 49 And iAsc <= 57 Then set @mChar = @mChar
> else @mChar = ""
> end
> set @msTemp = @msTemp & @mChar
> set @i = @i + 1
> end
> END
> RETURN @msTemp
> END
>
> Can anybody point out what I'm doing wrong?
> Thanks.
> Randy
>


CASE is an expression, not a control of flow statement - see the examples in
Books Online. Or you can use a single IF (and I think you mean 97, not 87,
otherwise your first two ranges would overlap):

if not (
@iAsc between 97 and 122 or
@iAsc between 65 and 90 or
@iAsc between 49 and 57
)
set mChar = ''

Simon


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 07:07 AM
EoRaptor013
 
Posts: n/a
Default Re: Create User Function w/Case

Thank you, responders.
Please believe, I always read the BOL before asking. In this case (no
pun intended), I believe the BOL could be clearer. In light of your
responses, I looked again and believe the real clue I should have
gotten was the use of the word "function". A function returns a value,
therefore I need something to hold that value. I agree that the
examples should help. Unfortunately, my version of the BOL does not
show CASE being used to assign a value to a variable or other
receptacle. Since I'm not the sharpest pencil in the box, the SELECT
statements didn't turn on any light blubs for me.
Again, thank you all very much.
Randy

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:09 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com