SQL select statement is finding something that should not exist in a view











up vote
0
down vote

favorite












I have this sql statement for making a view



create view RecordYearsTwo 
as
select
Record.RecordID, RecordValue.Value
from
Record
join
RecordValue on Record.RecordID = RecordValue.RecordID
where
len(RecordValue.Value) = 4
and RecordValue.Value like '[16-20][0-9][0-9][0-9]%'
and RecordValue.Value like '%[16-20][0-9][0-9][0-9]'
and RecordValue.Value != '26 Mar 1850';


When I then run



select * 
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where cast(RecordYearsTwo.Value as int) >= 1800


I get this error




Conversion failed when converting the nvarchar value '26 Mar 1850' to data type int.




My understanding is that '26 Mar 1850' shouldn't even exist in my view because the length of everything in my view should be 4 and I specifically said should not equal '26 Mar 1850'



Any ideas?










share|improve this question
























  • Are you sure you dropped the view, then re-created it? Side note: It would be better to be storing the date in a SARGable format, preferrably ISO: YYYY-MM-DD (so, '1850-03-26, assuming you can't store it as a date object). This would prevent you from even needing to do the conversion, among other things.
    – Clockwork-Muse
    Nov 21 at 18:45






  • 1




    SQL Server could decide to merge the where clauses together.
    – Salman A
    Nov 21 at 20:29






  • 1




    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. In this case, the question is lacking the table definitions. For example, if RecordValue.Value is of type int, that might cause this error. Please provide a Minimal, Complete, and Verifiable example.
    – Richardissimo
    Nov 21 at 20:44















up vote
0
down vote

favorite












I have this sql statement for making a view



create view RecordYearsTwo 
as
select
Record.RecordID, RecordValue.Value
from
Record
join
RecordValue on Record.RecordID = RecordValue.RecordID
where
len(RecordValue.Value) = 4
and RecordValue.Value like '[16-20][0-9][0-9][0-9]%'
and RecordValue.Value like '%[16-20][0-9][0-9][0-9]'
and RecordValue.Value != '26 Mar 1850';


When I then run



select * 
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where cast(RecordYearsTwo.Value as int) >= 1800


I get this error




Conversion failed when converting the nvarchar value '26 Mar 1850' to data type int.




My understanding is that '26 Mar 1850' shouldn't even exist in my view because the length of everything in my view should be 4 and I specifically said should not equal '26 Mar 1850'



Any ideas?










share|improve this question
























  • Are you sure you dropped the view, then re-created it? Side note: It would be better to be storing the date in a SARGable format, preferrably ISO: YYYY-MM-DD (so, '1850-03-26, assuming you can't store it as a date object). This would prevent you from even needing to do the conversion, among other things.
    – Clockwork-Muse
    Nov 21 at 18:45






  • 1




    SQL Server could decide to merge the where clauses together.
    – Salman A
    Nov 21 at 20:29






  • 1




    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. In this case, the question is lacking the table definitions. For example, if RecordValue.Value is of type int, that might cause this error. Please provide a Minimal, Complete, and Verifiable example.
    – Richardissimo
    Nov 21 at 20:44













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have this sql statement for making a view



create view RecordYearsTwo 
as
select
Record.RecordID, RecordValue.Value
from
Record
join
RecordValue on Record.RecordID = RecordValue.RecordID
where
len(RecordValue.Value) = 4
and RecordValue.Value like '[16-20][0-9][0-9][0-9]%'
and RecordValue.Value like '%[16-20][0-9][0-9][0-9]'
and RecordValue.Value != '26 Mar 1850';


When I then run



select * 
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where cast(RecordYearsTwo.Value as int) >= 1800


I get this error




Conversion failed when converting the nvarchar value '26 Mar 1850' to data type int.




My understanding is that '26 Mar 1850' shouldn't even exist in my view because the length of everything in my view should be 4 and I specifically said should not equal '26 Mar 1850'



Any ideas?










share|improve this question















I have this sql statement for making a view



create view RecordYearsTwo 
as
select
Record.RecordID, RecordValue.Value
from
Record
join
RecordValue on Record.RecordID = RecordValue.RecordID
where
len(RecordValue.Value) = 4
and RecordValue.Value like '[16-20][0-9][0-9][0-9]%'
and RecordValue.Value like '%[16-20][0-9][0-9][0-9]'
and RecordValue.Value != '26 Mar 1850';


When I then run



select * 
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where cast(RecordYearsTwo.Value as int) >= 1800


I get this error




Conversion failed when converting the nvarchar value '26 Mar 1850' to data type int.




My understanding is that '26 Mar 1850' shouldn't even exist in my view because the length of everything in my view should be 4 and I specifically said should not equal '26 Mar 1850'



Any ideas?







sql sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 0:28









Eric Brandt

2,0901521




2,0901521










asked Nov 21 at 18:41









Jared Smith

312




312












  • Are you sure you dropped the view, then re-created it? Side note: It would be better to be storing the date in a SARGable format, preferrably ISO: YYYY-MM-DD (so, '1850-03-26, assuming you can't store it as a date object). This would prevent you from even needing to do the conversion, among other things.
    – Clockwork-Muse
    Nov 21 at 18:45






  • 1




    SQL Server could decide to merge the where clauses together.
    – Salman A
    Nov 21 at 20:29






  • 1




    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. In this case, the question is lacking the table definitions. For example, if RecordValue.Value is of type int, that might cause this error. Please provide a Minimal, Complete, and Verifiable example.
    – Richardissimo
    Nov 21 at 20:44


















  • Are you sure you dropped the view, then re-created it? Side note: It would be better to be storing the date in a SARGable format, preferrably ISO: YYYY-MM-DD (so, '1850-03-26, assuming you can't store it as a date object). This would prevent you from even needing to do the conversion, among other things.
    – Clockwork-Muse
    Nov 21 at 18:45






  • 1




    SQL Server could decide to merge the where clauses together.
    – Salman A
    Nov 21 at 20:29






  • 1




    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. In this case, the question is lacking the table definitions. For example, if RecordValue.Value is of type int, that might cause this error. Please provide a Minimal, Complete, and Verifiable example.
    – Richardissimo
    Nov 21 at 20:44
















Are you sure you dropped the view, then re-created it? Side note: It would be better to be storing the date in a SARGable format, preferrably ISO: YYYY-MM-DD (so, '1850-03-26, assuming you can't store it as a date object). This would prevent you from even needing to do the conversion, among other things.
– Clockwork-Muse
Nov 21 at 18:45




Are you sure you dropped the view, then re-created it? Side note: It would be better to be storing the date in a SARGable format, preferrably ISO: YYYY-MM-DD (so, '1850-03-26, assuming you can't store it as a date object). This would prevent you from even needing to do the conversion, among other things.
– Clockwork-Muse
Nov 21 at 18:45




1




1




SQL Server could decide to merge the where clauses together.
– Salman A
Nov 21 at 20:29




SQL Server could decide to merge the where clauses together.
– Salman A
Nov 21 at 20:29




1




1




Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. In this case, the question is lacking the table definitions. For example, if RecordValue.Value is of type int, that might cause this error. Please provide a Minimal, Complete, and Verifiable example.
– Richardissimo
Nov 21 at 20:44




Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. In this case, the question is lacking the table definitions. For example, if RecordValue.Value is of type int, that might cause this error. Please provide a Minimal, Complete, and Verifiable example.
– Richardissimo
Nov 21 at 20:44












3 Answers
3






active

oldest

votes

















up vote
1
down vote













The criteria for that datestamp isn't needed.

Because even the first criteria wouldn't accept it (to long).



And those LIKE criteria don't need the % if only 4 characters are expected.



create view RecordYearsTwo as
select rec.RecordID, val.Value
from Record rec
join RecordValue val on val.RecordID = rec.RecordID
where len(val.Value) = 4
and (val.Value like '1[6-9][0-9][0-9]' or val.Value like '20[0-9][0-9]')


And to avoid the error you could use TRY_CAST instead.



select * 
from Record
join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
where try_cast(RecordYearsTwo.Value as int) >= 1800





share|improve this answer






























    up vote
    0
    down vote













    With multiple expressions in the where clause, you can't guarantee what order they get executed in. Try:



    create view RecordYearsTwo as
    select r.RecordID, v.Value
    from Record r
    join (
    select *
    from RecordValue
    where len(Value) = 4
    ) v on r.RecordID = v.RecordID
    where v.Value like '[16-20][0-9][0-9][0-9]%'
    and v.Value like '%[16-20][0-9][0-9][0-9]'





    share|improve this answer




























      up vote
      0
      down vote













      First, your view conditions do not make sense. I think you want:



      create view RecordYearsTwo as
      select r.RecordID, rv.Value
      from Record r join
      RecordValue rv
      on r.RecordID = rv.RecordID
      where len(rv.Value) = 4
      try_convert(int, rv.Value) >= 1600 and
      try_convert(int, rv.Value) < 2100;


      Your logic doesn't make sense. I find this amusing: 'v.Value like '[16-20][0-9][0-9][0-9]%'. That like pattern says to get any '1', any character between '6' and '2' (which is none), and any '0'. I understand what you mean, but SQL Server does not.



      Then, the view does not get executed first. You have no idea what the order of execution is, so for your query, you want try_convert() again:



      select * 
      from Record join r
      RecordYearsTwo ry2
      on r.RecordID = ry2.RecordID
      where try_convert(int, ry2.Value) >= 1800





      share|improve this answer





















        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',
        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%2f53418620%2fsql-select-statement-is-finding-something-that-should-not-exist-in-a-view%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








        up vote
        1
        down vote













        The criteria for that datestamp isn't needed.

        Because even the first criteria wouldn't accept it (to long).



        And those LIKE criteria don't need the % if only 4 characters are expected.



        create view RecordYearsTwo as
        select rec.RecordID, val.Value
        from Record rec
        join RecordValue val on val.RecordID = rec.RecordID
        where len(val.Value) = 4
        and (val.Value like '1[6-9][0-9][0-9]' or val.Value like '20[0-9][0-9]')


        And to avoid the error you could use TRY_CAST instead.



        select * 
        from Record
        join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
        where try_cast(RecordYearsTwo.Value as int) >= 1800





        share|improve this answer



























          up vote
          1
          down vote













          The criteria for that datestamp isn't needed.

          Because even the first criteria wouldn't accept it (to long).



          And those LIKE criteria don't need the % if only 4 characters are expected.



          create view RecordYearsTwo as
          select rec.RecordID, val.Value
          from Record rec
          join RecordValue val on val.RecordID = rec.RecordID
          where len(val.Value) = 4
          and (val.Value like '1[6-9][0-9][0-9]' or val.Value like '20[0-9][0-9]')


          And to avoid the error you could use TRY_CAST instead.



          select * 
          from Record
          join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
          where try_cast(RecordYearsTwo.Value as int) >= 1800





          share|improve this answer

























            up vote
            1
            down vote










            up vote
            1
            down vote









            The criteria for that datestamp isn't needed.

            Because even the first criteria wouldn't accept it (to long).



            And those LIKE criteria don't need the % if only 4 characters are expected.



            create view RecordYearsTwo as
            select rec.RecordID, val.Value
            from Record rec
            join RecordValue val on val.RecordID = rec.RecordID
            where len(val.Value) = 4
            and (val.Value like '1[6-9][0-9][0-9]' or val.Value like '20[0-9][0-9]')


            And to avoid the error you could use TRY_CAST instead.



            select * 
            from Record
            join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
            where try_cast(RecordYearsTwo.Value as int) >= 1800





            share|improve this answer














            The criteria for that datestamp isn't needed.

            Because even the first criteria wouldn't accept it (to long).



            And those LIKE criteria don't need the % if only 4 characters are expected.



            create view RecordYearsTwo as
            select rec.RecordID, val.Value
            from Record rec
            join RecordValue val on val.RecordID = rec.RecordID
            where len(val.Value) = 4
            and (val.Value like '1[6-9][0-9][0-9]' or val.Value like '20[0-9][0-9]')


            And to avoid the error you could use TRY_CAST instead.



            select * 
            from Record
            join RecordYearsTwo on Record.RecordID = RecordYearsTwo.RecordID
            where try_cast(RecordYearsTwo.Value as int) >= 1800






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 at 21:02

























            answered Nov 21 at 20:47









            LukStorms

            11.1k31532




            11.1k31532
























                up vote
                0
                down vote













                With multiple expressions in the where clause, you can't guarantee what order they get executed in. Try:



                create view RecordYearsTwo as
                select r.RecordID, v.Value
                from Record r
                join (
                select *
                from RecordValue
                where len(Value) = 4
                ) v on r.RecordID = v.RecordID
                where v.Value like '[16-20][0-9][0-9][0-9]%'
                and v.Value like '%[16-20][0-9][0-9][0-9]'





                share|improve this answer

























                  up vote
                  0
                  down vote













                  With multiple expressions in the where clause, you can't guarantee what order they get executed in. Try:



                  create view RecordYearsTwo as
                  select r.RecordID, v.Value
                  from Record r
                  join (
                  select *
                  from RecordValue
                  where len(Value) = 4
                  ) v on r.RecordID = v.RecordID
                  where v.Value like '[16-20][0-9][0-9][0-9]%'
                  and v.Value like '%[16-20][0-9][0-9][0-9]'





                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    With multiple expressions in the where clause, you can't guarantee what order they get executed in. Try:



                    create view RecordYearsTwo as
                    select r.RecordID, v.Value
                    from Record r
                    join (
                    select *
                    from RecordValue
                    where len(Value) = 4
                    ) v on r.RecordID = v.RecordID
                    where v.Value like '[16-20][0-9][0-9][0-9]%'
                    and v.Value like '%[16-20][0-9][0-9][0-9]'





                    share|improve this answer












                    With multiple expressions in the where clause, you can't guarantee what order they get executed in. Try:



                    create view RecordYearsTwo as
                    select r.RecordID, v.Value
                    from Record r
                    join (
                    select *
                    from RecordValue
                    where len(Value) = 4
                    ) v on r.RecordID = v.RecordID
                    where v.Value like '[16-20][0-9][0-9][0-9]%'
                    and v.Value like '%[16-20][0-9][0-9][0-9]'






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 at 18:47









                    RedFilter

                    133k30241253




                    133k30241253






















                        up vote
                        0
                        down vote













                        First, your view conditions do not make sense. I think you want:



                        create view RecordYearsTwo as
                        select r.RecordID, rv.Value
                        from Record r join
                        RecordValue rv
                        on r.RecordID = rv.RecordID
                        where len(rv.Value) = 4
                        try_convert(int, rv.Value) >= 1600 and
                        try_convert(int, rv.Value) < 2100;


                        Your logic doesn't make sense. I find this amusing: 'v.Value like '[16-20][0-9][0-9][0-9]%'. That like pattern says to get any '1', any character between '6' and '2' (which is none), and any '0'. I understand what you mean, but SQL Server does not.



                        Then, the view does not get executed first. You have no idea what the order of execution is, so for your query, you want try_convert() again:



                        select * 
                        from Record join r
                        RecordYearsTwo ry2
                        on r.RecordID = ry2.RecordID
                        where try_convert(int, ry2.Value) >= 1800





                        share|improve this answer

























                          up vote
                          0
                          down vote













                          First, your view conditions do not make sense. I think you want:



                          create view RecordYearsTwo as
                          select r.RecordID, rv.Value
                          from Record r join
                          RecordValue rv
                          on r.RecordID = rv.RecordID
                          where len(rv.Value) = 4
                          try_convert(int, rv.Value) >= 1600 and
                          try_convert(int, rv.Value) < 2100;


                          Your logic doesn't make sense. I find this amusing: 'v.Value like '[16-20][0-9][0-9][0-9]%'. That like pattern says to get any '1', any character between '6' and '2' (which is none), and any '0'. I understand what you mean, but SQL Server does not.



                          Then, the view does not get executed first. You have no idea what the order of execution is, so for your query, you want try_convert() again:



                          select * 
                          from Record join r
                          RecordYearsTwo ry2
                          on r.RecordID = ry2.RecordID
                          where try_convert(int, ry2.Value) >= 1800





                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            First, your view conditions do not make sense. I think you want:



                            create view RecordYearsTwo as
                            select r.RecordID, rv.Value
                            from Record r join
                            RecordValue rv
                            on r.RecordID = rv.RecordID
                            where len(rv.Value) = 4
                            try_convert(int, rv.Value) >= 1600 and
                            try_convert(int, rv.Value) < 2100;


                            Your logic doesn't make sense. I find this amusing: 'v.Value like '[16-20][0-9][0-9][0-9]%'. That like pattern says to get any '1', any character between '6' and '2' (which is none), and any '0'. I understand what you mean, but SQL Server does not.



                            Then, the view does not get executed first. You have no idea what the order of execution is, so for your query, you want try_convert() again:



                            select * 
                            from Record join r
                            RecordYearsTwo ry2
                            on r.RecordID = ry2.RecordID
                            where try_convert(int, ry2.Value) >= 1800





                            share|improve this answer












                            First, your view conditions do not make sense. I think you want:



                            create view RecordYearsTwo as
                            select r.RecordID, rv.Value
                            from Record r join
                            RecordValue rv
                            on r.RecordID = rv.RecordID
                            where len(rv.Value) = 4
                            try_convert(int, rv.Value) >= 1600 and
                            try_convert(int, rv.Value) < 2100;


                            Your logic doesn't make sense. I find this amusing: 'v.Value like '[16-20][0-9][0-9][0-9]%'. That like pattern says to get any '1', any character between '6' and '2' (which is none), and any '0'. I understand what you mean, but SQL Server does not.



                            Then, the view does not get executed first. You have no idea what the order of execution is, so for your query, you want try_convert() again:



                            select * 
                            from Record join r
                            RecordYearsTwo ry2
                            on r.RecordID = ry2.RecordID
                            where try_convert(int, ry2.Value) >= 1800






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 at 22:54









                            Gordon Linoff

                            751k34286394




                            751k34286394






























                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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%2f53418620%2fsql-select-statement-is-finding-something-that-should-not-exist-in-a-view%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

                                Berounka

                                Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

                                Sphinx de Gizeh