where case when sql










0















I have a sales table



I wish to do the following select the data date that is available if the top date SalePeriodFrom = 20181101



(if the top value SalePeriodFrom 01/11/2018 - first of the month data is available)



if not take the data from the day before (last day of the previous month 31/10/2018)



the code I have is



Select *
from Sales

Where case when SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) then

(SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)
and (SalePeriodTo > '20010101' or
SalePeriodTo is null))

else (SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE())
and (SalePeriodTo > '20010101' or
SalePeriodTo is null)) end


I can't seem to get this to work, any ideas please team










share|improve this question
























  • You're trying to use a CASE expression as a statement. A CASE expression returns a scalar value (that needs to be compared to another expression in the WHERE to create a Boolean result). A CASE expression doesn't return a Boolean result on its own. For example CASE ColumnA WHEN 'A' THEN 1 ELSE 2 END = 2;

    – Larnu
    Nov 15 '18 at 11:31












  • T-SQL does not have Boolean values. It has Boolean expressions, but these are only valid in particular contexts. And yes, this is annoying. You can have your CASE return a 1 or 0 and check on that, or split the query in two and use a UNION [ALL] to combine the results, or figure out universal expressions to compare SalePeriodFrom and SalePeriodTo to (I can't immediately see what those would be).

    – Jeroen Mostert
    Nov 15 '18 at 11:34






  • 1





    I can't figure out what you want to accomplish. Can you provide sample data and desired results?

    – Gordon Linoff
    Nov 15 '18 at 11:36











  • It's generally better to use AND/OR constructions instead of case expressions in the WHERE clause.

    – jarlh
    Nov 15 '18 at 11:37















0















I have a sales table



I wish to do the following select the data date that is available if the top date SalePeriodFrom = 20181101



(if the top value SalePeriodFrom 01/11/2018 - first of the month data is available)



if not take the data from the day before (last day of the previous month 31/10/2018)



the code I have is



Select *
from Sales

Where case when SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) then

(SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)
and (SalePeriodTo > '20010101' or
SalePeriodTo is null))

else (SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE())
and (SalePeriodTo > '20010101' or
SalePeriodTo is null)) end


I can't seem to get this to work, any ideas please team










share|improve this question
























  • You're trying to use a CASE expression as a statement. A CASE expression returns a scalar value (that needs to be compared to another expression in the WHERE to create a Boolean result). A CASE expression doesn't return a Boolean result on its own. For example CASE ColumnA WHEN 'A' THEN 1 ELSE 2 END = 2;

    – Larnu
    Nov 15 '18 at 11:31












  • T-SQL does not have Boolean values. It has Boolean expressions, but these are only valid in particular contexts. And yes, this is annoying. You can have your CASE return a 1 or 0 and check on that, or split the query in two and use a UNION [ALL] to combine the results, or figure out universal expressions to compare SalePeriodFrom and SalePeriodTo to (I can't immediately see what those would be).

    – Jeroen Mostert
    Nov 15 '18 at 11:34






  • 1





    I can't figure out what you want to accomplish. Can you provide sample data and desired results?

    – Gordon Linoff
    Nov 15 '18 at 11:36











  • It's generally better to use AND/OR constructions instead of case expressions in the WHERE clause.

    – jarlh
    Nov 15 '18 at 11:37













0












0








0








I have a sales table



I wish to do the following select the data date that is available if the top date SalePeriodFrom = 20181101



(if the top value SalePeriodFrom 01/11/2018 - first of the month data is available)



if not take the data from the day before (last day of the previous month 31/10/2018)



the code I have is



Select *
from Sales

Where case when SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) then

(SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)
and (SalePeriodTo > '20010101' or
SalePeriodTo is null))

else (SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE())
and (SalePeriodTo > '20010101' or
SalePeriodTo is null)) end


I can't seem to get this to work, any ideas please team










share|improve this question
















I have a sales table



I wish to do the following select the data date that is available if the top date SalePeriodFrom = 20181101



(if the top value SalePeriodFrom 01/11/2018 - first of the month data is available)



if not take the data from the day before (last day of the previous month 31/10/2018)



the code I have is



Select *
from Sales

Where case when SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) then

(SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)
and (SalePeriodTo > '20010101' or
SalePeriodTo is null))

else (SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE())
and (SalePeriodTo > '20010101' or
SalePeriodTo is null)) end


I can't seem to get this to work, any ideas please team







sql sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:08







pete

















asked Nov 15 '18 at 11:28









petepete

518




518












  • You're trying to use a CASE expression as a statement. A CASE expression returns a scalar value (that needs to be compared to another expression in the WHERE to create a Boolean result). A CASE expression doesn't return a Boolean result on its own. For example CASE ColumnA WHEN 'A' THEN 1 ELSE 2 END = 2;

    – Larnu
    Nov 15 '18 at 11:31












  • T-SQL does not have Boolean values. It has Boolean expressions, but these are only valid in particular contexts. And yes, this is annoying. You can have your CASE return a 1 or 0 and check on that, or split the query in two and use a UNION [ALL] to combine the results, or figure out universal expressions to compare SalePeriodFrom and SalePeriodTo to (I can't immediately see what those would be).

    – Jeroen Mostert
    Nov 15 '18 at 11:34






  • 1





    I can't figure out what you want to accomplish. Can you provide sample data and desired results?

    – Gordon Linoff
    Nov 15 '18 at 11:36











  • It's generally better to use AND/OR constructions instead of case expressions in the WHERE clause.

    – jarlh
    Nov 15 '18 at 11:37

















  • You're trying to use a CASE expression as a statement. A CASE expression returns a scalar value (that needs to be compared to another expression in the WHERE to create a Boolean result). A CASE expression doesn't return a Boolean result on its own. For example CASE ColumnA WHEN 'A' THEN 1 ELSE 2 END = 2;

    – Larnu
    Nov 15 '18 at 11:31












  • T-SQL does not have Boolean values. It has Boolean expressions, but these are only valid in particular contexts. And yes, this is annoying. You can have your CASE return a 1 or 0 and check on that, or split the query in two and use a UNION [ALL] to combine the results, or figure out universal expressions to compare SalePeriodFrom and SalePeriodTo to (I can't immediately see what those would be).

    – Jeroen Mostert
    Nov 15 '18 at 11:34






  • 1





    I can't figure out what you want to accomplish. Can you provide sample data and desired results?

    – Gordon Linoff
    Nov 15 '18 at 11:36











  • It's generally better to use AND/OR constructions instead of case expressions in the WHERE clause.

    – jarlh
    Nov 15 '18 at 11:37
















You're trying to use a CASE expression as a statement. A CASE expression returns a scalar value (that needs to be compared to another expression in the WHERE to create a Boolean result). A CASE expression doesn't return a Boolean result on its own. For example CASE ColumnA WHEN 'A' THEN 1 ELSE 2 END = 2;

– Larnu
Nov 15 '18 at 11:31






You're trying to use a CASE expression as a statement. A CASE expression returns a scalar value (that needs to be compared to another expression in the WHERE to create a Boolean result). A CASE expression doesn't return a Boolean result on its own. For example CASE ColumnA WHEN 'A' THEN 1 ELSE 2 END = 2;

– Larnu
Nov 15 '18 at 11:31














T-SQL does not have Boolean values. It has Boolean expressions, but these are only valid in particular contexts. And yes, this is annoying. You can have your CASE return a 1 or 0 and check on that, or split the query in two and use a UNION [ALL] to combine the results, or figure out universal expressions to compare SalePeriodFrom and SalePeriodTo to (I can't immediately see what those would be).

– Jeroen Mostert
Nov 15 '18 at 11:34





T-SQL does not have Boolean values. It has Boolean expressions, but these are only valid in particular contexts. And yes, this is annoying. You can have your CASE return a 1 or 0 and check on that, or split the query in two and use a UNION [ALL] to combine the results, or figure out universal expressions to compare SalePeriodFrom and SalePeriodTo to (I can't immediately see what those would be).

– Jeroen Mostert
Nov 15 '18 at 11:34




1




1





I can't figure out what you want to accomplish. Can you provide sample data and desired results?

– Gordon Linoff
Nov 15 '18 at 11:36





I can't figure out what you want to accomplish. Can you provide sample data and desired results?

– Gordon Linoff
Nov 15 '18 at 11:36













It's generally better to use AND/OR constructions instead of case expressions in the WHERE clause.

– jarlh
Nov 15 '18 at 11:37





It's generally better to use AND/OR constructions instead of case expressions in the WHERE clause.

– jarlh
Nov 15 '18 at 11:37












3 Answers
3






active

oldest

votes


















2














This appears to replicate the boolean expressions you have inside your CASE expression, however, I can't check this without sample data:



SELECT List of Columns --You should probably list your columns here
FROM dbo.Sales --Assumed dbo schema
WHERE (SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)
AND (SalePeriodTo > '20181101' OR SalePeriodTo IS NULL))
OR (SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE())
AND (SalePeriodTo > '20010101' OR SalePeriodTo IS NULL));


Like mentioned into the comments on the question, this doesn't use a CASE expression at all. Using a CASE on your columns would cause the query to become non-SARGable, so (generally) they are best avoided.



Note I've taken out the clause SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0). This actually made no sense, as it looked like you were trying to check that the value of SalePeriodFrom was both greater than (>) and less than or equal to (<=) the expression DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0). It is impossible to something to be greater than something and less than or equal to it. For example 1 is greater than 0 but it is not equal or less than it. 0 is less than or equal to 0, but it is not greater than it.






share|improve this answer























  • this does not seem to work, I have tried similar to answer by serge below. There is data available in the dataset that SalePeriodFrom = 01/11/2018,

    – pete
    Nov 16 '18 at 12:33











  • @pete then provide sample data and expected results, like was asked for please. Have you tried altering this example (based on guess work). Maybe it's as simple as using >= instead; we can't know without knowing your requirements.

    – Larnu
    Nov 17 '18 at 10:03



















0














As mentioned above, the first WHEN condition is always false so you may discard it.



SELECT * FROM Sales
WHERE SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) AND
(SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE()) AND
(SalePeriodTo > '20010101' OR SalePeriodTo is null))





share|improve this answer




















  • 1





    I mention this is my answer, however, SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) AND SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) can never be true.

    – Larnu
    Nov 15 '18 at 11:45











  • @Larnu yes, it's very strange logical expression obfuscated by CASE

    – serge
    Nov 15 '18 at 13:37











  • hi this does not seem to work , there is data available in the dataset that SalePeriodFrom = 01/11/2018, it seems to have done the query for all records after the OR . any more ideas please

    – pete
    Nov 15 '18 at 14:07











  • @pete answer updated

    – serge
    Nov 15 '18 at 14:44











  • hi, this returned no rows, any more ideas please?

    – pete
    Nov 15 '18 at 17:46


















0














Your question logic is really garbled and hard to make sense of. Read it back to yourself, or read it aloud to a co-worker and see if they get what you're on about



My understanding of your requirement is:



If the sales table contains any data from 01-Nov-2018, return all the data with a date of 01-Nov-2018, otherwise return any data with a date of 31-Oct-2018



select *
from sales
where SalePeriodFrom =
(SELECT MAX(saleperiodfrom) FROM sales WHERE saleperiodfrom IN ('2018-11-01', '2018-10-30'))


If there is data for both dates or only 1st Nov, the MAX() will return the 1st Nov date. If there is no data from 1st Nov but there is data from 30th Oct then the MAX will return the 30th Oct date. If there is no data for either date, the max will return nothing and you'll get no data



If I've misunderstood your requirement, please state it more clearly



——————————————————————————————-



Edit:



Or maybe you mean:



If the sales table contains any data with a date 01-Nov-2018, return all the data after midnight on 01-Nov-2018, otherwise return any data after midnight on 31-Oct-2018



select *
from sales
where SalePeriodFrom >=
(SELECT MAX(saleperiodfrom) FROM sales WHERE saleperiodfrom IN ('2018-11-01', '2018-10-30'))





share|improve this answer

























  • hi I tried the following where saleperiodFrom = (SELECT MAX(saleperiodFrom) FROM Sales WHERE saleperiodFrom IN ('2018-11-01', '2018-10-31') and (saleperiodTo> '20010101' or ActiveTo is null)) I get no records. to clarify I want the data to go from saleperiodFrom 01/11/2018, if the max value 01/11/2018 has not been entered into the data set yet then the saleperiodFrom need to go from 31/10/2018 - last day of the previous month. please help / more ideas.. do appreciate

    – pete
    Nov 16 '18 at 12:49











  • Make the = into >= instead? You’re not making a lot of sense to be honest..

    – Caius Jard
    Nov 16 '18 at 18:00










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318464%2fwhere-case-when-sql%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









2














This appears to replicate the boolean expressions you have inside your CASE expression, however, I can't check this without sample data:



SELECT List of Columns --You should probably list your columns here
FROM dbo.Sales --Assumed dbo schema
WHERE (SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)
AND (SalePeriodTo > '20181101' OR SalePeriodTo IS NULL))
OR (SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE())
AND (SalePeriodTo > '20010101' OR SalePeriodTo IS NULL));


Like mentioned into the comments on the question, this doesn't use a CASE expression at all. Using a CASE on your columns would cause the query to become non-SARGable, so (generally) they are best avoided.



Note I've taken out the clause SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0). This actually made no sense, as it looked like you were trying to check that the value of SalePeriodFrom was both greater than (>) and less than or equal to (<=) the expression DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0). It is impossible to something to be greater than something and less than or equal to it. For example 1 is greater than 0 but it is not equal or less than it. 0 is less than or equal to 0, but it is not greater than it.






share|improve this answer























  • this does not seem to work, I have tried similar to answer by serge below. There is data available in the dataset that SalePeriodFrom = 01/11/2018,

    – pete
    Nov 16 '18 at 12:33











  • @pete then provide sample data and expected results, like was asked for please. Have you tried altering this example (based on guess work). Maybe it's as simple as using >= instead; we can't know without knowing your requirements.

    – Larnu
    Nov 17 '18 at 10:03
















2














This appears to replicate the boolean expressions you have inside your CASE expression, however, I can't check this without sample data:



SELECT List of Columns --You should probably list your columns here
FROM dbo.Sales --Assumed dbo schema
WHERE (SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)
AND (SalePeriodTo > '20181101' OR SalePeriodTo IS NULL))
OR (SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE())
AND (SalePeriodTo > '20010101' OR SalePeriodTo IS NULL));


Like mentioned into the comments on the question, this doesn't use a CASE expression at all. Using a CASE on your columns would cause the query to become non-SARGable, so (generally) they are best avoided.



Note I've taken out the clause SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0). This actually made no sense, as it looked like you were trying to check that the value of SalePeriodFrom was both greater than (>) and less than or equal to (<=) the expression DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0). It is impossible to something to be greater than something and less than or equal to it. For example 1 is greater than 0 but it is not equal or less than it. 0 is less than or equal to 0, but it is not greater than it.






share|improve this answer























  • this does not seem to work, I have tried similar to answer by serge below. There is data available in the dataset that SalePeriodFrom = 01/11/2018,

    – pete
    Nov 16 '18 at 12:33











  • @pete then provide sample data and expected results, like was asked for please. Have you tried altering this example (based on guess work). Maybe it's as simple as using >= instead; we can't know without knowing your requirements.

    – Larnu
    Nov 17 '18 at 10:03














2












2








2







This appears to replicate the boolean expressions you have inside your CASE expression, however, I can't check this without sample data:



SELECT List of Columns --You should probably list your columns here
FROM dbo.Sales --Assumed dbo schema
WHERE (SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)
AND (SalePeriodTo > '20181101' OR SalePeriodTo IS NULL))
OR (SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE())
AND (SalePeriodTo > '20010101' OR SalePeriodTo IS NULL));


Like mentioned into the comments on the question, this doesn't use a CASE expression at all. Using a CASE on your columns would cause the query to become non-SARGable, so (generally) they are best avoided.



Note I've taken out the clause SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0). This actually made no sense, as it looked like you were trying to check that the value of SalePeriodFrom was both greater than (>) and less than or equal to (<=) the expression DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0). It is impossible to something to be greater than something and less than or equal to it. For example 1 is greater than 0 but it is not equal or less than it. 0 is less than or equal to 0, but it is not greater than it.






share|improve this answer













This appears to replicate the boolean expressions you have inside your CASE expression, however, I can't check this without sample data:



SELECT List of Columns --You should probably list your columns here
FROM dbo.Sales --Assumed dbo schema
WHERE (SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)
AND (SalePeriodTo > '20181101' OR SalePeriodTo IS NULL))
OR (SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE())
AND (SalePeriodTo > '20010101' OR SalePeriodTo IS NULL));


Like mentioned into the comments on the question, this doesn't use a CASE expression at all. Using a CASE on your columns would cause the query to become non-SARGable, so (generally) they are best avoided.



Note I've taken out the clause SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0). This actually made no sense, as it looked like you were trying to check that the value of SalePeriodFrom was both greater than (>) and less than or equal to (<=) the expression DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0). It is impossible to something to be greater than something and less than or equal to it. For example 1 is greater than 0 but it is not equal or less than it. 0 is less than or equal to 0, but it is not greater than it.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 11:40









LarnuLarnu

21.1k51733




21.1k51733












  • this does not seem to work, I have tried similar to answer by serge below. There is data available in the dataset that SalePeriodFrom = 01/11/2018,

    – pete
    Nov 16 '18 at 12:33











  • @pete then provide sample data and expected results, like was asked for please. Have you tried altering this example (based on guess work). Maybe it's as simple as using >= instead; we can't know without knowing your requirements.

    – Larnu
    Nov 17 '18 at 10:03


















  • this does not seem to work, I have tried similar to answer by serge below. There is data available in the dataset that SalePeriodFrom = 01/11/2018,

    – pete
    Nov 16 '18 at 12:33











  • @pete then provide sample data and expected results, like was asked for please. Have you tried altering this example (based on guess work). Maybe it's as simple as using >= instead; we can't know without knowing your requirements.

    – Larnu
    Nov 17 '18 at 10:03

















this does not seem to work, I have tried similar to answer by serge below. There is data available in the dataset that SalePeriodFrom = 01/11/2018,

– pete
Nov 16 '18 at 12:33





this does not seem to work, I have tried similar to answer by serge below. There is data available in the dataset that SalePeriodFrom = 01/11/2018,

– pete
Nov 16 '18 at 12:33













@pete then provide sample data and expected results, like was asked for please. Have you tried altering this example (based on guess work). Maybe it's as simple as using >= instead; we can't know without knowing your requirements.

– Larnu
Nov 17 '18 at 10:03






@pete then provide sample data and expected results, like was asked for please. Have you tried altering this example (based on guess work). Maybe it's as simple as using >= instead; we can't know without knowing your requirements.

– Larnu
Nov 17 '18 at 10:03














0














As mentioned above, the first WHEN condition is always false so you may discard it.



SELECT * FROM Sales
WHERE SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) AND
(SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE()) AND
(SalePeriodTo > '20010101' OR SalePeriodTo is null))





share|improve this answer




















  • 1





    I mention this is my answer, however, SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) AND SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) can never be true.

    – Larnu
    Nov 15 '18 at 11:45











  • @Larnu yes, it's very strange logical expression obfuscated by CASE

    – serge
    Nov 15 '18 at 13:37











  • hi this does not seem to work , there is data available in the dataset that SalePeriodFrom = 01/11/2018, it seems to have done the query for all records after the OR . any more ideas please

    – pete
    Nov 15 '18 at 14:07











  • @pete answer updated

    – serge
    Nov 15 '18 at 14:44











  • hi, this returned no rows, any more ideas please?

    – pete
    Nov 15 '18 at 17:46















0














As mentioned above, the first WHEN condition is always false so you may discard it.



SELECT * FROM Sales
WHERE SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) AND
(SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE()) AND
(SalePeriodTo > '20010101' OR SalePeriodTo is null))





share|improve this answer




















  • 1





    I mention this is my answer, however, SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) AND SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) can never be true.

    – Larnu
    Nov 15 '18 at 11:45











  • @Larnu yes, it's very strange logical expression obfuscated by CASE

    – serge
    Nov 15 '18 at 13:37











  • hi this does not seem to work , there is data available in the dataset that SalePeriodFrom = 01/11/2018, it seems to have done the query for all records after the OR . any more ideas please

    – pete
    Nov 15 '18 at 14:07











  • @pete answer updated

    – serge
    Nov 15 '18 at 14:44











  • hi, this returned no rows, any more ideas please?

    – pete
    Nov 15 '18 at 17:46













0












0








0







As mentioned above, the first WHEN condition is always false so you may discard it.



SELECT * FROM Sales
WHERE SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) AND
(SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE()) AND
(SalePeriodTo > '20010101' OR SalePeriodTo is null))





share|improve this answer















As mentioned above, the first WHEN condition is always false so you may discard it.



SELECT * FROM Sales
WHERE SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) AND
(SalePeriodFrom <= DATEADD(DAY, -(DAY(GETDATE())), GETDATE()) AND
(SalePeriodTo > '20010101' OR SalePeriodTo is null))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 14:43

























answered Nov 15 '18 at 11:38









sergeserge

70148




70148







  • 1





    I mention this is my answer, however, SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) AND SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) can never be true.

    – Larnu
    Nov 15 '18 at 11:45











  • @Larnu yes, it's very strange logical expression obfuscated by CASE

    – serge
    Nov 15 '18 at 13:37











  • hi this does not seem to work , there is data available in the dataset that SalePeriodFrom = 01/11/2018, it seems to have done the query for all records after the OR . any more ideas please

    – pete
    Nov 15 '18 at 14:07











  • @pete answer updated

    – serge
    Nov 15 '18 at 14:44











  • hi, this returned no rows, any more ideas please?

    – pete
    Nov 15 '18 at 17:46












  • 1





    I mention this is my answer, however, SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) AND SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) can never be true.

    – Larnu
    Nov 15 '18 at 11:45











  • @Larnu yes, it's very strange logical expression obfuscated by CASE

    – serge
    Nov 15 '18 at 13:37











  • hi this does not seem to work , there is data available in the dataset that SalePeriodFrom = 01/11/2018, it seems to have done the query for all records after the OR . any more ideas please

    – pete
    Nov 15 '18 at 14:07











  • @pete answer updated

    – serge
    Nov 15 '18 at 14:44











  • hi, this returned no rows, any more ideas please?

    – pete
    Nov 15 '18 at 17:46







1




1





I mention this is my answer, however, SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) AND SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) can never be true.

– Larnu
Nov 15 '18 at 11:45





I mention this is my answer, however, SalePeriodFrom > DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) AND SalePeriodFrom <= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) can never be true.

– Larnu
Nov 15 '18 at 11:45













@Larnu yes, it's very strange logical expression obfuscated by CASE

– serge
Nov 15 '18 at 13:37





@Larnu yes, it's very strange logical expression obfuscated by CASE

– serge
Nov 15 '18 at 13:37













hi this does not seem to work , there is data available in the dataset that SalePeriodFrom = 01/11/2018, it seems to have done the query for all records after the OR . any more ideas please

– pete
Nov 15 '18 at 14:07





hi this does not seem to work , there is data available in the dataset that SalePeriodFrom = 01/11/2018, it seems to have done the query for all records after the OR . any more ideas please

– pete
Nov 15 '18 at 14:07













@pete answer updated

– serge
Nov 15 '18 at 14:44





@pete answer updated

– serge
Nov 15 '18 at 14:44













hi, this returned no rows, any more ideas please?

– pete
Nov 15 '18 at 17:46





hi, this returned no rows, any more ideas please?

– pete
Nov 15 '18 at 17:46











0














Your question logic is really garbled and hard to make sense of. Read it back to yourself, or read it aloud to a co-worker and see if they get what you're on about



My understanding of your requirement is:



If the sales table contains any data from 01-Nov-2018, return all the data with a date of 01-Nov-2018, otherwise return any data with a date of 31-Oct-2018



select *
from sales
where SalePeriodFrom =
(SELECT MAX(saleperiodfrom) FROM sales WHERE saleperiodfrom IN ('2018-11-01', '2018-10-30'))


If there is data for both dates or only 1st Nov, the MAX() will return the 1st Nov date. If there is no data from 1st Nov but there is data from 30th Oct then the MAX will return the 30th Oct date. If there is no data for either date, the max will return nothing and you'll get no data



If I've misunderstood your requirement, please state it more clearly



——————————————————————————————-



Edit:



Or maybe you mean:



If the sales table contains any data with a date 01-Nov-2018, return all the data after midnight on 01-Nov-2018, otherwise return any data after midnight on 31-Oct-2018



select *
from sales
where SalePeriodFrom >=
(SELECT MAX(saleperiodfrom) FROM sales WHERE saleperiodfrom IN ('2018-11-01', '2018-10-30'))





share|improve this answer

























  • hi I tried the following where saleperiodFrom = (SELECT MAX(saleperiodFrom) FROM Sales WHERE saleperiodFrom IN ('2018-11-01', '2018-10-31') and (saleperiodTo> '20010101' or ActiveTo is null)) I get no records. to clarify I want the data to go from saleperiodFrom 01/11/2018, if the max value 01/11/2018 has not been entered into the data set yet then the saleperiodFrom need to go from 31/10/2018 - last day of the previous month. please help / more ideas.. do appreciate

    – pete
    Nov 16 '18 at 12:49











  • Make the = into >= instead? You’re not making a lot of sense to be honest..

    – Caius Jard
    Nov 16 '18 at 18:00















0














Your question logic is really garbled and hard to make sense of. Read it back to yourself, or read it aloud to a co-worker and see if they get what you're on about



My understanding of your requirement is:



If the sales table contains any data from 01-Nov-2018, return all the data with a date of 01-Nov-2018, otherwise return any data with a date of 31-Oct-2018



select *
from sales
where SalePeriodFrom =
(SELECT MAX(saleperiodfrom) FROM sales WHERE saleperiodfrom IN ('2018-11-01', '2018-10-30'))


If there is data for both dates or only 1st Nov, the MAX() will return the 1st Nov date. If there is no data from 1st Nov but there is data from 30th Oct then the MAX will return the 30th Oct date. If there is no data for either date, the max will return nothing and you'll get no data



If I've misunderstood your requirement, please state it more clearly



——————————————————————————————-



Edit:



Or maybe you mean:



If the sales table contains any data with a date 01-Nov-2018, return all the data after midnight on 01-Nov-2018, otherwise return any data after midnight on 31-Oct-2018



select *
from sales
where SalePeriodFrom >=
(SELECT MAX(saleperiodfrom) FROM sales WHERE saleperiodfrom IN ('2018-11-01', '2018-10-30'))





share|improve this answer

























  • hi I tried the following where saleperiodFrom = (SELECT MAX(saleperiodFrom) FROM Sales WHERE saleperiodFrom IN ('2018-11-01', '2018-10-31') and (saleperiodTo> '20010101' or ActiveTo is null)) I get no records. to clarify I want the data to go from saleperiodFrom 01/11/2018, if the max value 01/11/2018 has not been entered into the data set yet then the saleperiodFrom need to go from 31/10/2018 - last day of the previous month. please help / more ideas.. do appreciate

    – pete
    Nov 16 '18 at 12:49











  • Make the = into >= instead? You’re not making a lot of sense to be honest..

    – Caius Jard
    Nov 16 '18 at 18:00













0












0








0







Your question logic is really garbled and hard to make sense of. Read it back to yourself, or read it aloud to a co-worker and see if they get what you're on about



My understanding of your requirement is:



If the sales table contains any data from 01-Nov-2018, return all the data with a date of 01-Nov-2018, otherwise return any data with a date of 31-Oct-2018



select *
from sales
where SalePeriodFrom =
(SELECT MAX(saleperiodfrom) FROM sales WHERE saleperiodfrom IN ('2018-11-01', '2018-10-30'))


If there is data for both dates or only 1st Nov, the MAX() will return the 1st Nov date. If there is no data from 1st Nov but there is data from 30th Oct then the MAX will return the 30th Oct date. If there is no data for either date, the max will return nothing and you'll get no data



If I've misunderstood your requirement, please state it more clearly



——————————————————————————————-



Edit:



Or maybe you mean:



If the sales table contains any data with a date 01-Nov-2018, return all the data after midnight on 01-Nov-2018, otherwise return any data after midnight on 31-Oct-2018



select *
from sales
where SalePeriodFrom >=
(SELECT MAX(saleperiodfrom) FROM sales WHERE saleperiodfrom IN ('2018-11-01', '2018-10-30'))





share|improve this answer















Your question logic is really garbled and hard to make sense of. Read it back to yourself, or read it aloud to a co-worker and see if they get what you're on about



My understanding of your requirement is:



If the sales table contains any data from 01-Nov-2018, return all the data with a date of 01-Nov-2018, otherwise return any data with a date of 31-Oct-2018



select *
from sales
where SalePeriodFrom =
(SELECT MAX(saleperiodfrom) FROM sales WHERE saleperiodfrom IN ('2018-11-01', '2018-10-30'))


If there is data for both dates or only 1st Nov, the MAX() will return the 1st Nov date. If there is no data from 1st Nov but there is data from 30th Oct then the MAX will return the 30th Oct date. If there is no data for either date, the max will return nothing and you'll get no data



If I've misunderstood your requirement, please state it more clearly



——————————————————————————————-



Edit:



Or maybe you mean:



If the sales table contains any data with a date 01-Nov-2018, return all the data after midnight on 01-Nov-2018, otherwise return any data after midnight on 31-Oct-2018



select *
from sales
where SalePeriodFrom >=
(SELECT MAX(saleperiodfrom) FROM sales WHERE saleperiodfrom IN ('2018-11-01', '2018-10-30'))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 18:04

























answered Nov 15 '18 at 20:33









Caius JardCaius Jard

12.4k21340




12.4k21340












  • hi I tried the following where saleperiodFrom = (SELECT MAX(saleperiodFrom) FROM Sales WHERE saleperiodFrom IN ('2018-11-01', '2018-10-31') and (saleperiodTo> '20010101' or ActiveTo is null)) I get no records. to clarify I want the data to go from saleperiodFrom 01/11/2018, if the max value 01/11/2018 has not been entered into the data set yet then the saleperiodFrom need to go from 31/10/2018 - last day of the previous month. please help / more ideas.. do appreciate

    – pete
    Nov 16 '18 at 12:49











  • Make the = into >= instead? You’re not making a lot of sense to be honest..

    – Caius Jard
    Nov 16 '18 at 18:00

















  • hi I tried the following where saleperiodFrom = (SELECT MAX(saleperiodFrom) FROM Sales WHERE saleperiodFrom IN ('2018-11-01', '2018-10-31') and (saleperiodTo> '20010101' or ActiveTo is null)) I get no records. to clarify I want the data to go from saleperiodFrom 01/11/2018, if the max value 01/11/2018 has not been entered into the data set yet then the saleperiodFrom need to go from 31/10/2018 - last day of the previous month. please help / more ideas.. do appreciate

    – pete
    Nov 16 '18 at 12:49











  • Make the = into >= instead? You’re not making a lot of sense to be honest..

    – Caius Jard
    Nov 16 '18 at 18:00
















hi I tried the following where saleperiodFrom = (SELECT MAX(saleperiodFrom) FROM Sales WHERE saleperiodFrom IN ('2018-11-01', '2018-10-31') and (saleperiodTo> '20010101' or ActiveTo is null)) I get no records. to clarify I want the data to go from saleperiodFrom 01/11/2018, if the max value 01/11/2018 has not been entered into the data set yet then the saleperiodFrom need to go from 31/10/2018 - last day of the previous month. please help / more ideas.. do appreciate

– pete
Nov 16 '18 at 12:49





hi I tried the following where saleperiodFrom = (SELECT MAX(saleperiodFrom) FROM Sales WHERE saleperiodFrom IN ('2018-11-01', '2018-10-31') and (saleperiodTo> '20010101' or ActiveTo is null)) I get no records. to clarify I want the data to go from saleperiodFrom 01/11/2018, if the max value 01/11/2018 has not been entered into the data set yet then the saleperiodFrom need to go from 31/10/2018 - last day of the previous month. please help / more ideas.. do appreciate

– pete
Nov 16 '18 at 12:49













Make the = into >= instead? You’re not making a lot of sense to be honest..

– Caius Jard
Nov 16 '18 at 18:00





Make the = into >= instead? You’re not making a lot of sense to be honest..

– Caius Jard
Nov 16 '18 at 18:00

















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318464%2fwhere-case-when-sql%23new-answer', 'question_page');

);

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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto