Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug in LINQ to SQL with empty strings on database

I have been using LINQ to SQL for years now, but this is the first time I have seen this behavior.

I have a DB table with a few columns (varchar(15)) that may contain empty strings (''). I verify this by running LEN(Column) and checking the result be 0.

Now when I call this from LINQ2SQL, it returns the object field with a string containing a single space (string.Length == 1).

There are a few workarounds I could apply, like making them NULL on the DB or trimming the string, but I would like to know if anyone has come across this before or if the bug is known (reported on MS Connect). If not, I'll report it.

Thanks.

like image 571
leppie Avatar asked Oct 21 '11 08:10

leppie


1 Answers

The issue is with the LEN function:

SELECT LEN(' ')

Returns 0 in SQL Server; it is a total PITA.

But

SELECT DATALENGTH(' ')

Returns 1

like image 141
satnhak Avatar answered Nov 02 '22 07:11

satnhak