If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Convert empty string to null automatically

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-21-06, 16:45
PolarBear2k PolarBear2k is offline
Registered User
 
Join Date: Jun 2005
Posts: 79
Convert empty string to null automatically

Hi,

Is there a setting for MySQL 4.1 to automatically convert empty strings to null when inserting and updating?

Thanks
Reply With Quote
  #2 (permalink)  
Old 04-21-06, 19:30
dbmab dbmab is offline
Registered User
 
Join Date: Apr 2006
Location: Denver, Co. USA
Posts: 240
I do not know of and could not find a setting to do this.

There are three ways to get a NULL to replace an empty string (assume in the following a table with name and info columns) -

1) Don't list the columns that are empty strings. The following query will insert a name with a NULL for info.
PHP Code:
$query "INSERT INTO table (name) values ('$myname')"
2) Explicitly put the NULL keyword in the query (note that you cannot put a '$myinfo' variable, with or without quotes, and set it to NULL or 'NULL' and have it work) -
PHP Code:
$query "INSERT INTO table (name,info) values ('$myname',NULL)"
3) Use an IF statement to give a NULL or the original value -
PHP Code:
$query "INSERT INTO table (name,info) values (IF(LENGTH('$myname')=0,NULL,'$myname'),IF(LENGTH('$myinfo')=0,NULL,'$myinfo'))"
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On