Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Display Multiple Fields in Case Expand / Collapse
Author
Message
Posted Friday, September 27, 2013 11:23 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, December 5, 2016 9:48 AM
Points: 406, Visits: 1,219
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 ','.




Post #1499525
Posted Friday, September 27, 2013 11:57 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 1:48 PM
Points: 8,233, Visits: 17,809
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
Post #1499533
Posted Friday, September 27, 2013 12:42 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, December 5, 2016 9:48 AM
Points: 406, Visits: 1,219
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?
Post #1499550
Posted Friday, September 27, 2013 12:59 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 1:48 PM
Points: 8,233, Visits: 17,809
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
Post #1499558
Posted Friday, September 27, 2013 1:21 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, December 5, 2016 9:48 AM
Points: 406, Visits: 1,219
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

Post #1499564
Posted Friday, September 27, 2013 2:37 PM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Yesterday @ 7:24 PM
Points: 693, Visits: 3,802
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/
Post #1499589
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse