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-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 2:07 PM
Points: 169, Visits: 506
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

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:43 PM
Points: 2,763, Visits: 5,907
You can't. You need to repeat the CASE statement for every column needed or change the logic.



Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

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-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 2:07 PM
Points: 169, Visits: 506
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

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:43 PM
Points: 2,763, Visits: 5,907
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

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-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 2:07 PM
Points: 169, Visits: 506
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: Yesterday @ 10:37 PM
Points: 319, Visits: 1,142
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