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
 Transact-SQL (2005)
 How to merge 2 columns in a request?

Author  Topic 

lurked
Starting Member

6 Posts

Posted - 2009-04-06 : 15:45:59
Hello guys,
It's my first time posting here, but I used these forums for some problems I had, already. Lets hope that this question will be answered also!

So here is the problem:

I need to create a view from a few different tables.
quote:

Lets say I need to create a view from these 2 tables:

table1 table2
Code Code_Int Value | Code Code_Int Name
1 ACZ01 va1 | 1 NULL na1
2 NULL va2 | 2 AH801 na2
3 EG702 va3 | 3 NULL na3
4 NULL va4 | 4 224DN na4


And from these tables, I need to create a view like this :

view1
Code Code_Int Value Name
1 ACZ01 va1 na1
2 AH801 va2 na2
3 EG702 va3 na3
4 224DN va4 na4



So, if you look well at the column Code_Int for the 2 tables, some of the codes are NULL.

What I need is a bit hard to explain :
view1.Code_Int comes from table1, but if table1.Code_Int is NULL, view1.CodeInt needs to be table2.Code_Int
(I hope that makes sense for you)
-

So yeah, I need to know how to create a view that will use the Code_Int from the correct table, the one where it isn't NULL.

Thanks for your help, and ask if you need any additionnal details!

- Frank


Edit: Fixed some mistakes... English is not my main language, sorry if it's hard to understand

sriv
Starting Member

11 Posts

Posted - 2009-04-06 : 15:54:01
Create view dbo.view1
As
Select a.Code,
IsNull(a.Code_Int, b.Code_Int) Code_Int,
a.value,
b.Name
From table1 a
Join table2 b on a.Code = b.Code
Go
Go to Top of Page

lurked
Starting Member

6 Posts

Posted - 2009-04-06 : 16:10:05
quote:
Originally posted by sriv

Create view dbo.view1
As
Select a.Code,
IsNull(a.Code_Int, b.Code_Int) Code_Int,
a.value,
b.Name
From table1 a
Join table2 b on a.Code = b.Code
Go



It works perfectly, thanks a lot for the fast answer!
(Seriously, omg... 8minutes for the right answer, after I wasted 1 hour googling...)

Thanks again! :D
Go to Top of Page
   

- Advertisement -