开发者

SQL DECIMAL column that supports and converts empty OR spaces into NULL

开发者 https://www.devze.com 2023-01-14 14:27 出处:网络
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 tha

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) V开发者_开发问答ALUES ('@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?


Try this

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

0

精彩评论

暂无评论...
验证码 换一张
取 消