2013-04-09 26 views
7

Sto creando una funzione per restituire '0' o '1' a seconda del risultato delle istruzioni nidificate if else. Utilizzando MSSQL.Istruzione nidificata if else

ALTER FUNCTION udf_check_names_starting_with_quotationmark 
(@string NVARCHAR(100)) 
RETURNS INT 
AS 
BEGIN 
    DECLARE @condition INT 
    SET @string = LTRIM(@string) 
    SET @string = RTRIM(@string) 

    IF (PATINDEX('"%', @string) !=0) 
    BEGIN 
     SET @condition = 1 
    END 
    ELSE IF (PATINDEX('%"%', @string) !=0) 
    BEGIN 
     SET @condition=1 
    END 
    ELSE IF (PATINDEX('%"', @string) !=0) 
    BEGIN 
     SET @condition = 1 
    END 
    ELSE 
    BEGIN 
     SET @condition=0 
    END 
    RETURN @condition 
END 

Tutto sta funzionando bene con questo. C'è un modo migliore per ottenere questo (ho provato a utilizzare OR ma editor SQL che mostra un errore, non riconoscendo l'OR).

risposta

6
SET @condition = (case when PATINDEX('"%', @string) !=0 or 
          PATINDEX('%"%', @string) !=0 or 
          PATINDEX('%"', @string) !=0 then 1 else 0 end) 
+0

Grazie? sei così tanto per questo aiuto .. Stavo giocando con Case ma non riuscivo a farlo funzionare, mio ​​male .. Grazie .. –

3

Sicuramente questo può essere semplificata:

IF (PATINDEX('%"%', @string) !=0) 
    BEGIN 
     SET @condition=1 
    END 
    ELSE 
    BEGIN 
     SET @condition=0 
    END 

- dal PATINDEX('%"%', @string) sarà> 0 in cui o PATINDEX('"%', @string) o PATINDEX ('% "', @String) sono> 0

+0

Grazie per l'aiuto .. –