SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to return column based on the input parameter
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

RiyaSen845
Starting Member

3 Posts

Posted - 03/21/2013 :  01:13:52  Show Profile  Reply with Quote
Hi,

I have 5 columns contained in the same table

1: C1CODE
2: C2CODE
3: NAME
4: DATE
5: STATUS

and passing the input parameter @DatabaseName in sp.

- Admin
- Client

If I pass 'Admin' , then query should return the following columns

1: C1CODE
3: NAME
4: DATE
5: STATUS

If I pass 'ClientJob' , then query should return the following columns

2: C2CODE
3: NAME
4: DATE
5: STATUS

Can anyone help me out on this?

bandi
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 03/21/2013 :  01:34:05  Show Profile  Reply with Quote

DECLARE @DatabaseName VARCHAR(20) = 'Admin'

SELECT CASE @DatabaseName WHEN 'Admin' THEN C1CODE WHEN 'ClientJob' THEN C2CODE END As CodeByInputParam , NAME, DATE, STATUS
  FROM TableName

--Alternate 
IF @DatabaseName = 'Admin'
	SELECT C1CODE, NAME, DATE, STATUS
	FROM TableName
ELSE IF @DatabaseName = 'ClientJob'
	SELECT C2CODE, NAME, DATE, STATUS
	FROM TableName


--
Chandu

Edited by - bandi on 03/21/2013 01:35:11
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 03/21/2013 :  02:07:55  Show Profile  Reply with Quote
no need of IF. you could just do

SELECT CASE  @DatabaseName WHEN 'Admin' THEN C1CODE WHEN 'ClientJob' THEN C2CODE END, NAME, DATE, STATUS
	FROM TableName


I hope C1CODE AND C2CODE are of compatible datatypes.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 03/21/2013 :  02:12:21  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

no need of IF. you could just do

SELECT CASE  @DatabaseName WHEN 'Admin' THEN C1CODE WHEN 'ClientJob' THEN C2CODE END, NAME, DATE, STATUS
	FROM TableName

I hope C1CODE AND C2CODE are of compatible datatypes.


Yes visakh.. thats why i have given two possible solutions for OP

--
Chandu
Go to Top of Page

RiyaSen845
Starting Member

3 Posts

Posted - 03/21/2013 :  18:55:12  Show Profile  Reply with Quote
Thanks guys, It worked!!

I also want to hide the column if value of C1CODE is NULL and display only NAME,DATE and STATUS.

Any idea on this?
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 03/22/2013 :  00:57:27  Show Profile  Reply with Quote
Means all rows for C1CODE is NULL then you want to hide that column from display....?

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 03/22/2013 :  01:00:27  Show Profile  Reply with Quote
quote:
Originally posted by RiyaSen845

Thanks guys, It worked!!

I also want to hide the column if value of C1CODE is NULL and display only NAME,DATE and STATUS.

Any idea on this?


Thats a presentation requirement and has to be dealt at your front end application.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000