Adding a list of numbers to a query with CASE WHEN
Is it possible to add a list of numbers to this query below:
DECLARE @MONTHS NVARCHAR(10)
DECLARE @YEAR NVARCHAR(10)
BEGIN
SET @MONTHS = 'Q1'
SET @YEAR = '2017'
SELECT *
FROM netlication.dbo.iehResults
WHERE DATEPART(yyyy,COLLDATE) = CAST(@YEAR AS INT)
AND DATEPART(mm,collDate) IN (CASE @MONTHS WHEN 'Q1' THEN '1' End)
END
The query above works fine, but for the value Q1 I want to be able to pass a list of numbers 1,2 and 3
So I tried doing:
AND DATEPART(mm, collDate) IN (CASE @MONTHS WHEN 'Q1' THEN '1,2,3' End)
But I get a conversion error:
Msg 245, Level 16, State 1, Line 9
Conversion failed when converting the varchar value '1,2,3' to data type int.
sql sql-server tsql
add a comment |
Is it possible to add a list of numbers to this query below:
DECLARE @MONTHS NVARCHAR(10)
DECLARE @YEAR NVARCHAR(10)
BEGIN
SET @MONTHS = 'Q1'
SET @YEAR = '2017'
SELECT *
FROM netlication.dbo.iehResults
WHERE DATEPART(yyyy,COLLDATE) = CAST(@YEAR AS INT)
AND DATEPART(mm,collDate) IN (CASE @MONTHS WHEN 'Q1' THEN '1' End)
END
The query above works fine, but for the value Q1 I want to be able to pass a list of numbers 1,2 and 3
So I tried doing:
AND DATEPART(mm, collDate) IN (CASE @MONTHS WHEN 'Q1' THEN '1,2,3' End)
But I get a conversion error:
Msg 245, Level 16, State 1, Line 9
Conversion failed when converting the varchar value '1,2,3' to data type int.
sql sql-server tsql
1
Aside: Yourcase
expression is return a string ('1'
) rather than anint
(1
) whileDatePart
returns anint
. The mismatch in datatypes is resolved according to the rules for data type precedence. The best practice is to use the appropriate datatype.
– HABO
Nov 28 '18 at 22:20
add a comment |
Is it possible to add a list of numbers to this query below:
DECLARE @MONTHS NVARCHAR(10)
DECLARE @YEAR NVARCHAR(10)
BEGIN
SET @MONTHS = 'Q1'
SET @YEAR = '2017'
SELECT *
FROM netlication.dbo.iehResults
WHERE DATEPART(yyyy,COLLDATE) = CAST(@YEAR AS INT)
AND DATEPART(mm,collDate) IN (CASE @MONTHS WHEN 'Q1' THEN '1' End)
END
The query above works fine, but for the value Q1 I want to be able to pass a list of numbers 1,2 and 3
So I tried doing:
AND DATEPART(mm, collDate) IN (CASE @MONTHS WHEN 'Q1' THEN '1,2,3' End)
But I get a conversion error:
Msg 245, Level 16, State 1, Line 9
Conversion failed when converting the varchar value '1,2,3' to data type int.
sql sql-server tsql
Is it possible to add a list of numbers to this query below:
DECLARE @MONTHS NVARCHAR(10)
DECLARE @YEAR NVARCHAR(10)
BEGIN
SET @MONTHS = 'Q1'
SET @YEAR = '2017'
SELECT *
FROM netlication.dbo.iehResults
WHERE DATEPART(yyyy,COLLDATE) = CAST(@YEAR AS INT)
AND DATEPART(mm,collDate) IN (CASE @MONTHS WHEN 'Q1' THEN '1' End)
END
The query above works fine, but for the value Q1 I want to be able to pass a list of numbers 1,2 and 3
So I tried doing:
AND DATEPART(mm, collDate) IN (CASE @MONTHS WHEN 'Q1' THEN '1,2,3' End)
But I get a conversion error:
Msg 245, Level 16, State 1, Line 9
Conversion failed when converting the varchar value '1,2,3' to data type int.
sql sql-server tsql
sql sql-server tsql
edited Nov 28 '18 at 19:56
marc_s
583k13011241270
583k13011241270
asked Nov 28 '18 at 18:04
psj01psj01
1,0431430
1,0431430
1
Aside: Yourcase
expression is return a string ('1'
) rather than anint
(1
) whileDatePart
returns anint
. The mismatch in datatypes is resolved according to the rules for data type precedence. The best practice is to use the appropriate datatype.
– HABO
Nov 28 '18 at 22:20
add a comment |
1
Aside: Yourcase
expression is return a string ('1'
) rather than anint
(1
) whileDatePart
returns anint
. The mismatch in datatypes is resolved according to the rules for data type precedence. The best practice is to use the appropriate datatype.
– HABO
Nov 28 '18 at 22:20
1
1
Aside: Your
case
expression is return a string ('1'
) rather than an int
(1
) while DatePart
returns an int
. The mismatch in datatypes is resolved according to the rules for data type precedence. The best practice is to use the appropriate datatype.– HABO
Nov 28 '18 at 22:20
Aside: Your
case
expression is return a string ('1'
) rather than an int
(1
) while DatePart
returns an int
. The mismatch in datatypes is resolved according to the rules for data type precedence. The best practice is to use the appropriate datatype.– HABO
Nov 28 '18 at 22:20
add a comment |
2 Answers
2
active
oldest
votes
Instead of DATEPART(mm,collDate)
you can use DATEPART(q,collDate)
which will return the quarter and you won't need to compare with 3 values. But to compare, you must use between
and 2 case
statements.
DECLARE @MONTHS NVARCHAR(10)
DECLARE @YEAR NVARCHAR(10)
BEGIN
SET @MONTHS = 'Q1'
set @YEAR = '2017'
SELECT * FROM netlication.dbo.iehResults
WHERE DATEPART(yyyy,COLLDATE) = CAST(@YEAR AS INT)
AND DATEPART(q,collDate) = (CASE @MONTHS WHEN 'Q1' THEN 1 WHEN 'Q2' THEN 2 WHEN 'Q3' THEN 3 WHEN 'Q4' THEN 4 End)
END
However, performance wise it will be better to calculate the start and end dates of the quarter and then to filter by COLLDATE between them:
set @StartDate = DATEFROMPARTS(@YAR, CASE @MONTHS
WHEN 'Q1' THEN 1
WHEN 'Q2' THEN 4
WHEN 'Q3' THEN 7
WHEN 'Q4' THEN 10 End, 1)
set @EndDate = DATEADD(q, 1, @StartDate)
SELECT * FROM netlication.dbo.iehResults
WHERE COLLDATE >= @StartDate and COLLDATE < @EndDate
add a comment |
AND DATEPART(mm,collDate) IN (SELECT 1 WHERE @MONTHS = 'Q1'
UNION
SELECT 2 WHERE @MONTHS = 'Q1'
UNION
SELECT 3 WHERE @MONTHS = 'Q1')
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53525518%2fadding-a-list-of-numbers-to-a-query-with-case-when%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of DATEPART(mm,collDate)
you can use DATEPART(q,collDate)
which will return the quarter and you won't need to compare with 3 values. But to compare, you must use between
and 2 case
statements.
DECLARE @MONTHS NVARCHAR(10)
DECLARE @YEAR NVARCHAR(10)
BEGIN
SET @MONTHS = 'Q1'
set @YEAR = '2017'
SELECT * FROM netlication.dbo.iehResults
WHERE DATEPART(yyyy,COLLDATE) = CAST(@YEAR AS INT)
AND DATEPART(q,collDate) = (CASE @MONTHS WHEN 'Q1' THEN 1 WHEN 'Q2' THEN 2 WHEN 'Q3' THEN 3 WHEN 'Q4' THEN 4 End)
END
However, performance wise it will be better to calculate the start and end dates of the quarter and then to filter by COLLDATE between them:
set @StartDate = DATEFROMPARTS(@YAR, CASE @MONTHS
WHEN 'Q1' THEN 1
WHEN 'Q2' THEN 4
WHEN 'Q3' THEN 7
WHEN 'Q4' THEN 10 End, 1)
set @EndDate = DATEADD(q, 1, @StartDate)
SELECT * FROM netlication.dbo.iehResults
WHERE COLLDATE >= @StartDate and COLLDATE < @EndDate
add a comment |
Instead of DATEPART(mm,collDate)
you can use DATEPART(q,collDate)
which will return the quarter and you won't need to compare with 3 values. But to compare, you must use between
and 2 case
statements.
DECLARE @MONTHS NVARCHAR(10)
DECLARE @YEAR NVARCHAR(10)
BEGIN
SET @MONTHS = 'Q1'
set @YEAR = '2017'
SELECT * FROM netlication.dbo.iehResults
WHERE DATEPART(yyyy,COLLDATE) = CAST(@YEAR AS INT)
AND DATEPART(q,collDate) = (CASE @MONTHS WHEN 'Q1' THEN 1 WHEN 'Q2' THEN 2 WHEN 'Q3' THEN 3 WHEN 'Q4' THEN 4 End)
END
However, performance wise it will be better to calculate the start and end dates of the quarter and then to filter by COLLDATE between them:
set @StartDate = DATEFROMPARTS(@YAR, CASE @MONTHS
WHEN 'Q1' THEN 1
WHEN 'Q2' THEN 4
WHEN 'Q3' THEN 7
WHEN 'Q4' THEN 10 End, 1)
set @EndDate = DATEADD(q, 1, @StartDate)
SELECT * FROM netlication.dbo.iehResults
WHERE COLLDATE >= @StartDate and COLLDATE < @EndDate
add a comment |
Instead of DATEPART(mm,collDate)
you can use DATEPART(q,collDate)
which will return the quarter and you won't need to compare with 3 values. But to compare, you must use between
and 2 case
statements.
DECLARE @MONTHS NVARCHAR(10)
DECLARE @YEAR NVARCHAR(10)
BEGIN
SET @MONTHS = 'Q1'
set @YEAR = '2017'
SELECT * FROM netlication.dbo.iehResults
WHERE DATEPART(yyyy,COLLDATE) = CAST(@YEAR AS INT)
AND DATEPART(q,collDate) = (CASE @MONTHS WHEN 'Q1' THEN 1 WHEN 'Q2' THEN 2 WHEN 'Q3' THEN 3 WHEN 'Q4' THEN 4 End)
END
However, performance wise it will be better to calculate the start and end dates of the quarter and then to filter by COLLDATE between them:
set @StartDate = DATEFROMPARTS(@YAR, CASE @MONTHS
WHEN 'Q1' THEN 1
WHEN 'Q2' THEN 4
WHEN 'Q3' THEN 7
WHEN 'Q4' THEN 10 End, 1)
set @EndDate = DATEADD(q, 1, @StartDate)
SELECT * FROM netlication.dbo.iehResults
WHERE COLLDATE >= @StartDate and COLLDATE < @EndDate
Instead of DATEPART(mm,collDate)
you can use DATEPART(q,collDate)
which will return the quarter and you won't need to compare with 3 values. But to compare, you must use between
and 2 case
statements.
DECLARE @MONTHS NVARCHAR(10)
DECLARE @YEAR NVARCHAR(10)
BEGIN
SET @MONTHS = 'Q1'
set @YEAR = '2017'
SELECT * FROM netlication.dbo.iehResults
WHERE DATEPART(yyyy,COLLDATE) = CAST(@YEAR AS INT)
AND DATEPART(q,collDate) = (CASE @MONTHS WHEN 'Q1' THEN 1 WHEN 'Q2' THEN 2 WHEN 'Q3' THEN 3 WHEN 'Q4' THEN 4 End)
END
However, performance wise it will be better to calculate the start and end dates of the quarter and then to filter by COLLDATE between them:
set @StartDate = DATEFROMPARTS(@YAR, CASE @MONTHS
WHEN 'Q1' THEN 1
WHEN 'Q2' THEN 4
WHEN 'Q3' THEN 7
WHEN 'Q4' THEN 10 End, 1)
set @EndDate = DATEADD(q, 1, @StartDate)
SELECT * FROM netlication.dbo.iehResults
WHERE COLLDATE >= @StartDate and COLLDATE < @EndDate
edited Nov 28 '18 at 18:14
answered Nov 28 '18 at 18:08
Andrey NikolovAndrey Nikolov
4,3733922
4,3733922
add a comment |
add a comment |
AND DATEPART(mm,collDate) IN (SELECT 1 WHERE @MONTHS = 'Q1'
UNION
SELECT 2 WHERE @MONTHS = 'Q1'
UNION
SELECT 3 WHERE @MONTHS = 'Q1')
add a comment |
AND DATEPART(mm,collDate) IN (SELECT 1 WHERE @MONTHS = 'Q1'
UNION
SELECT 2 WHERE @MONTHS = 'Q1'
UNION
SELECT 3 WHERE @MONTHS = 'Q1')
add a comment |
AND DATEPART(mm,collDate) IN (SELECT 1 WHERE @MONTHS = 'Q1'
UNION
SELECT 2 WHERE @MONTHS = 'Q1'
UNION
SELECT 3 WHERE @MONTHS = 'Q1')
AND DATEPART(mm,collDate) IN (SELECT 1 WHERE @MONTHS = 'Q1'
UNION
SELECT 2 WHERE @MONTHS = 'Q1'
UNION
SELECT 3 WHERE @MONTHS = 'Q1')
answered Nov 28 '18 at 18:10
dcpdcp
43.3k16119147
43.3k16119147
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53525518%2fadding-a-list-of-numbers-to-a-query-with-case-when%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Aside: Your
case
expression is return a string ('1'
) rather than anint
(1
) whileDatePart
returns anint
. The mismatch in datatypes is resolved according to the rules for data type precedence. The best practice is to use the appropriate datatype.– HABO
Nov 28 '18 at 22:20