Case When in Where Clause concerning DATE The Next CEO of Stack OverflowSQL: IF clause within WHERE clauseHow to return only the Date from a SQL Server DateTime datatypeParameterize an SQL IN clauseSQL join: where clause vs. on clauseINNER JOIN ON vs WHERE clauseWhen should I use cross apply over inner join?T-SQL CASE Clause: How to specify WHEN NULLSQL Server: CASE WHEN OR THEN ELSE END => the OR is not supported“IN” condition at CASE WHEN on WHERE CLAUSE?SQL Server query that determined oldest shipped date vs current inventory level
What does "Its cash flow is deeply negative" mean?
Contours of a clandestine nature
Why does standard notation not preserve intervals (visually)
Unreliable Magic - Is it worth it?
How did people program for Consoles with multiple CPUs?
Is "for causing autism in X" grammatical?
Why do professional authors make "consistency" mistakes? And how to avoid them?
Rotate a column
Are there any limitations on attacking while grappling?
Can you replace a racial trait cantrip when leveling up?
Can we say or write : "No, it'sn't"?
Elegant way to replace substring in a regex with optional groups in Python?
Is there a difference between "Fahrstuhl" and "Aufzug"
How to prevent changing the value of variable?
MessageLevel in QGIS3
Why has the US not been more assertive in confronting Russia in recent years?
Why do remote companies require working in the US?
How to safely derail a train during transit?
If a black hole is created from light, can this black hole then move at speed of light?
Complex fractions
How does the Z80 determine which peripheral sent an interrupt?
Should I tutor a student who I know has cheated on their homework?
Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis
Bold, vivid family
Case When in Where Clause concerning DATE
The Next CEO of Stack OverflowSQL: IF clause within WHERE clauseHow to return only the Date from a SQL Server DateTime datatypeParameterize an SQL IN clauseSQL join: where clause vs. on clauseINNER JOIN ON vs WHERE clauseWhen should I use cross apply over inner join?T-SQL CASE Clause: How to specify WHEN NULLSQL Server: CASE WHEN OR THEN ELSE END => the OR is not supported“IN” condition at CASE WHEN on WHERE CLAUSE?SQL Server query that determined oldest shipped date vs current inventory level
Hi I am trying to create a report that doesn't take into consideration weekends. Basically today plus 2 days excluding weekends. But I am having issues with it.
Where [Location Code]='MO3TRAILS'
and
Case when datepart(weekday,[Shipment Date]+2)='1'then [Shipment Date]+4
else
Case when datepart(weekday,[Shipment Date]+2)= '7' then [Shipment Date]+4
else
[Shipment Date]+2 end end
I keep getting "An expression of non-boolean type specified in a context where a condition is expected, near 'end'
" as an error message.
Plan on adding this in order to filter a report in visual studio for building orders. based on today. Here is the whole current query:
SELECT CASE WHEN h.[Sent To WMS] = '1' THEN 'YES' ELSE 'NO' END AS
'Ready to Ship'
, h.[Internal Comment]
, CASE WHEN h.[Customer Price Group] <> 'INTERNAL' AND l.[No_]
LIKE 'P-%' THEN l.[Gen_ Prod_ Posting Group] ELSE
CASE WHEN l.[No_] LIKE 'H-%' THEN l.[Gen_ Prod_ Posting Group]
ELSE '' END END AS 'POS/Repack'
, h.No_
, h.[Ship-to Name]
, h.[Bill-to Name]
, l.[Location Code]
, h.[Pool Load]
, h.[Shipping Agent Code]
, l.No_ AS 'SKU'
, CAST(ROUND(l.[Quantity (Base)], 0) AS INT) AS 'QTY'
, CAST(ROUND(m.[Qty_ per Unit of Measure], 0) AS INT) AS 'ITEMS
PER PALLET'
, CAST(CONVERT(varchar, h.[Shipment Date], 101) AS date) AS 'Slot
Date'
, h.[Ship-to County], ROUND(l.[Gross Weight], 0) AS 'Weight'
, CASE WHEN l.[No_] LIKE 'F-00%' THEN FLOOR(l.[Quantity (Base)] /
m.[Qty_ per Unit of Measure]) ELSE 0 END AS [Full Pallet]
, CASE WHEN l.[No_] LIKE 'F-00%' THEN CAST(ROUND(l.[Quantity
(Base)], 0) AS INT)- FLOOR(l.[Quantity (Base)] / m.[Qty_ per Unit of
Measure]) * CAST(ROUND(m.[Qty_ per Unit of Measure], 0) AS INT) ELSE 0 END
AS Picks
, l.[Quantity (Base)] / m.[Qty_ per Unit of Measure] AS [Pallet
Spots]
FROM DATASERVER.[Dynamics NAV].dbo.[Sales Header] AS
h INNER JOIN
DATASERVER.[Dynamics NAV].dbo.[Sales Line] AS l ON
h.No_ = l.[Document No_] INNER JOIN
DATASERVER.[Dynamics NAV].dbo.[Item Unit of Measure]
AS m ON l.No_ = m.[Item No_]
WHERE (l.[Location Code] = 'MO3TRAILS')
AND (m.Code = 'PALLET')
AND (l.[Location Code] <> 'ECW')
AND (m.[Item No_] <> 'D-000006')
AND (m.[Item No_] <> 'D-000008')
ORDER BY 'Slot Date'
sql tsql where-clause case-when
|
show 3 more comments
Hi I am trying to create a report that doesn't take into consideration weekends. Basically today plus 2 days excluding weekends. But I am having issues with it.
Where [Location Code]='MO3TRAILS'
and
Case when datepart(weekday,[Shipment Date]+2)='1'then [Shipment Date]+4
else
Case when datepart(weekday,[Shipment Date]+2)= '7' then [Shipment Date]+4
else
[Shipment Date]+2 end end
I keep getting "An expression of non-boolean type specified in a context where a condition is expected, near 'end'
" as an error message.
Plan on adding this in order to filter a report in visual studio for building orders. based on today. Here is the whole current query:
SELECT CASE WHEN h.[Sent To WMS] = '1' THEN 'YES' ELSE 'NO' END AS
'Ready to Ship'
, h.[Internal Comment]
, CASE WHEN h.[Customer Price Group] <> 'INTERNAL' AND l.[No_]
LIKE 'P-%' THEN l.[Gen_ Prod_ Posting Group] ELSE
CASE WHEN l.[No_] LIKE 'H-%' THEN l.[Gen_ Prod_ Posting Group]
ELSE '' END END AS 'POS/Repack'
, h.No_
, h.[Ship-to Name]
, h.[Bill-to Name]
, l.[Location Code]
, h.[Pool Load]
, h.[Shipping Agent Code]
, l.No_ AS 'SKU'
, CAST(ROUND(l.[Quantity (Base)], 0) AS INT) AS 'QTY'
, CAST(ROUND(m.[Qty_ per Unit of Measure], 0) AS INT) AS 'ITEMS
PER PALLET'
, CAST(CONVERT(varchar, h.[Shipment Date], 101) AS date) AS 'Slot
Date'
, h.[Ship-to County], ROUND(l.[Gross Weight], 0) AS 'Weight'
, CASE WHEN l.[No_] LIKE 'F-00%' THEN FLOOR(l.[Quantity (Base)] /
m.[Qty_ per Unit of Measure]) ELSE 0 END AS [Full Pallet]
, CASE WHEN l.[No_] LIKE 'F-00%' THEN CAST(ROUND(l.[Quantity
(Base)], 0) AS INT)- FLOOR(l.[Quantity (Base)] / m.[Qty_ per Unit of
Measure]) * CAST(ROUND(m.[Qty_ per Unit of Measure], 0) AS INT) ELSE 0 END
AS Picks
, l.[Quantity (Base)] / m.[Qty_ per Unit of Measure] AS [Pallet
Spots]
FROM DATASERVER.[Dynamics NAV].dbo.[Sales Header] AS
h INNER JOIN
DATASERVER.[Dynamics NAV].dbo.[Sales Line] AS l ON
h.No_ = l.[Document No_] INNER JOIN
DATASERVER.[Dynamics NAV].dbo.[Item Unit of Measure]
AS m ON l.No_ = m.[Item No_]
WHERE (l.[Location Code] = 'MO3TRAILS')
AND (m.Code = 'PALLET')
AND (l.[Location Code] <> 'ECW')
AND (m.[Item No_] <> 'D-000006')
AND (m.[Item No_] <> 'D-000008')
ORDER BY 'Slot Date'
sql tsql where-clause case-when
2
Did you intend to compare the result ofCASE
with something else?
– PM 77-1
Mar 7 at 15:39
No. I only want the report to pull the [Ship Date]+2 for weekdays 2-4 and if weekdays are 5, or 6 [Ship Date]+4. I am excluding weekdays 7 & 1 because the report will not run on these days.
– beauwurtz84
Mar 7 at 15:53
1
Can you include the first part of the query aswell please. I think you want the case in the select part and not the where part.
– Armin
Mar 7 at 15:53
I included the real report I am running. I plan on adding the logic to it once I can get the CASE WHEN clause to work properly. I know putting it in the SELECT area will change the Ship Date to display either +2 or +4 but I want it to only show what I need to build in 2 days only excluding weekends.
– beauwurtz84
Mar 7 at 16:26
When you haveAND
it assumes that you have some conditions that resolve to Boolean values before and after. In your case on the right is Date.
– PM 77-1
Mar 7 at 17:03
|
show 3 more comments
Hi I am trying to create a report that doesn't take into consideration weekends. Basically today plus 2 days excluding weekends. But I am having issues with it.
Where [Location Code]='MO3TRAILS'
and
Case when datepart(weekday,[Shipment Date]+2)='1'then [Shipment Date]+4
else
Case when datepart(weekday,[Shipment Date]+2)= '7' then [Shipment Date]+4
else
[Shipment Date]+2 end end
I keep getting "An expression of non-boolean type specified in a context where a condition is expected, near 'end'
" as an error message.
Plan on adding this in order to filter a report in visual studio for building orders. based on today. Here is the whole current query:
SELECT CASE WHEN h.[Sent To WMS] = '1' THEN 'YES' ELSE 'NO' END AS
'Ready to Ship'
, h.[Internal Comment]
, CASE WHEN h.[Customer Price Group] <> 'INTERNAL' AND l.[No_]
LIKE 'P-%' THEN l.[Gen_ Prod_ Posting Group] ELSE
CASE WHEN l.[No_] LIKE 'H-%' THEN l.[Gen_ Prod_ Posting Group]
ELSE '' END END AS 'POS/Repack'
, h.No_
, h.[Ship-to Name]
, h.[Bill-to Name]
, l.[Location Code]
, h.[Pool Load]
, h.[Shipping Agent Code]
, l.No_ AS 'SKU'
, CAST(ROUND(l.[Quantity (Base)], 0) AS INT) AS 'QTY'
, CAST(ROUND(m.[Qty_ per Unit of Measure], 0) AS INT) AS 'ITEMS
PER PALLET'
, CAST(CONVERT(varchar, h.[Shipment Date], 101) AS date) AS 'Slot
Date'
, h.[Ship-to County], ROUND(l.[Gross Weight], 0) AS 'Weight'
, CASE WHEN l.[No_] LIKE 'F-00%' THEN FLOOR(l.[Quantity (Base)] /
m.[Qty_ per Unit of Measure]) ELSE 0 END AS [Full Pallet]
, CASE WHEN l.[No_] LIKE 'F-00%' THEN CAST(ROUND(l.[Quantity
(Base)], 0) AS INT)- FLOOR(l.[Quantity (Base)] / m.[Qty_ per Unit of
Measure]) * CAST(ROUND(m.[Qty_ per Unit of Measure], 0) AS INT) ELSE 0 END
AS Picks
, l.[Quantity (Base)] / m.[Qty_ per Unit of Measure] AS [Pallet
Spots]
FROM DATASERVER.[Dynamics NAV].dbo.[Sales Header] AS
h INNER JOIN
DATASERVER.[Dynamics NAV].dbo.[Sales Line] AS l ON
h.No_ = l.[Document No_] INNER JOIN
DATASERVER.[Dynamics NAV].dbo.[Item Unit of Measure]
AS m ON l.No_ = m.[Item No_]
WHERE (l.[Location Code] = 'MO3TRAILS')
AND (m.Code = 'PALLET')
AND (l.[Location Code] <> 'ECW')
AND (m.[Item No_] <> 'D-000006')
AND (m.[Item No_] <> 'D-000008')
ORDER BY 'Slot Date'
sql tsql where-clause case-when
Hi I am trying to create a report that doesn't take into consideration weekends. Basically today plus 2 days excluding weekends. But I am having issues with it.
Where [Location Code]='MO3TRAILS'
and
Case when datepart(weekday,[Shipment Date]+2)='1'then [Shipment Date]+4
else
Case when datepart(weekday,[Shipment Date]+2)= '7' then [Shipment Date]+4
else
[Shipment Date]+2 end end
I keep getting "An expression of non-boolean type specified in a context where a condition is expected, near 'end'
" as an error message.
Plan on adding this in order to filter a report in visual studio for building orders. based on today. Here is the whole current query:
SELECT CASE WHEN h.[Sent To WMS] = '1' THEN 'YES' ELSE 'NO' END AS
'Ready to Ship'
, h.[Internal Comment]
, CASE WHEN h.[Customer Price Group] <> 'INTERNAL' AND l.[No_]
LIKE 'P-%' THEN l.[Gen_ Prod_ Posting Group] ELSE
CASE WHEN l.[No_] LIKE 'H-%' THEN l.[Gen_ Prod_ Posting Group]
ELSE '' END END AS 'POS/Repack'
, h.No_
, h.[Ship-to Name]
, h.[Bill-to Name]
, l.[Location Code]
, h.[Pool Load]
, h.[Shipping Agent Code]
, l.No_ AS 'SKU'
, CAST(ROUND(l.[Quantity (Base)], 0) AS INT) AS 'QTY'
, CAST(ROUND(m.[Qty_ per Unit of Measure], 0) AS INT) AS 'ITEMS
PER PALLET'
, CAST(CONVERT(varchar, h.[Shipment Date], 101) AS date) AS 'Slot
Date'
, h.[Ship-to County], ROUND(l.[Gross Weight], 0) AS 'Weight'
, CASE WHEN l.[No_] LIKE 'F-00%' THEN FLOOR(l.[Quantity (Base)] /
m.[Qty_ per Unit of Measure]) ELSE 0 END AS [Full Pallet]
, CASE WHEN l.[No_] LIKE 'F-00%' THEN CAST(ROUND(l.[Quantity
(Base)], 0) AS INT)- FLOOR(l.[Quantity (Base)] / m.[Qty_ per Unit of
Measure]) * CAST(ROUND(m.[Qty_ per Unit of Measure], 0) AS INT) ELSE 0 END
AS Picks
, l.[Quantity (Base)] / m.[Qty_ per Unit of Measure] AS [Pallet
Spots]
FROM DATASERVER.[Dynamics NAV].dbo.[Sales Header] AS
h INNER JOIN
DATASERVER.[Dynamics NAV].dbo.[Sales Line] AS l ON
h.No_ = l.[Document No_] INNER JOIN
DATASERVER.[Dynamics NAV].dbo.[Item Unit of Measure]
AS m ON l.No_ = m.[Item No_]
WHERE (l.[Location Code] = 'MO3TRAILS')
AND (m.Code = 'PALLET')
AND (l.[Location Code] <> 'ECW')
AND (m.[Item No_] <> 'D-000006')
AND (m.[Item No_] <> 'D-000008')
ORDER BY 'Slot Date'
sql tsql where-clause case-when
sql tsql where-clause case-when
edited Mar 8 at 16:41
a_horse_with_no_name
306k46468566
306k46468566
asked Mar 7 at 15:34
beauwurtz84beauwurtz84
33
33
2
Did you intend to compare the result ofCASE
with something else?
– PM 77-1
Mar 7 at 15:39
No. I only want the report to pull the [Ship Date]+2 for weekdays 2-4 and if weekdays are 5, or 6 [Ship Date]+4. I am excluding weekdays 7 & 1 because the report will not run on these days.
– beauwurtz84
Mar 7 at 15:53
1
Can you include the first part of the query aswell please. I think you want the case in the select part and not the where part.
– Armin
Mar 7 at 15:53
I included the real report I am running. I plan on adding the logic to it once I can get the CASE WHEN clause to work properly. I know putting it in the SELECT area will change the Ship Date to display either +2 or +4 but I want it to only show what I need to build in 2 days only excluding weekends.
– beauwurtz84
Mar 7 at 16:26
When you haveAND
it assumes that you have some conditions that resolve to Boolean values before and after. In your case on the right is Date.
– PM 77-1
Mar 7 at 17:03
|
show 3 more comments
2
Did you intend to compare the result ofCASE
with something else?
– PM 77-1
Mar 7 at 15:39
No. I only want the report to pull the [Ship Date]+2 for weekdays 2-4 and if weekdays are 5, or 6 [Ship Date]+4. I am excluding weekdays 7 & 1 because the report will not run on these days.
– beauwurtz84
Mar 7 at 15:53
1
Can you include the first part of the query aswell please. I think you want the case in the select part and not the where part.
– Armin
Mar 7 at 15:53
I included the real report I am running. I plan on adding the logic to it once I can get the CASE WHEN clause to work properly. I know putting it in the SELECT area will change the Ship Date to display either +2 or +4 but I want it to only show what I need to build in 2 days only excluding weekends.
– beauwurtz84
Mar 7 at 16:26
When you haveAND
it assumes that you have some conditions that resolve to Boolean values before and after. In your case on the right is Date.
– PM 77-1
Mar 7 at 17:03
2
2
Did you intend to compare the result of
CASE
with something else?– PM 77-1
Mar 7 at 15:39
Did you intend to compare the result of
CASE
with something else?– PM 77-1
Mar 7 at 15:39
No. I only want the report to pull the [Ship Date]+2 for weekdays 2-4 and if weekdays are 5, or 6 [Ship Date]+4. I am excluding weekdays 7 & 1 because the report will not run on these days.
– beauwurtz84
Mar 7 at 15:53
No. I only want the report to pull the [Ship Date]+2 for weekdays 2-4 and if weekdays are 5, or 6 [Ship Date]+4. I am excluding weekdays 7 & 1 because the report will not run on these days.
– beauwurtz84
Mar 7 at 15:53
1
1
Can you include the first part of the query aswell please. I think you want the case in the select part and not the where part.
– Armin
Mar 7 at 15:53
Can you include the first part of the query aswell please. I think you want the case in the select part and not the where part.
– Armin
Mar 7 at 15:53
I included the real report I am running. I plan on adding the logic to it once I can get the CASE WHEN clause to work properly. I know putting it in the SELECT area will change the Ship Date to display either +2 or +4 but I want it to only show what I need to build in 2 days only excluding weekends.
– beauwurtz84
Mar 7 at 16:26
I included the real report I am running. I plan on adding the logic to it once I can get the CASE WHEN clause to work properly. I know putting it in the SELECT area will change the Ship Date to display either +2 or +4 but I want it to only show what I need to build in 2 days only excluding weekends.
– beauwurtz84
Mar 7 at 16:26
When you have
AND
it assumes that you have some conditions that resolve to Boolean values before and after. In your case on the right is Date.– PM 77-1
Mar 7 at 17:03
When you have
AND
it assumes that you have some conditions that resolve to Boolean values before and after. In your case on the right is Date.– PM 77-1
Mar 7 at 17:03
|
show 3 more comments
3 Answers
3
active
oldest
votes
This is what I think you want to achieve. When today plus 2 days is a weekend then you want the entries with shipment date in 4 days and if today plus 2 days is a weekday you want the entries with shipment date in 2 days. If I am correct, the following Where statement should do the trick.
WHERE
[Shipment Date] =
case when datepart(weekday,getdate() + 2) in (1,7) then getdate() + 4
else getdate() + 2 end
Ok. I tried this but it doesn't like the '=' signs of [Shipment Date] = getdate() +4. It says incorrect syntax near '='
– beauwurtz84
Mar 8 at 16:48
I edited it now it should work.
– Armin
Mar 8 at 19:22
add a comment |
This code is part of a WHERE
clause, meaning everything here must resolve to a boolean "yes" or "no" result.
CASE
statements create values. However, the SQL language does not have real boolean values; it only has comparison operators. Therefore, CASE
by itself cannot produce a complete boolean expression; it can only produce the value used on one side of a comparison operator.
For this question, we have code structured like so:
WHERE column = value AND CASE
As just shown, this is not value. The result of the CASE
expression must compare to something, e.g.:
WHERE column = value AND CASE < column/value
Additionally, in producing those values, a CASE expression can have more than one WHEN
section. There is no need to nest multiple CASE expressions here. Even better, us an IN()
clause, then the first part of each WHEN
is the same and the latter part is constant.
add a comment |
OK. So I finally got this sorted out to work properly using this:
WHERE h.[Shipment Date]= CASE WHEN datepart(weekday,GETDATE()+2)IN (1) THEN (datediff(DAY,-4,getdate()))
ELSE CASE WHEN datepart(weekday,GETDATE()+2)IN (7) THEN (datediff(DAY,-4,getdate())) ELSE (datediff(DAY,-2,getdate())) END END
Using this returned all rows that have a h.[Shipment Date] of 3/12/19 which is = getdate()+4.
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%2f55047522%2fcase-when-in-where-clause-concerning-date%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is what I think you want to achieve. When today plus 2 days is a weekend then you want the entries with shipment date in 4 days and if today plus 2 days is a weekday you want the entries with shipment date in 2 days. If I am correct, the following Where statement should do the trick.
WHERE
[Shipment Date] =
case when datepart(weekday,getdate() + 2) in (1,7) then getdate() + 4
else getdate() + 2 end
Ok. I tried this but it doesn't like the '=' signs of [Shipment Date] = getdate() +4. It says incorrect syntax near '='
– beauwurtz84
Mar 8 at 16:48
I edited it now it should work.
– Armin
Mar 8 at 19:22
add a comment |
This is what I think you want to achieve. When today plus 2 days is a weekend then you want the entries with shipment date in 4 days and if today plus 2 days is a weekday you want the entries with shipment date in 2 days. If I am correct, the following Where statement should do the trick.
WHERE
[Shipment Date] =
case when datepart(weekday,getdate() + 2) in (1,7) then getdate() + 4
else getdate() + 2 end
Ok. I tried this but it doesn't like the '=' signs of [Shipment Date] = getdate() +4. It says incorrect syntax near '='
– beauwurtz84
Mar 8 at 16:48
I edited it now it should work.
– Armin
Mar 8 at 19:22
add a comment |
This is what I think you want to achieve. When today plus 2 days is a weekend then you want the entries with shipment date in 4 days and if today plus 2 days is a weekday you want the entries with shipment date in 2 days. If I am correct, the following Where statement should do the trick.
WHERE
[Shipment Date] =
case when datepart(weekday,getdate() + 2) in (1,7) then getdate() + 4
else getdate() + 2 end
This is what I think you want to achieve. When today plus 2 days is a weekend then you want the entries with shipment date in 4 days and if today plus 2 days is a weekday you want the entries with shipment date in 2 days. If I am correct, the following Where statement should do the trick.
WHERE
[Shipment Date] =
case when datepart(weekday,getdate() + 2) in (1,7) then getdate() + 4
else getdate() + 2 end
edited Mar 8 at 19:20
answered Mar 8 at 16:18
ArminArmin
11115
11115
Ok. I tried this but it doesn't like the '=' signs of [Shipment Date] = getdate() +4. It says incorrect syntax near '='
– beauwurtz84
Mar 8 at 16:48
I edited it now it should work.
– Armin
Mar 8 at 19:22
add a comment |
Ok. I tried this but it doesn't like the '=' signs of [Shipment Date] = getdate() +4. It says incorrect syntax near '='
– beauwurtz84
Mar 8 at 16:48
I edited it now it should work.
– Armin
Mar 8 at 19:22
Ok. I tried this but it doesn't like the '=' signs of [Shipment Date] = getdate() +4. It says incorrect syntax near '='
– beauwurtz84
Mar 8 at 16:48
Ok. I tried this but it doesn't like the '=' signs of [Shipment Date] = getdate() +4. It says incorrect syntax near '='
– beauwurtz84
Mar 8 at 16:48
I edited it now it should work.
– Armin
Mar 8 at 19:22
I edited it now it should work.
– Armin
Mar 8 at 19:22
add a comment |
This code is part of a WHERE
clause, meaning everything here must resolve to a boolean "yes" or "no" result.
CASE
statements create values. However, the SQL language does not have real boolean values; it only has comparison operators. Therefore, CASE
by itself cannot produce a complete boolean expression; it can only produce the value used on one side of a comparison operator.
For this question, we have code structured like so:
WHERE column = value AND CASE
As just shown, this is not value. The result of the CASE
expression must compare to something, e.g.:
WHERE column = value AND CASE < column/value
Additionally, in producing those values, a CASE expression can have more than one WHEN
section. There is no need to nest multiple CASE expressions here. Even better, us an IN()
clause, then the first part of each WHEN
is the same and the latter part is constant.
add a comment |
This code is part of a WHERE
clause, meaning everything here must resolve to a boolean "yes" or "no" result.
CASE
statements create values. However, the SQL language does not have real boolean values; it only has comparison operators. Therefore, CASE
by itself cannot produce a complete boolean expression; it can only produce the value used on one side of a comparison operator.
For this question, we have code structured like so:
WHERE column = value AND CASE
As just shown, this is not value. The result of the CASE
expression must compare to something, e.g.:
WHERE column = value AND CASE < column/value
Additionally, in producing those values, a CASE expression can have more than one WHEN
section. There is no need to nest multiple CASE expressions here. Even better, us an IN()
clause, then the first part of each WHEN
is the same and the latter part is constant.
add a comment |
This code is part of a WHERE
clause, meaning everything here must resolve to a boolean "yes" or "no" result.
CASE
statements create values. However, the SQL language does not have real boolean values; it only has comparison operators. Therefore, CASE
by itself cannot produce a complete boolean expression; it can only produce the value used on one side of a comparison operator.
For this question, we have code structured like so:
WHERE column = value AND CASE
As just shown, this is not value. The result of the CASE
expression must compare to something, e.g.:
WHERE column = value AND CASE < column/value
Additionally, in producing those values, a CASE expression can have more than one WHEN
section. There is no need to nest multiple CASE expressions here. Even better, us an IN()
clause, then the first part of each WHEN
is the same and the latter part is constant.
This code is part of a WHERE
clause, meaning everything here must resolve to a boolean "yes" or "no" result.
CASE
statements create values. However, the SQL language does not have real boolean values; it only has comparison operators. Therefore, CASE
by itself cannot produce a complete boolean expression; it can only produce the value used on one side of a comparison operator.
For this question, we have code structured like so:
WHERE column = value AND CASE
As just shown, this is not value. The result of the CASE
expression must compare to something, e.g.:
WHERE column = value AND CASE < column/value
Additionally, in producing those values, a CASE expression can have more than one WHEN
section. There is no need to nest multiple CASE expressions here. Even better, us an IN()
clause, then the first part of each WHEN
is the same and the latter part is constant.
edited Mar 8 at 17:09
answered Mar 8 at 16:48
Joel CoehoornJoel Coehoorn
312k96497735
312k96497735
add a comment |
add a comment |
OK. So I finally got this sorted out to work properly using this:
WHERE h.[Shipment Date]= CASE WHEN datepart(weekday,GETDATE()+2)IN (1) THEN (datediff(DAY,-4,getdate()))
ELSE CASE WHEN datepart(weekday,GETDATE()+2)IN (7) THEN (datediff(DAY,-4,getdate())) ELSE (datediff(DAY,-2,getdate())) END END
Using this returned all rows that have a h.[Shipment Date] of 3/12/19 which is = getdate()+4.
add a comment |
OK. So I finally got this sorted out to work properly using this:
WHERE h.[Shipment Date]= CASE WHEN datepart(weekday,GETDATE()+2)IN (1) THEN (datediff(DAY,-4,getdate()))
ELSE CASE WHEN datepart(weekday,GETDATE()+2)IN (7) THEN (datediff(DAY,-4,getdate())) ELSE (datediff(DAY,-2,getdate())) END END
Using this returned all rows that have a h.[Shipment Date] of 3/12/19 which is = getdate()+4.
add a comment |
OK. So I finally got this sorted out to work properly using this:
WHERE h.[Shipment Date]= CASE WHEN datepart(weekday,GETDATE()+2)IN (1) THEN (datediff(DAY,-4,getdate()))
ELSE CASE WHEN datepart(weekday,GETDATE()+2)IN (7) THEN (datediff(DAY,-4,getdate())) ELSE (datediff(DAY,-2,getdate())) END END
Using this returned all rows that have a h.[Shipment Date] of 3/12/19 which is = getdate()+4.
OK. So I finally got this sorted out to work properly using this:
WHERE h.[Shipment Date]= CASE WHEN datepart(weekday,GETDATE()+2)IN (1) THEN (datediff(DAY,-4,getdate()))
ELSE CASE WHEN datepart(weekday,GETDATE()+2)IN (7) THEN (datediff(DAY,-4,getdate())) ELSE (datediff(DAY,-2,getdate())) END END
Using this returned all rows that have a h.[Shipment Date] of 3/12/19 which is = getdate()+4.
answered Mar 8 at 18:08
beauwurtz84beauwurtz84
33
33
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%2f55047522%2fcase-when-in-where-clause-concerning-date%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
2
Did you intend to compare the result of
CASE
with something else?– PM 77-1
Mar 7 at 15:39
No. I only want the report to pull the [Ship Date]+2 for weekdays 2-4 and if weekdays are 5, or 6 [Ship Date]+4. I am excluding weekdays 7 & 1 because the report will not run on these days.
– beauwurtz84
Mar 7 at 15:53
1
Can you include the first part of the query aswell please. I think you want the case in the select part and not the where part.
– Armin
Mar 7 at 15:53
I included the real report I am running. I plan on adding the logic to it once I can get the CASE WHEN clause to work properly. I know putting it in the SELECT area will change the Ship Date to display either +2 or +4 but I want it to only show what I need to build in 2 days only excluding weekends.
– beauwurtz84
Mar 7 at 16:26
When you have
AND
it assumes that you have some conditions that resolve to Boolean values before and after. In your case on the right is Date.– PM 77-1
Mar 7 at 17:03