Case when Statement Not Working with my data












1















While Working case statement i got stuck



For Eg i have below scenario



Amt         StartDate   EndDate     Port    Trade
10.00 9/21/2018 9/21/2020 NULL NULL
54,523.00 11/14/2018 11/15/2018 NULL NULL
756.00 11/14/2018 11/15/2018 NULL NULL
456.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
453.00 11/14/2018 11/15/2018 NULL NULL
786.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
568.00 11/14/2018 11/15/2018 NULL NULL
12,358.00 11/14/2018 11/15/2018 NULL NULL
45,388.00 11/5/2018 12/5/2018 NULL NULL
75,368.00 8/9/2018 12/20/2018 call collateral
783,678.00 7/13/2018 1/14/2019 NULL NULL



  • 1)what i am looking for is when startdate and enddate difference = 1
    or Port like '%call%' and Trade='collateral' then amt

  • 2) when startdate and enddate difference > 7
    and Port not like '%call%' and Trade <> 'collateral' then amt


My First Condition Works




select CASE WHEN DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))=1
OR (Port like 'Call' and [Trade]='collateral')
THEN amt ELSE 0 END AS money1
from tablename



But Second Condition not Working



select CASE WHEN (DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999)
and [Trade] <> 'collateral' and Portfolio not like '%Call%'
THEN amt ELSE 0 END AS money2 from tablename


excepted o/p



Amt         StartDate   EndDate money1              money2
10.00 9/21/2018 9/21/2020 10.00
54,523.00 11/14/2018 11/15/2018 54,523.00
756.00 11/14/2018 11/15/2018 756.00
456.00 11/14/2018 11/15/2018 456.00
86.00 11/14/2018 11/15/2018 86.00
86.00 11/14/2018 11/15/2018 86.00
453.00 11/14/2018 11/15/2018 453.00
786.00 11/14/2018 11/15/2018 786.00
86.00 11/14/2018 11/15/2018 86.00
568.00 11/14/2018 11/15/2018 568.00
12,358.00 11/14/2018 11/15/2018 12,358.00
45,388.00 11/5/2018 12/5/2018 45,388.00
75,368.00 8/9/2018 12/20/2018 75,368.00
783,678.00 7/13/2018 1/14/2019 783,678.00


Need Help i am using Sql server 2012.










share|improve this question




















  • 1





    Specify the expected result!

    – jarlh
    Nov 20 '18 at 13:32











  • You wrote not like '%call%' in the question and not like 'Call' in the code. They're not the same... One has wild-cards, one has an upper case C (which may or may not matter depending on your collation sequence).

    – MatBailie
    Nov 20 '18 at 13:40








  • 2





    I'm not a sql-server expert but check the result of Portfolio not like... when the value equals NULL.

    – Robert Kock
    Nov 20 '18 at 13:45
















1















While Working case statement i got stuck



For Eg i have below scenario



Amt         StartDate   EndDate     Port    Trade
10.00 9/21/2018 9/21/2020 NULL NULL
54,523.00 11/14/2018 11/15/2018 NULL NULL
756.00 11/14/2018 11/15/2018 NULL NULL
456.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
453.00 11/14/2018 11/15/2018 NULL NULL
786.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
568.00 11/14/2018 11/15/2018 NULL NULL
12,358.00 11/14/2018 11/15/2018 NULL NULL
45,388.00 11/5/2018 12/5/2018 NULL NULL
75,368.00 8/9/2018 12/20/2018 call collateral
783,678.00 7/13/2018 1/14/2019 NULL NULL



  • 1)what i am looking for is when startdate and enddate difference = 1
    or Port like '%call%' and Trade='collateral' then amt

  • 2) when startdate and enddate difference > 7
    and Port not like '%call%' and Trade <> 'collateral' then amt


My First Condition Works




select CASE WHEN DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))=1
OR (Port like 'Call' and [Trade]='collateral')
THEN amt ELSE 0 END AS money1
from tablename



But Second Condition not Working



select CASE WHEN (DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999)
and [Trade] <> 'collateral' and Portfolio not like '%Call%'
THEN amt ELSE 0 END AS money2 from tablename


excepted o/p



Amt         StartDate   EndDate money1              money2
10.00 9/21/2018 9/21/2020 10.00
54,523.00 11/14/2018 11/15/2018 54,523.00
756.00 11/14/2018 11/15/2018 756.00
456.00 11/14/2018 11/15/2018 456.00
86.00 11/14/2018 11/15/2018 86.00
86.00 11/14/2018 11/15/2018 86.00
453.00 11/14/2018 11/15/2018 453.00
786.00 11/14/2018 11/15/2018 786.00
86.00 11/14/2018 11/15/2018 86.00
568.00 11/14/2018 11/15/2018 568.00
12,358.00 11/14/2018 11/15/2018 12,358.00
45,388.00 11/5/2018 12/5/2018 45,388.00
75,368.00 8/9/2018 12/20/2018 75,368.00
783,678.00 7/13/2018 1/14/2019 783,678.00


Need Help i am using Sql server 2012.










share|improve this question




















  • 1





    Specify the expected result!

    – jarlh
    Nov 20 '18 at 13:32











  • You wrote not like '%call%' in the question and not like 'Call' in the code. They're not the same... One has wild-cards, one has an upper case C (which may or may not matter depending on your collation sequence).

    – MatBailie
    Nov 20 '18 at 13:40








  • 2





    I'm not a sql-server expert but check the result of Portfolio not like... when the value equals NULL.

    – Robert Kock
    Nov 20 '18 at 13:45














1












1








1








While Working case statement i got stuck



For Eg i have below scenario



Amt         StartDate   EndDate     Port    Trade
10.00 9/21/2018 9/21/2020 NULL NULL
54,523.00 11/14/2018 11/15/2018 NULL NULL
756.00 11/14/2018 11/15/2018 NULL NULL
456.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
453.00 11/14/2018 11/15/2018 NULL NULL
786.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
568.00 11/14/2018 11/15/2018 NULL NULL
12,358.00 11/14/2018 11/15/2018 NULL NULL
45,388.00 11/5/2018 12/5/2018 NULL NULL
75,368.00 8/9/2018 12/20/2018 call collateral
783,678.00 7/13/2018 1/14/2019 NULL NULL



  • 1)what i am looking for is when startdate and enddate difference = 1
    or Port like '%call%' and Trade='collateral' then amt

  • 2) when startdate and enddate difference > 7
    and Port not like '%call%' and Trade <> 'collateral' then amt


My First Condition Works




select CASE WHEN DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))=1
OR (Port like 'Call' and [Trade]='collateral')
THEN amt ELSE 0 END AS money1
from tablename



But Second Condition not Working



select CASE WHEN (DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999)
and [Trade] <> 'collateral' and Portfolio not like '%Call%'
THEN amt ELSE 0 END AS money2 from tablename


excepted o/p



Amt         StartDate   EndDate money1              money2
10.00 9/21/2018 9/21/2020 10.00
54,523.00 11/14/2018 11/15/2018 54,523.00
756.00 11/14/2018 11/15/2018 756.00
456.00 11/14/2018 11/15/2018 456.00
86.00 11/14/2018 11/15/2018 86.00
86.00 11/14/2018 11/15/2018 86.00
453.00 11/14/2018 11/15/2018 453.00
786.00 11/14/2018 11/15/2018 786.00
86.00 11/14/2018 11/15/2018 86.00
568.00 11/14/2018 11/15/2018 568.00
12,358.00 11/14/2018 11/15/2018 12,358.00
45,388.00 11/5/2018 12/5/2018 45,388.00
75,368.00 8/9/2018 12/20/2018 75,368.00
783,678.00 7/13/2018 1/14/2019 783,678.00


Need Help i am using Sql server 2012.










share|improve this question
















While Working case statement i got stuck



For Eg i have below scenario



Amt         StartDate   EndDate     Port    Trade
10.00 9/21/2018 9/21/2020 NULL NULL
54,523.00 11/14/2018 11/15/2018 NULL NULL
756.00 11/14/2018 11/15/2018 NULL NULL
456.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
453.00 11/14/2018 11/15/2018 NULL NULL
786.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
568.00 11/14/2018 11/15/2018 NULL NULL
12,358.00 11/14/2018 11/15/2018 NULL NULL
45,388.00 11/5/2018 12/5/2018 NULL NULL
75,368.00 8/9/2018 12/20/2018 call collateral
783,678.00 7/13/2018 1/14/2019 NULL NULL



  • 1)what i am looking for is when startdate and enddate difference = 1
    or Port like '%call%' and Trade='collateral' then amt

  • 2) when startdate and enddate difference > 7
    and Port not like '%call%' and Trade <> 'collateral' then amt


My First Condition Works




select CASE WHEN DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))=1
OR (Port like 'Call' and [Trade]='collateral')
THEN amt ELSE 0 END AS money1
from tablename



But Second Condition not Working



select CASE WHEN (DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999)
and [Trade] <> 'collateral' and Portfolio not like '%Call%'
THEN amt ELSE 0 END AS money2 from tablename


excepted o/p



Amt         StartDate   EndDate money1              money2
10.00 9/21/2018 9/21/2020 10.00
54,523.00 11/14/2018 11/15/2018 54,523.00
756.00 11/14/2018 11/15/2018 756.00
456.00 11/14/2018 11/15/2018 456.00
86.00 11/14/2018 11/15/2018 86.00
86.00 11/14/2018 11/15/2018 86.00
453.00 11/14/2018 11/15/2018 453.00
786.00 11/14/2018 11/15/2018 786.00
86.00 11/14/2018 11/15/2018 86.00
568.00 11/14/2018 11/15/2018 568.00
12,358.00 11/14/2018 11/15/2018 12,358.00
45,388.00 11/5/2018 12/5/2018 45,388.00
75,368.00 8/9/2018 12/20/2018 75,368.00
783,678.00 7/13/2018 1/14/2019 783,678.00


Need Help i am using Sql server 2012.







sql sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 13:42







rahul c

















asked Nov 20 '18 at 13:31









rahul crahul c

257




257








  • 1





    Specify the expected result!

    – jarlh
    Nov 20 '18 at 13:32











  • You wrote not like '%call%' in the question and not like 'Call' in the code. They're not the same... One has wild-cards, one has an upper case C (which may or may not matter depending on your collation sequence).

    – MatBailie
    Nov 20 '18 at 13:40








  • 2





    I'm not a sql-server expert but check the result of Portfolio not like... when the value equals NULL.

    – Robert Kock
    Nov 20 '18 at 13:45














  • 1





    Specify the expected result!

    – jarlh
    Nov 20 '18 at 13:32











  • You wrote not like '%call%' in the question and not like 'Call' in the code. They're not the same... One has wild-cards, one has an upper case C (which may or may not matter depending on your collation sequence).

    – MatBailie
    Nov 20 '18 at 13:40








  • 2





    I'm not a sql-server expert but check the result of Portfolio not like... when the value equals NULL.

    – Robert Kock
    Nov 20 '18 at 13:45








1




1





Specify the expected result!

– jarlh
Nov 20 '18 at 13:32





Specify the expected result!

– jarlh
Nov 20 '18 at 13:32













You wrote not like '%call%' in the question and not like 'Call' in the code. They're not the same... One has wild-cards, one has an upper case C (which may or may not matter depending on your collation sequence).

– MatBailie
Nov 20 '18 at 13:40







You wrote not like '%call%' in the question and not like 'Call' in the code. They're not the same... One has wild-cards, one has an upper case C (which may or may not matter depending on your collation sequence).

– MatBailie
Nov 20 '18 at 13:40






2




2





I'm not a sql-server expert but check the result of Portfolio not like... when the value equals NULL.

– Robert Kock
Nov 20 '18 at 13:45





I'm not a sql-server expert but check the result of Portfolio not like... when the value equals NULL.

– Robert Kock
Nov 20 '18 at 13:45












2 Answers
2






active

oldest

votes


















1














Check whether PortFolio and Trade are NULL.

So:



SELECT CASE WHEN DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999 AND
([Trade] IS NULL OR [Trade] <> 'collateral') AND
(Portfolio IS NULL OR Portfolio NOT LIKE '%call%')
THEN amt ELSE 0 END AS money2
FROM tablename;





share|improve this answer
























  • Thanks a lot it work for me....

    – rahul c
    Nov 20 '18 at 13:56











  • If i am using Isnull([Trade],'') still it is working.

    – rahul c
    Nov 20 '18 at 14:02



















1














Your like operator should be '%Call%'



select CASE WHEN (DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999)
and [Trade] <> 'collateral' and Portfolio not like '%Call%'
THEN amt ELSE 0 END AS money2 from tablename





share|improve this answer
























  • Thanks For replay @fa06 i have Taken '%Call%' Still it is Not Working

    – rahul c
    Nov 20 '18 at 13:44











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%2f53394126%2fcase-when-statement-not-working-with-my-data%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









1














Check whether PortFolio and Trade are NULL.

So:



SELECT CASE WHEN DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999 AND
([Trade] IS NULL OR [Trade] <> 'collateral') AND
(Portfolio IS NULL OR Portfolio NOT LIKE '%call%')
THEN amt ELSE 0 END AS money2
FROM tablename;





share|improve this answer
























  • Thanks a lot it work for me....

    – rahul c
    Nov 20 '18 at 13:56











  • If i am using Isnull([Trade],'') still it is working.

    – rahul c
    Nov 20 '18 at 14:02
















1














Check whether PortFolio and Trade are NULL.

So:



SELECT CASE WHEN DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999 AND
([Trade] IS NULL OR [Trade] <> 'collateral') AND
(Portfolio IS NULL OR Portfolio NOT LIKE '%call%')
THEN amt ELSE 0 END AS money2
FROM tablename;





share|improve this answer
























  • Thanks a lot it work for me....

    – rahul c
    Nov 20 '18 at 13:56











  • If i am using Isnull([Trade],'') still it is working.

    – rahul c
    Nov 20 '18 at 14:02














1












1








1







Check whether PortFolio and Trade are NULL.

So:



SELECT CASE WHEN DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999 AND
([Trade] IS NULL OR [Trade] <> 'collateral') AND
(Portfolio IS NULL OR Portfolio NOT LIKE '%call%')
THEN amt ELSE 0 END AS money2
FROM tablename;





share|improve this answer













Check whether PortFolio and Trade are NULL.

So:



SELECT CASE WHEN DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999 AND
([Trade] IS NULL OR [Trade] <> 'collateral') AND
(Portfolio IS NULL OR Portfolio NOT LIKE '%call%')
THEN amt ELSE 0 END AS money2
FROM tablename;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 13:53









Robert KockRobert Kock

4,0591617




4,0591617













  • Thanks a lot it work for me....

    – rahul c
    Nov 20 '18 at 13:56











  • If i am using Isnull([Trade],'') still it is working.

    – rahul c
    Nov 20 '18 at 14:02



















  • Thanks a lot it work for me....

    – rahul c
    Nov 20 '18 at 13:56











  • If i am using Isnull([Trade],'') still it is working.

    – rahul c
    Nov 20 '18 at 14:02

















Thanks a lot it work for me....

– rahul c
Nov 20 '18 at 13:56





Thanks a lot it work for me....

– rahul c
Nov 20 '18 at 13:56













If i am using Isnull([Trade],'') still it is working.

– rahul c
Nov 20 '18 at 14:02





If i am using Isnull([Trade],'') still it is working.

– rahul c
Nov 20 '18 at 14:02













1














Your like operator should be '%Call%'



select CASE WHEN (DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999)
and [Trade] <> 'collateral' and Portfolio not like '%Call%'
THEN amt ELSE 0 END AS money2 from tablename





share|improve this answer
























  • Thanks For replay @fa06 i have Taken '%Call%' Still it is Not Working

    – rahul c
    Nov 20 '18 at 13:44
















1














Your like operator should be '%Call%'



select CASE WHEN (DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999)
and [Trade] <> 'collateral' and Portfolio not like '%Call%'
THEN amt ELSE 0 END AS money2 from tablename





share|improve this answer
























  • Thanks For replay @fa06 i have Taken '%Call%' Still it is Not Working

    – rahul c
    Nov 20 '18 at 13:44














1












1








1







Your like operator should be '%Call%'



select CASE WHEN (DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999)
and [Trade] <> 'collateral' and Portfolio not like '%Call%'
THEN amt ELSE 0 END AS money2 from tablename





share|improve this answer













Your like operator should be '%Call%'



select CASE WHEN (DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND 
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999)
and [Trade] <> 'collateral' and Portfolio not like '%Call%'
THEN amt ELSE 0 END AS money2 from tablename






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 13:34









fa06fa06

12.2k2917




12.2k2917













  • Thanks For replay @fa06 i have Taken '%Call%' Still it is Not Working

    – rahul c
    Nov 20 '18 at 13:44



















  • Thanks For replay @fa06 i have Taken '%Call%' Still it is Not Working

    – rahul c
    Nov 20 '18 at 13:44

















Thanks For replay @fa06 i have Taken '%Call%' Still it is Not Working

– rahul c
Nov 20 '18 at 13:44





Thanks For replay @fa06 i have Taken '%Call%' Still it is Not Working

– rahul c
Nov 20 '18 at 13:44


















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%2f53394126%2fcase-when-statement-not-working-with-my-data%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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory