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
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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