Friday, March 9, 2012

NULL vs. empty strings, SQL Server setting

Without getting into a theoretical discussion about whether it is the
right thing to do, is there a switch or flag for SQL Server 2005 to
make NULL strings be the same as empty string?
Thanks,
K GalangaNo such switch...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Kat" <galangkk@.sverdrup.com> wrote in message
news:1160335879.054188.203150@.i3g2000cwc.googlegroups.com...
> Without getting into a theoretical discussion about whether it is the
> right thing to do, is there a switch or flag for SQL Server 2005 to
> make NULL strings be the same as empty string?
> Thanks,
> K Galanga
>|||Kat wrote:
> Without getting into a theoretical discussion about whether it is the
> right thing to do, is there a switch or flag for SQL Server 2005 to
> make NULL strings be the same as empty string?
> Thanks,
> K Galanga
Depending on what your intention is, maybe you could make a column NOT
NULL and add a default:
ALTER TABLE tbl ADD CONSTRAINT df_tbl_col DEFAULT ('') FOR col;
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||Hello David,
What do you mean by "the same", you can get a NULL to act as a '' in concate
nation
,however comparisions i.e. NULL = '' will always be inconclusive.
set concat_null_yields_null off
is what you want to achieve the former
Simon Sabin
SQL Server MVP
http://sqlblogcasts.com/blogs/simons

> Kat wrote:
>
> Depending on what your intention is, maybe you could make a column NOT
> NULL and add a default:
> ALTER TABLE tbl ADD CONSTRAINT df_tbl_col DEFAULT ('') FOR col;
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx --
>|||Hi Kat
As others have said, there is no server-wide switch.
There are some things you can do in different environments. For the purpose
of concatenation, you can treat NULLs as empty strings with
SET CONCAT_NULL_YIELDS_NULL OFF
However, that is a session setting, and will not affect anything other than
the current connection.
If you have a variable or parameter, and you want to treat NULL as if it is
an empty string, you can use the ISNULL function in your queries. But that
is valid just for a single expression
HTH
Kalen Delaney, SQL Server MVP
http://sqlblog.com
"Kat" <galangkk@.sverdrup.com> wrote in message
news:1160335879.054188.203150@.i3g2000cwc.googlegroups.com...
> Without getting into a theoretical discussion about whether it is the
> right thing to do, is there a switch or flag for SQL Server 2005 to
> make NULL strings be the same as empty string?
> Thanks,
> K Galanga
>

No comments:

Post a Comment