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
 .NET Inside SQL Server (2005)
 Union statement with case inside

Author  Topic 

function
Starting Member

12 Posts

Posted - 2009-11-23 : 07:53:46
Hi guys,
I have 2 tables as follows:

FIRST TABLE:
===============
firsttable_id, perigrafi_id (coming from another table), first_decode

SECOND TABLE:
===============
secondtable_id, perigrafi_id (the same as above), second_encode

All i want to do is a union statement as follows:

SELECT '' as [first_encode]
from firsttable where perigrafi_id=1
union
SELECT second_encode
from secondtable where perigrafi_id=1

But the result is like this:
=============================
0
1

instead of
'' (empty space quote)
1
How can i achieve the second result with a case statement?

Thanks in advance!

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-23 : 08:21:51
It is the formation issues.Just return as such but suppress in .NET

or

SELECT '' as [first_encode]
from firsttable where perigrafi_id=1
union
SELECT cast(second_encode as varchar(10))
from secondtable where perigrafi_id=1

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-11-23 : 08:30:45
Hi function

SELECT statement within the UNION must have the same number of columns and must also have the similar data types.

-------------------------
R...
Go to Top of Page

function
Starting Member

12 Posts

Posted - 2009-11-23 : 08:38:14
Thank you all guys. It worked like a charm!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-23 : 08:51:52
You may need to read this too
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/02/empty-string-and-default-values.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -