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 2000 Forums
 Transact-SQL (2000)
 CASE statement

Author  Topic 

parand
Starting Member

8 Posts

Posted - 2007-09-27 : 06:37:56
Let's say we have two tables and we want to retrieve two columns, one of each.
We have two tables A and B and we want to retrieve columns 1 from table A and column 2 from table B. But the value of column 1 depends on the value of the column 2, such that if the value held in column 2 is, say 'AAA' then the value of column 1 is the negative of column1 else it is the value in column 1.
Would anybody tell me how to do this. Following is the sample SQL:

SELECT A.1, B.2
FROM A, B
WHERE (If 2 = 'AAA' THEN 1 = -1 ELSE 1 = 1)

What do I write in the brackets??

Many thanks,

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-27 : 06:41:36
[code]SELECT CASE
WHEN b.[2] = 'AAA' THEN -a.[1]
ELSE a.[1]
END AS [1],
b.[2]
FROM a
INNER JOIN b ON b.x = a.x[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

parand
Starting Member

8 Posts

Posted - 2007-09-27 : 07:04:31
Peso,

Thanks very much. I ran it and it has done it now.
Thank you very very much.

Parand
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-09-28 : 02:43:27
If the real column names are 1, 2, etc, you should change them to proper column names

Madhivanan

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

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-28 : 03:43:20
Madhi, you of all people should know this!

He used your import from Excel technique
Hence column names [1], [2], etc...



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-09-28 : 03:58:17
quote:
Originally posted by Peso

Madhi, you of all people should know this!

He used your import from Excel technique
Hence column names [1], [2], etc...



E 12°55'05.25"
N 56°04'39.16"



Well. Then OP should have converted them to proper names if the table is going to be used by applications

Madhivanan

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

- Advertisement -