SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Display Multiple Fields in Case


Display Multiple Fields in Case

Author
Message
sharonsql2013
sharonsql2013
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1254 Visits: 1265
How can I display multiple fields in Case statement.
If I use a "," , it gives an error...


Declare @DateInput as Date = '2012-06-10'

Select
Case when @DateInput between '2012-06-06' and '2013-06-10'
then
AnniversaryStartDate
,AnniversaryEndDate
Else
Null
End
from dbo.AnniversaryData
where DateInput = @DateInput

Error Incorrect syntax near ','.
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41106 Visits: 19815
You can't. You need to repeat the CASE statement for every column needed or change the logic.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
sharonsql2013
sharonsql2013
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1254 Visits: 1265
Declare @DateInput as Date

Select
Case when @DateInput between '2012-06-06'and '2013-06-10'
Then
AnniversaryStartDate
when @DateInput between '2012-06-06'and '2013-06-10'
Then
AnniversaryEndDate
Else
Null
End as AnniversaryStartDate,AnniversaryEndDate
from dbo.AnniversaryData
where DateInput = @DateInput

This resolves the issue of multiple cases, but now get an error saying Invalid Column. I do not have a columnname dateInput in my Anniversary Data table. But I want to accept a date parameter and if it falls between the start and end anniversary dates , display the details. Any advise?
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41106 Visits: 19815
I don't understand completely your problem. Please read the article linked on my signature to guide you on your posts and get better help. If you give me DDL, some sample data and expected results based on the sample data, I can give you more help.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
sharonsql2013
sharonsql2013
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1254 Visits: 1265
Here is the script:


CREATE TABLE [dbo].[AnniversaryData](
[AnniversaryNumber] [varchar](10) NOT NULL,
[ARAmt] [decimal](24, 2) NULL,
[AnniversaryStartDate] [date] NULL,
[AnniversaryEndDate] [date] NULL,
[InvoiceDueDate] [date] NULL)
CONSTRAINT [AnniversaryData] PRIMARY KEY CLUSTERED
(
[AnniversaryNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO




Output must be : If I pass 10-10-2012 as input then it should check between Anniversarystartdate and
AnniversaryEndDate. If it falls in that then it should display that else Null
LinksUp
LinksUp
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1718 Visits: 4640
sharonsql2013 (9/27/2013)

Output must be : If I pass 10-10-2012 as input then it should check between Anniversarystartdate and
AnniversaryEndDate. If it falls in that then it should display that else Null


I'm taking a shot in the dark here. But is this what you are looking for?


declare @DateInput Date = '2012-10-10'
select anniversaryNumber,
case when @DateInput between AnniversaryStartDate and AnniversaryEndDate then AnniversaryStartDate else null end StartDate,
case when @DateInput between AnniversaryStartDate and AnniversaryEndDate then AnniversaryEndDate else null end EndDate
from AnniversaryData



__________________________________________________________________________________________________________
How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search