Year query returns no results












1















I have a Holiday model, with a holiday_date attribute of DateTime.
I added a new Holiday (New Years Day) with a date of 1/1/2019.
When I do in the console Holiday.last, I see this:



#<Holiday id: 50, name: "New Years Day", holiday_date: "2018-12-31 23:00:00", created_at: "2018-11-13 13:15:54", updated_at: "2018-11-13 13:15:54">


So it is saved in UTC time, a day earlier. When I then do Holiday.last.holiday_date I get this:



Tue, 01 Jan 2019 00:00:00 CET +01:00


Great, the date is converted to our CET date and time. But when I query for a year like this:



Holiday.where("extract(year from holiday_date) = '2019'")


It returns no results. So it seems that there is no conversion to CET time with this query. How can I make sure that the query returns the holiday I added?










share|improve this question



























    1















    I have a Holiday model, with a holiday_date attribute of DateTime.
    I added a new Holiday (New Years Day) with a date of 1/1/2019.
    When I do in the console Holiday.last, I see this:



    #<Holiday id: 50, name: "New Years Day", holiday_date: "2018-12-31 23:00:00", created_at: "2018-11-13 13:15:54", updated_at: "2018-11-13 13:15:54">


    So it is saved in UTC time, a day earlier. When I then do Holiday.last.holiday_date I get this:



    Tue, 01 Jan 2019 00:00:00 CET +01:00


    Great, the date is converted to our CET date and time. But when I query for a year like this:



    Holiday.where("extract(year from holiday_date) = '2019'")


    It returns no results. So it seems that there is no conversion to CET time with this query. How can I make sure that the query returns the holiday I added?










    share|improve this question

























      1












      1








      1








      I have a Holiday model, with a holiday_date attribute of DateTime.
      I added a new Holiday (New Years Day) with a date of 1/1/2019.
      When I do in the console Holiday.last, I see this:



      #<Holiday id: 50, name: "New Years Day", holiday_date: "2018-12-31 23:00:00", created_at: "2018-11-13 13:15:54", updated_at: "2018-11-13 13:15:54">


      So it is saved in UTC time, a day earlier. When I then do Holiday.last.holiday_date I get this:



      Tue, 01 Jan 2019 00:00:00 CET +01:00


      Great, the date is converted to our CET date and time. But when I query for a year like this:



      Holiday.where("extract(year from holiday_date) = '2019'")


      It returns no results. So it seems that there is no conversion to CET time with this query. How can I make sure that the query returns the holiday I added?










      share|improve this question














      I have a Holiday model, with a holiday_date attribute of DateTime.
      I added a new Holiday (New Years Day) with a date of 1/1/2019.
      When I do in the console Holiday.last, I see this:



      #<Holiday id: 50, name: "New Years Day", holiday_date: "2018-12-31 23:00:00", created_at: "2018-11-13 13:15:54", updated_at: "2018-11-13 13:15:54">


      So it is saved in UTC time, a day earlier. When I then do Holiday.last.holiday_date I get this:



      Tue, 01 Jan 2019 00:00:00 CET +01:00


      Great, the date is converted to our CET date and time. But when I query for a year like this:



      Holiday.where("extract(year from holiday_date) = '2019'")


      It returns no results. So it seems that there is no conversion to CET time with this query. How can I make sure that the query returns the holiday I added?







      ruby-on-rails






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 13:37









      JohnJohn

      2,31783775




      2,31783775
























          2 Answers
          2






          active

          oldest

          votes


















          2














          You'll have to cast timezones twice:



          Holiday.where(
          "extract(year from holiday_date AT TIME ZONE 'UTC' AT TIME ZONE 'CET') = '2019'"
          )


          This will work, but it would be nice to use indices for your query, we'll just have to prepare it better:



          year = Time.zone.parse("2019-01-01")
          Holiday.where("holiday_date BETWEEN ? AND ?", year.beginning_of_year, year.end_of_year)
          # SELECT "holidays".* FROM "holidays" WHERE (holiday_date BETWEEN '2018-12-31 23:00:00' AND '2019-12-31 22:59:59.999999')


          I would really think whether you need datetimes for your holiday_date column, perhaps dates would be enough, so that you don't have to deal with timezones.






          share|improve this answer


























          • Thanks! Your last remark is correct, it should have been a date.

            – John
            Nov 14 '18 at 5:57



















          1














          You can query by timezone like this



          Holiday.where("extract(year from holiday_date AT TIME ZONE 'CET') = '2019'")





          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',
            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%2f53282264%2fyear-query-returns-no-results%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









            2














            You'll have to cast timezones twice:



            Holiday.where(
            "extract(year from holiday_date AT TIME ZONE 'UTC' AT TIME ZONE 'CET') = '2019'"
            )


            This will work, but it would be nice to use indices for your query, we'll just have to prepare it better:



            year = Time.zone.parse("2019-01-01")
            Holiday.where("holiday_date BETWEEN ? AND ?", year.beginning_of_year, year.end_of_year)
            # SELECT "holidays".* FROM "holidays" WHERE (holiday_date BETWEEN '2018-12-31 23:00:00' AND '2019-12-31 22:59:59.999999')


            I would really think whether you need datetimes for your holiday_date column, perhaps dates would be enough, so that you don't have to deal with timezones.






            share|improve this answer


























            • Thanks! Your last remark is correct, it should have been a date.

              – John
              Nov 14 '18 at 5:57
















            2














            You'll have to cast timezones twice:



            Holiday.where(
            "extract(year from holiday_date AT TIME ZONE 'UTC' AT TIME ZONE 'CET') = '2019'"
            )


            This will work, but it would be nice to use indices for your query, we'll just have to prepare it better:



            year = Time.zone.parse("2019-01-01")
            Holiday.where("holiday_date BETWEEN ? AND ?", year.beginning_of_year, year.end_of_year)
            # SELECT "holidays".* FROM "holidays" WHERE (holiday_date BETWEEN '2018-12-31 23:00:00' AND '2019-12-31 22:59:59.999999')


            I would really think whether you need datetimes for your holiday_date column, perhaps dates would be enough, so that you don't have to deal with timezones.






            share|improve this answer


























            • Thanks! Your last remark is correct, it should have been a date.

              – John
              Nov 14 '18 at 5:57














            2












            2








            2







            You'll have to cast timezones twice:



            Holiday.where(
            "extract(year from holiday_date AT TIME ZONE 'UTC' AT TIME ZONE 'CET') = '2019'"
            )


            This will work, but it would be nice to use indices for your query, we'll just have to prepare it better:



            year = Time.zone.parse("2019-01-01")
            Holiday.where("holiday_date BETWEEN ? AND ?", year.beginning_of_year, year.end_of_year)
            # SELECT "holidays".* FROM "holidays" WHERE (holiday_date BETWEEN '2018-12-31 23:00:00' AND '2019-12-31 22:59:59.999999')


            I would really think whether you need datetimes for your holiday_date column, perhaps dates would be enough, so that you don't have to deal with timezones.






            share|improve this answer















            You'll have to cast timezones twice:



            Holiday.where(
            "extract(year from holiday_date AT TIME ZONE 'UTC' AT TIME ZONE 'CET') = '2019'"
            )


            This will work, but it would be nice to use indices for your query, we'll just have to prepare it better:



            year = Time.zone.parse("2019-01-01")
            Holiday.where("holiday_date BETWEEN ? AND ?", year.beginning_of_year, year.end_of_year)
            # SELECT "holidays".* FROM "holidays" WHERE (holiday_date BETWEEN '2018-12-31 23:00:00' AND '2019-12-31 22:59:59.999999')


            I would really think whether you need datetimes for your holiday_date column, perhaps dates would be enough, so that you don't have to deal with timezones.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 15:29

























            answered Nov 13 '18 at 14:05









            Marcin KołodziejMarcin Kołodziej

            4,2961315




            4,2961315













            • Thanks! Your last remark is correct, it should have been a date.

              – John
              Nov 14 '18 at 5:57



















            • Thanks! Your last remark is correct, it should have been a date.

              – John
              Nov 14 '18 at 5:57

















            Thanks! Your last remark is correct, it should have been a date.

            – John
            Nov 14 '18 at 5:57





            Thanks! Your last remark is correct, it should have been a date.

            – John
            Nov 14 '18 at 5:57













            1














            You can query by timezone like this



            Holiday.where("extract(year from holiday_date AT TIME ZONE 'CET') = '2019'")





            share|improve this answer




























              1














              You can query by timezone like this



              Holiday.where("extract(year from holiday_date AT TIME ZONE 'CET') = '2019'")





              share|improve this answer


























                1












                1








                1







                You can query by timezone like this



                Holiday.where("extract(year from holiday_date AT TIME ZONE 'CET') = '2019'")





                share|improve this answer













                You can query by timezone like this



                Holiday.where("extract(year from holiday_date AT TIME ZONE 'CET') = '2019'")






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 14:01









                Vijay AtminVijay Atmin

                16410




                16410






























                    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%2f53282264%2fyear-query-returns-no-results%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

                    List item for chat from Array inside array React Native

                    Thiostrepton

                    Caerphilly