Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatic Empty to Null Conversion

Status
Not open for further replies.

mlager

Programmer
Sep 1, 2006
74
0
0
US
Is there any way to have empty values converted to NULL on an insert or an update? I've got an application that will inserted '' rather than NULL if a textbox is left blank. My desire is to have it insert a NULL. I was hoping to find a solution that would enable me to not have to do all this checking in my code and do it at the DB level, possibly a trigger?
 
I also think it should be done by your application

However, you can force the NULL values using before triggers.
Here is an example:
Code:
DELIMITER $$
CREATE TRIGGER t_insert_before_table BEFORE INSERT ON table 
    FOR EACH ROW BEGIN
    IF NEW.Textfield = '' THEN
       NEW.Textfield=NULL;
    END IF;
END;
$$

DELIMITER ;
NEW is the record that will be inserted in the table.
In UPDATE TRIGGER you have NEW that contains the variable OLD that contains the values before the update)

---

The surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us - Calvin (and Hobbes) ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top