Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Default to string.empty

Author  Topic 

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2007-09-03 : 17:30:43
I want to default a varchar column to be a empty string, not null.

I used to use a space char as the default value, but I really don't want the default value to be a space, but rather a empty string.

How do I accomplish this?

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-09-03 : 18:02:44
An empty string is simply ''. Notice there is not a space between the two single quotes.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-04 : 04:05:13
Can I question why you want to do that?

How can you then distinguish between a value, provided by the user, which is "the empty string", and a default value - which, in effect, means that the value is not currently known?

To my mind that is what NULL is for

Kristen
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2007-09-04 : 22:00:34
quote:
Originally posted by Kristen

Can I question why you want to do that?

How can you then distinguish between a value, provided by the user, which is "the empty string", and a default value - which, in effect, means that the value is not currently known?

To my mind that is what NULL is for

Kristen


the real reason is I'm lazy... I didn't want to need to re-write my queries to accomidate the null value, or modify my front end app to deal with it. In .net a null value will cause a exception when trying to evaluate it if you don't code specifically to check for the null (Kindof a pain when multiple forms referance the field). As a precaution, I just wanted to save myself some headaches since that field is not used to query off of, but rather as a arbitrary display to the user, so it was easier for me to just make sure it was always empty, rather then go through my entire app hoping I didn't miss coding any areas to handle the possible null.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-05 : 03:44:03
"the real reason is I'm lazy"

Brilliant!

"In .net a null value will cause a exception when trying to evaluate it if you don't code specifically to check"

Interesting, didn't know that. When confronted with that sort of thing we normally take care of it "upstream" at the Application end. So we pull ResultSets into memory, and massage them to be "front end ready" at the same time. So the application could, in this case, be sure to get a non-null value in all string columns, but still have a means of checking if a column was actually NULL. (So an extra attribute is added to the column for whether NULL or not, and any NULL values set to empty-string so that the application doesn't barf).

Just thinking out loud, ignore if no relevance ...

Don't forget to make the column NOT NULL, as well as setting a default, so that it can't be set to NULL.

Kristen
Go to Top of Page
   

- Advertisement -