Solved

How do I exclude null values from a user defined functionso they don't show up in radio button list?

Posted on 2008-10-25
11
242 Views
Last Modified: 2010-04-21
Here is the code:
select  ID , Answer1 AS Answer
from yourTable
where  ID=1
UNION
select  ID , Answer2 AS Answer
from yourTable
where  ID=1
UNION
select  ID , Answer3 AS Answer
from yourTable
where  ID=1

Except it returns null values in the list if they are empty in the db. How do I EXCLUDE NULL values???
Thanks.
0
Comment
Question by:bolenka
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 22804041
what do you mean if they are empty? the answers?

try

select id, isnull(answer, 'UnSpecified') from
(select  ID , Answer1 AS Answer
from yourTable
where  ID=1
UNION
select  ID , Answer2 AS Answer
from yourTable
where  ID=1
UNION
select  ID , Answer3 AS Answer
from yourTable
where  ID=1) t
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22804081
what do you want to do when the values are null
want to show them in the radio list or what
the solution that we propose will depend on that
0
 

Author Comment

by:bolenka
ID: 22804117
I dont want them to show in the radiobuttonlist at all....yes, if the Answer field is empty...do not show them in the radiobuttonlist. Thanks.
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 300 total points
ID: 22804123
select id, answer
(select  ID , Answer1 AS Answer
from yourTable
where  ID=1
UNION
select  ID , Answer2 AS Answer
from yourTable
where  ID=1
UNION
select  ID , Answer3 AS Answer
from yourTable
where  ID=1) t
where answer is not null
0
 
LVL 9

Assisted Solution

by:jamesgu
jamesgu earned 125 total points
ID: 22804131
select  ID , Answer1 AS Answer
from yourTable
where  ID=1 AND Answer1  IS NOT NULL
UNION
select  ID , Answer2 AS Answer
from yourTable
where  ID=1 AND Answer2  IS NOT NULL
UNION
select  ID , Answer3 AS Answer
from yourTable
where  ID=1 AND Answer3  IS NOT NULL
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 25 total points
ID: 22804133
momi_sabag has given you the answer before i can even react ot it :)
0
 

Author Comment

by:bolenka
ID: 22807161
I don't think this is working the way I want it to...I have to spend some time trying each one of these before I assign points, what is the"t" for after the select statement momi sabag?

I would like it if it would NOT select the answer at all into the list if the value is NULL:

also, I have to assign an ID to each answer because each answer does not havean id...for example, this is what I have...


ALTER Function [dbo].[radiobuttonListDD] (@ID int)
Returns Table
As
Return
ALTER Function [dbo].[radiobuttonListDD] (@ID int)
Returns Table
As
Return
(select  '1' as IDValue , Answer1 AS Answer
from dbo.userAnswers1
where  ID=@ID
UNION
select  '2' as IDValue , Answer2 AS Answer
from dbo.userAnswers1
where  ID=@ID)
--Select * from dbo.radiobuttonListDD(15390)

I think is gives me results even if the Answer column is null since I am assigning and ID to each. How do I make them not show up AT ALL if the Answer column is NULL?

Thanks so much

0
 

Author Comment

by:bolenka
ID: 22807164
sorry...copied the alter function part 2 x. just ignore the first alter statement.
0
 
LVL 13

Assisted Solution

by:sm394
sm394 earned 50 total points
ID: 22807257
try that any issues let me know

ALTER FUNCTION fnTest
(
      -- Add the parameters for the function here
      @ID int
      
)
RETURNS
@Table_Var TABLE
(
      -- Add the column definitions for the TABLE variable here
      ID int IDENTITY,
      Answer nvarchar(255)
)
AS
BEGIN
      -- Fill the table variable with the rows for your result set


INSERT INTO @Table_Var (Answer)
select   Answer1 AS Answer
from yourTable
where  ID=@ID AND Answer1 IS NOT NULL
UNION
select   Answer2 AS Answer
from yourTable
where  ID=@ID AND Answer2 IS NOT NULL
      RETURN
END
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 22807661
the t has to be there because of syntax rules
when you use a derived table (that is, a select statement in a where clause surrounded by parenthesis ) you need to give it an alias
0
 

Author Closing Comment

by:bolenka
ID: 31509972
I actually had to change the IS NOT NULL to where Answer <> '' that is how I got the results that I wanted. I wanted the record to not show up in the results table, but the null wasnt working. I tried to split the points to be fair. thanks so much to everyone...I used momi sabag's answer first so that is who I gave the most points. thank you!
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can I build my own IDE using ASP.NET MVC? 2 33
Sql query 107 29
Help Extract Specific in SQL 8 25
SSRS troubles 4 20
I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now