RTRIM in where clause





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I'm using Microsoft SQL Server 2012 Management Studio. In my table below I'm trying to get rid of the ID numbers from 1st Jan 2019 to 4th Jan 2019 that are 5 digits long or after the dash begin with a zero.



IDnum        DateTime
-----------------------
11-102434 03/01/2019
11-02434 03/01/2019
11-102421 02/01/2019
11-02421 02/01/2019
10-02345 31/12/2018


This is what I would like to see



IDnum        DateTime
-------------------------
11-102434 03/01/2019
11-102421 02/01/2019
10-02345 31/12/2018


I'm thinking there needs to be some kind of RTRIM() in the where clause but not sure how to do this.










share|improve this question




















  • 2





    So, what SQL have you already written and how does it fail to do what you want?

    – Corion
    Jan 3 at 12:17











  • Do all IDnum's start with two digits and a dash?

    – jarlh
    Jan 3 at 12:17


















2















I'm using Microsoft SQL Server 2012 Management Studio. In my table below I'm trying to get rid of the ID numbers from 1st Jan 2019 to 4th Jan 2019 that are 5 digits long or after the dash begin with a zero.



IDnum        DateTime
-----------------------
11-102434 03/01/2019
11-02434 03/01/2019
11-102421 02/01/2019
11-02421 02/01/2019
10-02345 31/12/2018


This is what I would like to see



IDnum        DateTime
-------------------------
11-102434 03/01/2019
11-102421 02/01/2019
10-02345 31/12/2018


I'm thinking there needs to be some kind of RTRIM() in the where clause but not sure how to do this.










share|improve this question




















  • 2





    So, what SQL have you already written and how does it fail to do what you want?

    – Corion
    Jan 3 at 12:17











  • Do all IDnum's start with two digits and a dash?

    – jarlh
    Jan 3 at 12:17














2












2








2








I'm using Microsoft SQL Server 2012 Management Studio. In my table below I'm trying to get rid of the ID numbers from 1st Jan 2019 to 4th Jan 2019 that are 5 digits long or after the dash begin with a zero.



IDnum        DateTime
-----------------------
11-102434 03/01/2019
11-02434 03/01/2019
11-102421 02/01/2019
11-02421 02/01/2019
10-02345 31/12/2018


This is what I would like to see



IDnum        DateTime
-------------------------
11-102434 03/01/2019
11-102421 02/01/2019
10-02345 31/12/2018


I'm thinking there needs to be some kind of RTRIM() in the where clause but not sure how to do this.










share|improve this question
















I'm using Microsoft SQL Server 2012 Management Studio. In my table below I'm trying to get rid of the ID numbers from 1st Jan 2019 to 4th Jan 2019 that are 5 digits long or after the dash begin with a zero.



IDnum        DateTime
-----------------------
11-102434 03/01/2019
11-02434 03/01/2019
11-102421 02/01/2019
11-02421 02/01/2019
10-02345 31/12/2018


This is what I would like to see



IDnum        DateTime
-------------------------
11-102434 03/01/2019
11-102421 02/01/2019
10-02345 31/12/2018


I'm thinking there needs to be some kind of RTRIM() in the where clause but not sure how to do this.







sql sql-server sql-server-2012






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 12:22









marc_s

585k13011251272




585k13011251272










asked Jan 3 at 12:15









Clem_FandangoClem_Fandango

5211




5211








  • 2





    So, what SQL have you already written and how does it fail to do what you want?

    – Corion
    Jan 3 at 12:17











  • Do all IDnum's start with two digits and a dash?

    – jarlh
    Jan 3 at 12:17














  • 2





    So, what SQL have you already written and how does it fail to do what you want?

    – Corion
    Jan 3 at 12:17











  • Do all IDnum's start with two digits and a dash?

    – jarlh
    Jan 3 at 12:17








2




2





So, what SQL have you already written and how does it fail to do what you want?

– Corion
Jan 3 at 12:17





So, what SQL have you already written and how does it fail to do what you want?

– Corion
Jan 3 at 12:17













Do all IDnum's start with two digits and a dash?

– jarlh
Jan 3 at 12:17





Do all IDnum's start with two digits and a dash?

– jarlh
Jan 3 at 12:17












4 Answers
4






active

oldest

votes


















2














Here's one way.



SELECT IDnum, DateTime
FROM YourTable
WHERE NOT (DateTime >= '2019-01-01' AND DateTime < '2019-01-05' --exclude from 1st Jan 2019 to 4th Jan 2019
AND (IDnum LIKE '%-_____' --that are 5 digits long after the dash
OR
IDnum LIKE '%-0%' --or begin with a 0 (after the dash)
)
)





share|improve this answer
























  • Thank you @Zorkolot, works great

    – Clem_Fandango
    Jan 4 at 16:03



















3














Isn't this a relatively simple where clause?



where not (datetime >= '2019-01-01' and datetime < '2019-01-05' and
(idnum like '%-_____' or
idnum like '%-0%'
)
)





share|improve this answer


























  • Isn't your WHERE clause missing the last condition: after the dash begin with a zero. I don't think his sample data needs this part, since the entries that begin with a 0 also are 5 characters after the dash.

    – Zack
    Jan 3 at 13:40











  • @Zack . . . Thank you.

    – Gordon Linoff
    Jan 3 at 13:55











  • Thank you, this is great, have marked @Zorkolot as the correct answer as he answered first

    – Clem_Fandango
    Jan 4 at 16:05



















1














Reading your question, you have 2 main criteria in your excluded data:




  1. The date is between 2019-01-01 and 2019-01-04, AND

  2. Either the length of the IDNum is 5 characters (after the dash) OR the IDNum (after the dash) begins with a 0.


As with @Gordon's answer, you can wrap this up in a NOT:



WHERE NOT
(
[DateTime] >= '2019-01-01'
AND [DateTime] <= '2019-01-04'
AND
(
IDNum LIKE '%-0%'
OR IDnum LIKE '%-_____'
)
)


With De Morgan's laws, we can simplify this a bit (or at least distribute the NOT):



WHERE
(
[DateTime] < '2019-01-01'
OR [DateTime] > '2019-01-04'
OR
(
IDNum NOT LIKE '%-0%'
AND IDnum NOT LIKE '%-_____'
)
)





share|improve this answer
























  • Thank you for the detailed response @Zack

    – Clem_Fandango
    Jan 4 at 16:06



















0














if your purpose is to meet this two following conditions:=




  1. DateTime should be in between 1st January to 4th January, 2019


  2. In IDNum the length of number after '-' should be more or equal than 5,
    then you probably no need of rtrim.



code:



Select * from Test where DateTime >= '01-01-2019' and DateTime <= '01-03-2019' and ((SUBSTRING(IDNum, 4, 1) = 0) or LEN(SUBSTRING(IDNum, 4, LEN(IDNum)-1)) >= 5);


though i am not sure how 31-12-2018 are present in your expected result. :D






share|improve this answer
























  • If dealing with the datetime datatype, doing <= '01-03-2019' cuts of the time portion, only including datetime at midnight. For example, it would not include 01-03-2019 06:00:00.000... Also, OP may want times included during the 4th leading up to the 5th. And then the intent is to exclude these datetimes.

    – Zorkolot
    Jan 3 at 13:36














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%2f54022116%2frtrim-in-where-clause%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Here's one way.



SELECT IDnum, DateTime
FROM YourTable
WHERE NOT (DateTime >= '2019-01-01' AND DateTime < '2019-01-05' --exclude from 1st Jan 2019 to 4th Jan 2019
AND (IDnum LIKE '%-_____' --that are 5 digits long after the dash
OR
IDnum LIKE '%-0%' --or begin with a 0 (after the dash)
)
)





share|improve this answer
























  • Thank you @Zorkolot, works great

    – Clem_Fandango
    Jan 4 at 16:03
















2














Here's one way.



SELECT IDnum, DateTime
FROM YourTable
WHERE NOT (DateTime >= '2019-01-01' AND DateTime < '2019-01-05' --exclude from 1st Jan 2019 to 4th Jan 2019
AND (IDnum LIKE '%-_____' --that are 5 digits long after the dash
OR
IDnum LIKE '%-0%' --or begin with a 0 (after the dash)
)
)





share|improve this answer
























  • Thank you @Zorkolot, works great

    – Clem_Fandango
    Jan 4 at 16:03














2












2








2







Here's one way.



SELECT IDnum, DateTime
FROM YourTable
WHERE NOT (DateTime >= '2019-01-01' AND DateTime < '2019-01-05' --exclude from 1st Jan 2019 to 4th Jan 2019
AND (IDnum LIKE '%-_____' --that are 5 digits long after the dash
OR
IDnum LIKE '%-0%' --or begin with a 0 (after the dash)
)
)





share|improve this answer













Here's one way.



SELECT IDnum, DateTime
FROM YourTable
WHERE NOT (DateTime >= '2019-01-01' AND DateTime < '2019-01-05' --exclude from 1st Jan 2019 to 4th Jan 2019
AND (IDnum LIKE '%-_____' --that are 5 digits long after the dash
OR
IDnum LIKE '%-0%' --or begin with a 0 (after the dash)
)
)






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 13:48









ZorkolotZorkolot

1,536178




1,536178













  • Thank you @Zorkolot, works great

    – Clem_Fandango
    Jan 4 at 16:03



















  • Thank you @Zorkolot, works great

    – Clem_Fandango
    Jan 4 at 16:03

















Thank you @Zorkolot, works great

– Clem_Fandango
Jan 4 at 16:03





Thank you @Zorkolot, works great

– Clem_Fandango
Jan 4 at 16:03













3














Isn't this a relatively simple where clause?



where not (datetime >= '2019-01-01' and datetime < '2019-01-05' and
(idnum like '%-_____' or
idnum like '%-0%'
)
)





share|improve this answer


























  • Isn't your WHERE clause missing the last condition: after the dash begin with a zero. I don't think his sample data needs this part, since the entries that begin with a 0 also are 5 characters after the dash.

    – Zack
    Jan 3 at 13:40











  • @Zack . . . Thank you.

    – Gordon Linoff
    Jan 3 at 13:55











  • Thank you, this is great, have marked @Zorkolot as the correct answer as he answered first

    – Clem_Fandango
    Jan 4 at 16:05
















3














Isn't this a relatively simple where clause?



where not (datetime >= '2019-01-01' and datetime < '2019-01-05' and
(idnum like '%-_____' or
idnum like '%-0%'
)
)





share|improve this answer


























  • Isn't your WHERE clause missing the last condition: after the dash begin with a zero. I don't think his sample data needs this part, since the entries that begin with a 0 also are 5 characters after the dash.

    – Zack
    Jan 3 at 13:40











  • @Zack . . . Thank you.

    – Gordon Linoff
    Jan 3 at 13:55











  • Thank you, this is great, have marked @Zorkolot as the correct answer as he answered first

    – Clem_Fandango
    Jan 4 at 16:05














3












3








3







Isn't this a relatively simple where clause?



where not (datetime >= '2019-01-01' and datetime < '2019-01-05' and
(idnum like '%-_____' or
idnum like '%-0%'
)
)





share|improve this answer















Isn't this a relatively simple where clause?



where not (datetime >= '2019-01-01' and datetime < '2019-01-05' and
(idnum like '%-_____' or
idnum like '%-0%'
)
)






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 13:54

























answered Jan 3 at 12:18









Gordon LinoffGordon Linoff

797k37318423




797k37318423













  • Isn't your WHERE clause missing the last condition: after the dash begin with a zero. I don't think his sample data needs this part, since the entries that begin with a 0 also are 5 characters after the dash.

    – Zack
    Jan 3 at 13:40











  • @Zack . . . Thank you.

    – Gordon Linoff
    Jan 3 at 13:55











  • Thank you, this is great, have marked @Zorkolot as the correct answer as he answered first

    – Clem_Fandango
    Jan 4 at 16:05



















  • Isn't your WHERE clause missing the last condition: after the dash begin with a zero. I don't think his sample data needs this part, since the entries that begin with a 0 also are 5 characters after the dash.

    – Zack
    Jan 3 at 13:40











  • @Zack . . . Thank you.

    – Gordon Linoff
    Jan 3 at 13:55











  • Thank you, this is great, have marked @Zorkolot as the correct answer as he answered first

    – Clem_Fandango
    Jan 4 at 16:05

















Isn't your WHERE clause missing the last condition: after the dash begin with a zero. I don't think his sample data needs this part, since the entries that begin with a 0 also are 5 characters after the dash.

– Zack
Jan 3 at 13:40





Isn't your WHERE clause missing the last condition: after the dash begin with a zero. I don't think his sample data needs this part, since the entries that begin with a 0 also are 5 characters after the dash.

– Zack
Jan 3 at 13:40













@Zack . . . Thank you.

– Gordon Linoff
Jan 3 at 13:55





@Zack . . . Thank you.

– Gordon Linoff
Jan 3 at 13:55













Thank you, this is great, have marked @Zorkolot as the correct answer as he answered first

– Clem_Fandango
Jan 4 at 16:05





Thank you, this is great, have marked @Zorkolot as the correct answer as he answered first

– Clem_Fandango
Jan 4 at 16:05











1














Reading your question, you have 2 main criteria in your excluded data:




  1. The date is between 2019-01-01 and 2019-01-04, AND

  2. Either the length of the IDNum is 5 characters (after the dash) OR the IDNum (after the dash) begins with a 0.


As with @Gordon's answer, you can wrap this up in a NOT:



WHERE NOT
(
[DateTime] >= '2019-01-01'
AND [DateTime] <= '2019-01-04'
AND
(
IDNum LIKE '%-0%'
OR IDnum LIKE '%-_____'
)
)


With De Morgan's laws, we can simplify this a bit (or at least distribute the NOT):



WHERE
(
[DateTime] < '2019-01-01'
OR [DateTime] > '2019-01-04'
OR
(
IDNum NOT LIKE '%-0%'
AND IDnum NOT LIKE '%-_____'
)
)





share|improve this answer
























  • Thank you for the detailed response @Zack

    – Clem_Fandango
    Jan 4 at 16:06
















1














Reading your question, you have 2 main criteria in your excluded data:




  1. The date is between 2019-01-01 and 2019-01-04, AND

  2. Either the length of the IDNum is 5 characters (after the dash) OR the IDNum (after the dash) begins with a 0.


As with @Gordon's answer, you can wrap this up in a NOT:



WHERE NOT
(
[DateTime] >= '2019-01-01'
AND [DateTime] <= '2019-01-04'
AND
(
IDNum LIKE '%-0%'
OR IDnum LIKE '%-_____'
)
)


With De Morgan's laws, we can simplify this a bit (or at least distribute the NOT):



WHERE
(
[DateTime] < '2019-01-01'
OR [DateTime] > '2019-01-04'
OR
(
IDNum NOT LIKE '%-0%'
AND IDnum NOT LIKE '%-_____'
)
)





share|improve this answer
























  • Thank you for the detailed response @Zack

    – Clem_Fandango
    Jan 4 at 16:06














1












1








1







Reading your question, you have 2 main criteria in your excluded data:




  1. The date is between 2019-01-01 and 2019-01-04, AND

  2. Either the length of the IDNum is 5 characters (after the dash) OR the IDNum (after the dash) begins with a 0.


As with @Gordon's answer, you can wrap this up in a NOT:



WHERE NOT
(
[DateTime] >= '2019-01-01'
AND [DateTime] <= '2019-01-04'
AND
(
IDNum LIKE '%-0%'
OR IDnum LIKE '%-_____'
)
)


With De Morgan's laws, we can simplify this a bit (or at least distribute the NOT):



WHERE
(
[DateTime] < '2019-01-01'
OR [DateTime] > '2019-01-04'
OR
(
IDNum NOT LIKE '%-0%'
AND IDnum NOT LIKE '%-_____'
)
)





share|improve this answer













Reading your question, you have 2 main criteria in your excluded data:




  1. The date is between 2019-01-01 and 2019-01-04, AND

  2. Either the length of the IDNum is 5 characters (after the dash) OR the IDNum (after the dash) begins with a 0.


As with @Gordon's answer, you can wrap this up in a NOT:



WHERE NOT
(
[DateTime] >= '2019-01-01'
AND [DateTime] <= '2019-01-04'
AND
(
IDNum LIKE '%-0%'
OR IDnum LIKE '%-_____'
)
)


With De Morgan's laws, we can simplify this a bit (or at least distribute the NOT):



WHERE
(
[DateTime] < '2019-01-01'
OR [DateTime] > '2019-01-04'
OR
(
IDNum NOT LIKE '%-0%'
AND IDnum NOT LIKE '%-_____'
)
)






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 13:50









ZackZack

1,6241212




1,6241212













  • Thank you for the detailed response @Zack

    – Clem_Fandango
    Jan 4 at 16:06



















  • Thank you for the detailed response @Zack

    – Clem_Fandango
    Jan 4 at 16:06

















Thank you for the detailed response @Zack

– Clem_Fandango
Jan 4 at 16:06





Thank you for the detailed response @Zack

– Clem_Fandango
Jan 4 at 16:06











0














if your purpose is to meet this two following conditions:=




  1. DateTime should be in between 1st January to 4th January, 2019


  2. In IDNum the length of number after '-' should be more or equal than 5,
    then you probably no need of rtrim.



code:



Select * from Test where DateTime >= '01-01-2019' and DateTime <= '01-03-2019' and ((SUBSTRING(IDNum, 4, 1) = 0) or LEN(SUBSTRING(IDNum, 4, LEN(IDNum)-1)) >= 5);


though i am not sure how 31-12-2018 are present in your expected result. :D






share|improve this answer
























  • If dealing with the datetime datatype, doing <= '01-03-2019' cuts of the time portion, only including datetime at midnight. For example, it would not include 01-03-2019 06:00:00.000... Also, OP may want times included during the 4th leading up to the 5th. And then the intent is to exclude these datetimes.

    – Zorkolot
    Jan 3 at 13:36


















0














if your purpose is to meet this two following conditions:=




  1. DateTime should be in between 1st January to 4th January, 2019


  2. In IDNum the length of number after '-' should be more or equal than 5,
    then you probably no need of rtrim.



code:



Select * from Test where DateTime >= '01-01-2019' and DateTime <= '01-03-2019' and ((SUBSTRING(IDNum, 4, 1) = 0) or LEN(SUBSTRING(IDNum, 4, LEN(IDNum)-1)) >= 5);


though i am not sure how 31-12-2018 are present in your expected result. :D






share|improve this answer
























  • If dealing with the datetime datatype, doing <= '01-03-2019' cuts of the time portion, only including datetime at midnight. For example, it would not include 01-03-2019 06:00:00.000... Also, OP may want times included during the 4th leading up to the 5th. And then the intent is to exclude these datetimes.

    – Zorkolot
    Jan 3 at 13:36
















0












0








0







if your purpose is to meet this two following conditions:=




  1. DateTime should be in between 1st January to 4th January, 2019


  2. In IDNum the length of number after '-' should be more or equal than 5,
    then you probably no need of rtrim.



code:



Select * from Test where DateTime >= '01-01-2019' and DateTime <= '01-03-2019' and ((SUBSTRING(IDNum, 4, 1) = 0) or LEN(SUBSTRING(IDNum, 4, LEN(IDNum)-1)) >= 5);


though i am not sure how 31-12-2018 are present in your expected result. :D






share|improve this answer













if your purpose is to meet this two following conditions:=




  1. DateTime should be in between 1st January to 4th January, 2019


  2. In IDNum the length of number after '-' should be more or equal than 5,
    then you probably no need of rtrim.



code:



Select * from Test where DateTime >= '01-01-2019' and DateTime <= '01-03-2019' and ((SUBSTRING(IDNum, 4, 1) = 0) or LEN(SUBSTRING(IDNum, 4, LEN(IDNum)-1)) >= 5);


though i am not sure how 31-12-2018 are present in your expected result. :D







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 13:03









ReturnZeroReturnZero

14210




14210













  • If dealing with the datetime datatype, doing <= '01-03-2019' cuts of the time portion, only including datetime at midnight. For example, it would not include 01-03-2019 06:00:00.000... Also, OP may want times included during the 4th leading up to the 5th. And then the intent is to exclude these datetimes.

    – Zorkolot
    Jan 3 at 13:36





















  • If dealing with the datetime datatype, doing <= '01-03-2019' cuts of the time portion, only including datetime at midnight. For example, it would not include 01-03-2019 06:00:00.000... Also, OP may want times included during the 4th leading up to the 5th. And then the intent is to exclude these datetimes.

    – Zorkolot
    Jan 3 at 13:36



















If dealing with the datetime datatype, doing <= '01-03-2019' cuts of the time portion, only including datetime at midnight. For example, it would not include 01-03-2019 06:00:00.000... Also, OP may want times included during the 4th leading up to the 5th. And then the intent is to exclude these datetimes.

– Zorkolot
Jan 3 at 13:36







If dealing with the datetime datatype, doing <= '01-03-2019' cuts of the time portion, only including datetime at midnight. For example, it would not include 01-03-2019 06:00:00.000... Also, OP may want times included during the 4th leading up to the 5th. And then the intent is to exclude these datetimes.

– Zorkolot
Jan 3 at 13:36




















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%2f54022116%2frtrim-in-where-clause%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

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

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