SQL CASE语句

时间:2008-12-11 00:21:40

标签: sql sql-server sql-server-2000 case-statement

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

感谢。

3 个答案:

答案 0 :(得分:6)

这应该这样做:

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

答案 1 :(得分:4)

试试这个:

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)

答案 2 :(得分:0)

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

或者您可能需要

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

Here's the reference.

豫ICP备18024241号-1