tags:

views:

38

answers:

1

In my current project it would be convenient to exclude empty strings from the domain, so that is an empty string is met wherever, it is treated as NULL, for example if I UPDATE [table] SET [Something]='', SQL Server should treat it as SET [Something]=NULL, so that in next SELECT a NULL is returned as [Something] value.

Should I use triggers or there is a better way?

+1  A: 

There is no automatic means to force an empty string to be converted into a null. However, you could add check constraints on all char, varchar, nchar, and nvarchar colums which requires that the value be greater than zero or null. That would cause the client app to throw an exception if it did not convert the empty strings to null.

Beyond the use of check constraints, the ideal place to ensure that empty strings are converted to NULL is your data access layer. You should encapsulate the code that opens a connection, executes something against the database and then closes the connection. Presumably, that code will take an array of arguments used as the parameters to your parametrized queries. In that code, it can check if the data type is a string and if so, convert it to null.

Thomas