Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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 Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 8:15 AM
Points: 229, Visits: 693
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


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 1:20 PM
Points: 3,618, Visits: 8,117
You can't. You need to repeat the CASE statement for every column needed or change the logic.



Luis C.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1499533
Posted Friday, September 27, 2013 12:42 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 8:15 AM
Points: 229, Visits: 693
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


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 1:20 PM
Points: 3,618, Visits: 8,117
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1499558
Posted Friday, September 27, 2013 1:21 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 8:15 AM
Points: 229, Visits: 693
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
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 10:48 AM
Points: 357, Visits: 1,474
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