Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

How to substitute for a non-existing column in a joined ta..

 
   Database Help (Home) -> Programming RSS
Next:  Change Data Capture: Preserve capture instance da..  
Author Message
bob

External


Since: Apr 21, 2010
Posts: 2



(Msg. 1) Posted: Wed Apr 21, 2010 3:05 pm
Post subject: How to substitute for a non-existing column in a joined table
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi,

Is there a simpler way than a UNION to return a default value of a joined
table for which a corresponding row does not exist?

The following example (not a working one, of course) illustrates what I'm
after. I'd like to return 'N/A' as c2name if there is no matching row in t2

SELECT t1.c1

(CASE t2.t1pk
WHEN NULL THEN 'N/A'
ELSE t2.name END) AS c2name,

FROM t1

LEFT JOIN t2 ON t2.t1pk = t1.pk

Thanks.

 >> Stay informed about: How to substitute for a non-existing column in a joined ta.. 
Back to top
Login to vote
Jeroen Mostert

External


Since: Oct 08, 2009
Posts: 8



(Msg. 2) Posted: Wed Apr 21, 2010 4:25 pm
Post subject: Re: How to substitute for a non-existing column in a joined table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2010-04-21 21:05, bob wrote:
> Is there a simpler way than a UNION to return a default value of a joined
> table for which a corresponding row does not exist?
>
> The following example (not a working one, of course) illustrates what I'm
> after. I'd like to return 'N/A' as c2name if there is no matching row in t2
>
> SELECT t1.c1
>
If you put a comma here...

> (CASE t2.t1pk
> WHEN NULL THEN 'N/A'
> ELSE t2.name END) AS c2name,
>
> FROM t1
>
> LEFT JOIN t2 ON t2.t1pk = t1.pk
>
....then your query is syntactically valid and gives the desired result.

If you need something more complicated, you'll have to be more specific.

--
J.

 >> Stay informed about: How to substitute for a non-existing column in a joined ta.. 
Back to top
Login to vote
Q

External


Since: Feb 01, 2008
Posts: 5



(Msg. 3) Posted: Wed Apr 21, 2010 4:25 pm
Post subject: Re: How to substitute for a non-existing column in a joined table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

When comparing to NULL value, you have to use IS NULL or IS NOT NULL.
In your case, you need to re-write the CASE statement to:
(CASE WHEN t2.t1pk IS NULL THEN 'N/A'
ELSE t2.name END) AS c2name

Another example that I have is:
DECLARE @Emp TABLE (EmpID int, Name varchar(50))
declare @EmpNickName TABLE (EmpID int, NickName varchar(50))

INSERT INTO @Emp VALUES (1,'John T. Thompson')
INSERT INTO @Emp VALUES (2,'Gina Jackson')
INSERT INTO @Emp VALUES (3,'Dan Smith')

INSERT INTO @EmpNickName VALUES (1,'John')
INSERT INTO @EmpNickName VALUES (2,'Gina')

SELECT e.EmpID, e.Name,
(CASE WHEN n.NickName IS NULL THEN 'N/A'
ELSE n.NickName
END) as NickName
FROM @Emp e
LEFT JOIN @EmpNickName n ON n.EmpID = e.EmpID



"Jeroen Mostert" wrote:

> On 2010-04-21 21:05, bob wrote:
> > Is there a simpler way than a UNION to return a default value of a joined
> > table for which a corresponding row does not exist?
> >
> > The following example (not a working one, of course) illustrates what I'm
> > after. I'd like to return 'N/A' as c2name if there is no matching row in t2
> >
> > SELECT t1.c1
> >
> If you put a comma here...
>
> > (CASE t2.t1pk
> > WHEN NULL THEN 'N/A'
> > ELSE t2.name END) AS c2name,
> >
> > FROM t1
> >
> > LEFT JOIN t2 ON t2.t1pk = t1.pk
> >
> ....then your query is syntactically valid and gives the desired result.
>
> If you need something more complicated, you'll have to be more specific.
>
> --
> J.
> .
>
 >> Stay informed about: How to substitute for a non-existing column in a joined ta.. 
Back to top
Login to vote
Tom Cooper

External


Since: Mar 12, 2004
Posts: 8



(Msg. 4) Posted: Wed Apr 21, 2010 8:59 pm
Post subject: Re: How to substitute for a non-existing column in a joined table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Or just use the COALESCE function, e.g.,

SELECT t1.c1,
COALESCE( t2.t1pk, 'N/A') AS c2name,
FROM t1
LEFT JOIN t2 ON t2.t1pk = t1.pk

Tom

"bob" wrote in message

> Hi,
>
> Is there a simpler way than a UNION to return a default value of a joined
> table for which a corresponding row does not exist?
>
> The following example (not a working one, of course) illustrates what I'm
> after. I'd like to return 'N/A' as c2name if there is no matching row in
> t2
>
> SELECT t1.c1
>
> (CASE t2.t1pk
> WHEN NULL THEN 'N/A'
> ELSE t2.name END) AS c2name,
>
> FROM t1
>
> LEFT JOIN t2 ON t2.t1pk = t1.pk
>
> Thanks.
>
 >> Stay informed about: How to substitute for a non-existing column in a joined ta.. 
Back to top
Login to vote
Jeroen Mostert

External


Since: Oct 08, 2009
Posts: 8



(Msg. 5) Posted: Thu Apr 22, 2010 1:25 am
Post subject: Re: How to substitute for a non-existing column in a joined table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2010-04-21 22:52, Q wrote:
> When comparing to NULL value, you have to use IS NULL or IS NOT NULL.

Wow, I don't know how I managed to miss that.

--
J.
 >> Stay informed about: How to substitute for a non-existing column in a joined ta.. 
Back to top
Login to vote
bob

External


Since: Apr 21, 2010
Posts: 2



(Msg. 6) Posted: Thu Apr 22, 2010 12:32 pm
Post subject: Re: How to substitute for a non-existing column in a joined table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Jeroen Mostert" wrote in message

> On 2010-04-21 22:52, Q wrote:
>> When comparing to NULL value, you have to use IS NULL or IS NOT NULL.
>
> Wow, I don't know how I managed to miss that.
>
> --
> J.

Are you sure that 'IS NULL' works with CASE/WHEN? I know that it does with
WHERE, etc. but with CASE it seems to work without 'IS' only.
So, maybe you did not miss anything afterall Smile
 >> Stay informed about: How to substitute for a non-existing column in a joined ta.. 
Back to top
Login to vote
Jeroen Mostert

External


Since: Oct 08, 2009
Posts: 8



(Msg. 7) Posted: Thu Apr 22, 2010 4:25 pm
Post subject: Re: How to substitute for a non-existing column in a joined table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2010-04-22 18:32, bob wrote:
> "Jeroen Mostert" wrote in message
>
>> On 2010-04-21 22:52, Q wrote:
>>> When comparing to NULL value, you have to use IS NULL or IS NOT NULL.
>>
>> Wow, I don't know how I managed to miss that.
>>
> Are you sure that 'IS NULL' works with CASE/WHEN? I know that it does with
> WHERE, etc. but with CASE it seems to work without 'IS' only.
> So, maybe you did not miss anything afterall Smile
>
>
CASE has two forms, the first for matching a single expression against
multiple others, like so:

CASE x
WHEN A THEN x_A
WHEN B THEN x_B
...
END

This form definitely does not work for NULL, or rather, the WHEN clauses
behave as if you had written an equality comparison out in full. You can
easily test this yourself: setting ANSI_NULLS will affect the result just
like in (in)equality comparisons (if ANSI_NULLS is OFF, A = NULL will be
true, the intuitive but wrong result).

The other form evaluates multiple boolean expressions, like so:

CASE
WHEN x = A THEN x_A
WHEN x = B THEN x_B
...
END

The expressions obey the regular rules and thus require IS NULL if you want
to test for NULL. In fact, this is the only form you can use if you want to
test for NULL in a CASE.

--
J.
 >> Stay informed about: How to substitute for a non-existing column in a joined ta.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
How to Add Identity to Existing Column - Hi, I want to modify the column with Identity. But, i am getting the following error. ALTER TABLE <TableName> ALTER COLUMN <ColumnName> INT IDENTITY (10, 1) --Already 10 Values are inserted in Table. My Error: Msg 156, Level 15, State 1, L...

TSQL - self joined table - Hi all, I am working with the SSRS catalog database to create a report and got stuck on this query. The subquery is bringing back more than one row - so it errors out. Basically I want to return report names (Type=2) with the associated Parent ....

JOIN trouble (all from A, NULL from C, joined via B) - I have a problem I can't figure out. I have 3 tables A, B and C that join the following way: A 1:n B C 1:n B I'd like to show all rows from A and values from B with filter on C. Here some sql that illustrates the problem: -- CODE BEGIN use sandbo...

best way to get a count on a left joined table? - I'm trying to figure out some alternative ways to find some counts, so I can test them for performance. My example below shows what I'm talking about. (My real situation is based on real tables, not variable tables, but I'll use variable tables to show...

Cannot figure out a part of multi joined sql code - Hi, I have a view in sql server database where the sql statement is as follows (multi joined sql) SELECT PR.Project_Num AS ProjectNo, PR.Gen_Level, PR.Customer_ID, PR.User_ID, PR.Project_Name, PR.Project_Desc, PR.Date_Submitted, PR.Date_Due, ...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]