Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server adds empty spaces to value (char) when trying to set null

I am pulling data from SQL Server to my c# project. I have some textboxes that update from page-to-page. I have one textbox in particular that is set up to only accept two characters, which is setup in the database as char(2). If I were to delete those two characters and click my button to update the database and go to the next page, it stores two empty spaces. I need it to just be empty with no spaces. In my other textboxes, this issue does not occur. The database allows the data to be null. I am able to manually enter "null" in the database, but I need it to be done when erasing the two chars and updating it.

like image 313
smelmo Avatar asked Jun 05 '17 15:06

smelmo


People also ask

How do I get rid of blank spaces in SQL?

The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.

How do you set a blank NULL value in SQL?

You need to use NULLIF() function from MySQL. The syntax is as follows: SELECT NULLIF(yourCoumnName,' ') as anyVariableName from yourTableName; In the above syntax, if you compare empty string( ' ') to empty string( ' '), the result will always be NULL.

IS NULL empty or whitespace SQL?

The IS NULL operator is used to test for empty values (NULL values).

Is NULL and empty string the same in SQL?

Sometimes strings can be empty or NULL. The difference is that NULL is used to refer to nothing. However, an empty string is used to point to a unique string with zero length.


1 Answers

A column declared as CHAR(2) may contain one of the following:

  • 2 characters
  • NULL.

A column declared as CHAR(2) may not contain any of the following:

  • 0 characters
  • 1 character
  • 3 characters
  • etc.

When you try to store anything other than either 2 characters or NULL in the column, your database will troll you in the name of some ill-conceived notion of convenience: instead of generating an error, it will store something other than what you gave it to store.

(Amusingly enough, receiving an error when doing something wrong is, and historically has been, regarded as an inconvenience by a surprisingly large portion of programmers. But that's okay, that's how we get stackoverflow questions to answer.)

Specifically, your database will pad the value you are storing with spaces, to match the length of the column. So, if you try to store just one character, it will add one space. If you try to store zero characters, it will add two spaces.

Possible Solutions:

  • If you have the freedom to change the type of the column:

    Declare it as VARCHAR instead of CHAR(2), so that it will contain exactly what you store in it.

  • If you do not have the freedom to change the type of the column:

    You have to always be manually checking whether you are about to store an empty string into it, and if so, store NULL instead.

Note about Oracle

The Oracle RDBMS before version 11g (and perhaps also in more recent versions, I am not sure, if someone knows, please leave a comment) will do that last conversion for you: if you try to store an empty string, it will store NULL instead. This is extremely treacherous due to the following reasons:

  • It is yet one more example of the database system trolling you by storing something very different from what you gave it to store.

  • They apply the same rule to all types of character columns, even VARCHAR, which means that you cannot have an empty string even in columns that could accommodate one; you store either NULL or an empty string, you always get NULL back.

  • This behavior is completely different from the behavior of any other RDBMS.

  • The behavior is fixed, there is no way to configure Oracle to quit doing that.

like image 84
Mike Nakis Avatar answered Oct 01 '22 20:10

Mike Nakis