Saif Khan
Saif Khan

Reputation: 18782

SQL CASE Statement

Is the following possible in SQL Server 2000?

CREATE FUNCTION getItemType  (@code varchar(18))
RETURNS int
AS
BEGIN
Declare @Type tinyint
Select @Type = case len(@code)
WHEN 12,14,17 THEN 1
WHEN 13,15,18 THEN 2
WHEN 8,10 THEN 3
ELSE  0
END
RETURN (@Type)
END

Thanks.

Upvotes: 0

Views: 5586

Answers (3)

dkretz
dkretz

Reputation: 37645

 try  
  SELECT CASE
           WHEN LEN(@gcode) IN(x, y, z) THEN a  
         END
 etc.

or you may need

SELECT CASE LEN(@gcode)  
         WHEN x THEN a  
         WHEN y THEN a  
       END

etc.

Here's the reference.

Upvotes: 0

Dave Markle
Dave Markle

Reputation: 97671

This should do it:

CREATE FUNCTION getItemType(@code VARCHAR(18))
RETURNS INT
AS
BEGIN
    RETURN CASE 
        WHEN LEN(@code) IN (12,14,17) THEN 1
        WHEN LEN(@code) IN (13,15,18) THEN 2
        WHEN LEN(@code) IN (8,100)    THEN 3
        ELSE  0
    END
END

Upvotes: 6

keithwarren7
keithwarren7

Reputation: 14280

try this:

Select @Type = 
(select case 
WHEN len(@code) IN (12,14,17) THEN 1
WHEN len(@code) IN (13,15,18) THEN 2
WHEN len(@code) IN (8,10) THEN 3
ELSE  0
END)

Upvotes: 4

Related Questions