Empty string and Default values

Published 02 September 08 08:56 AM | Madhivanan 

Often newbies use empty string and consider it as NULL. But when an empty string is passed, it may not be stored as empty string or NULL. It actually depends on the datatype of the column

Consider this example

DECLARE @t TINYINT,@s SMALLINT,@i INT,@big BIGINT,@f FLOAT,@bit BIT, @sm SMALLMONEY,@m MONEY,@d DATETIME

 

SELECT

@t='',

@i='',

@big='',

@f='',

@bit='',

@sm='',
@m='',

@d=''

SELECT
@t as tinyint_col,

@i as int_col,

@big as bigint_col,

@f as float_col,

@bit as bit_col,

@sm as smallmoney_col,

@m as money_col,

@d as datetime_col

 

The result is Zero for all the variables except @d which has the date value of 1900-01-01 00:00:00.000





So beware of this and always use NULL if you want NULL values stored in the column
and dont use empty string

Comments

# Other SQL Server Blogs around the Web said on September 2, 2008 4:04 AM:

Often newbies use empty string and consider it as NULL. But when an empty string is passed, it may not

# SQL Server Transact-SQL (SSQA.net) : Empty string and Default values said on September 2, 2008 4:05 AM:

PingBack from http://sqlserver-qa.net/blogs/t-sql/archive/2008/09/02/4832.aspx

# Null vs Empty (Zero Length) string « Systems Engineering and RDBMS said on March 27, 2009 5:50 AM:

PingBack from http://decipherinfosys.wordpress.com/2009/03/27/null-vs-empty-zero-length-string/

Anonymous comments are disabled

Search

Go

This Blog

«September 2008»
SMTWTFS
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

Syndication