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 2000 Forums
 SQL Server Development (2000)
 Checking for value

Author  Topic 

NewNewbie
Starting Member

3 Posts

Posted - 2002-03-25 : 11:13:53
Hi!
I'm having a problem with loading spaces in my SQL Server database. I know it's coming from the form, because I am initializing all the formfields with spaces before displaying.

How can I (in a stored procedure) examine each field for spaces, and if they are equal to spaces only, insert nulls instead?

Teroman
Posting Yak Master

115 Posts

Posted - 2002-03-25 : 11:38:53
paste this into query analyser, should explain what is required

declare @foo char(10), @bar char(10)

set @foo = ''

select @bar = nullif(rtrim(@foo), '')

select @bar

set @foo = 'thing'

select @bar = nullif(rtrim(@foo), '')

select @bar

col


Go to Top of Page

NewNewbie
Starting Member

3 Posts

Posted - 2002-03-25 : 11:44:17
Thanks for your help!

Not quite sure how this works...

What exactly does this statement do?

select @bar = nullif(rtrim(@foo), '')

Does it set @bar=null if your right trim @foo and nothing but spaces are found?. Is that what the '' on the end is??

Thx!



Go to Top of Page

Teroman
Posting Yak Master

115 Posts

Posted - 2002-03-25 : 11:54:34
it sets the @bar variable to NULL if both arguments are equal, to the first if they are different.

so, it trims all the spaces from @foo, and compares it to an empty string.

if @foo is just spaces trimming them off leaves an empty string, both arguments are equal, we get NULL

if @foo contains anything else then trimming it does not give an empty string, @bar gets set to @foo.

hope its clear now, always try the transact-SQL help from query analyser for help on functions, its very useful

col

Go to Top of Page

NewNewbie
Starting Member

3 Posts

Posted - 2002-03-25 : 12:20:49
Yes! I think I understand now.

Thanks for your help!!

Go to Top of Page
   

- Advertisement -