sql-serverexpressioncasecoalescenullif

Case with NULL expression in SQL Server


I have a problem with this piece of code and i'm pretty sure the solution is quite easy, but i don't see it.

CASE @L0 WHEN '/' THEN NULL ELSE @L0 END

It is part of code i use to import data out of a flat file (csv), where NULL is stored as /. The problem is that @L0 can also be an input variable filled by another programm and it is possible that it might be NULL. Due to that it's not allowed to use a NULL value as expression in a CASE, the code doesn't work and i get an error message.

Can somebody tell me how to do it the right way?


Solution

  • If you would just like to replace the '/' with NULL and leave all other strings untouched, you can use the NULLIF() function e.g. NULLIF(@Lo, '/')