1

I have a MS SQL query on which I am using CASE STATEMENT. Like below -

SELECT NAME, EMAIL,
CASE Type WHEN LEN(Type) > 1 
           THEN 'LENGHT EXCEED' 
          WHEN '' 
           THEN 'Type is required' 
          WHEN NULL 
           THEN 'Type is required' 
          ELSE '' 
END
FROM TABLE

In the above query I am using CASE STATEMENT TO PUT VALIDATION on Type field. I am applying length and required field validations in the query.

But LEN(Type) > 1 is not working. It's

showing wrong syntax.

Can any one suggest how I can use it and also how we can call a function inside CASE STATEMENT.

Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
Pawan Agrawal
  • 263
  • 2
  • 4
  • 11
  • 2
    Replace `CASE type WHEN` with `CASE WHEN` – Lamak May 12 '16 at 13:39
  • yes it will work with CASE WHEN but what about other condition (required) validation ? Can you suggest the syntax for both ? – Pawan Agrawal May 12 '16 at 13:40
  • 1
    come on, you could search for the right syntax: `CASE WHEN LEN([Type]) > 1 THEN 'LENGHT EXCEED' WHEN [Type] = '' THEN 'Type is required' WHEN [Type] IS NULL THEN 'Type is required' ELSE '' END` – Lamak May 12 '16 at 13:42

1 Answers1

3

Try this

   CASE WHEN LEN(Type) > 1 
        THEN 'LENGHT EXCEED' 
        WHEN Type = '' 
        THEN 'Type is required' 
        WHEN Type IS NULL 
        THEN 'Type is required' 
        ELSE 
   END
Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
  • 2
    No, it's not gonna work for every condition. You shouldn't use `WHEN type = NULL`, it's `WHEN type IS NULL` – Lamak May 12 '16 at 13:45