tags:

views:

20

answers:

1

I have a DECIMAL field in my SQL table, and I want to support EMPTY and SPACES as well. So, when the user enters nothing OR spaces only in that field, I still want the table to take it and convert that into NULL.

I tried using CASE in an INSERT statement, in SQL. I am using it under PHP. I tried this:

INSERT INTO test (id,value) VALUES ('@id',CASE WHEN LTRIM(RTRIM('@amount')) IS NULL THEN LTRIM(RTRIM('@amount')) ELSE NULL END);

This doesn't work. However, the exact same statement but for UPDATE works fine!

If I can solve the problem using a different approach, please advise?

A: 

Try this

NULLIF(LTRIM(RTRIM('@amount')),'')

Pranay Rana
Not working. CASE is not working in the INSERT statement at all!
johnshaddad
@johnshaddad - try to use NULLIF function
Pranay Rana
That solved it. Thanks!
johnshaddad