Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 609597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:35:01+00:00 2026-05-13T17:35:01+00:00

I have a Ruby on Rails application with a PostgreSQL database; several tables have

  • 0

I have a Ruby on Rails application with a PostgreSQL database; several tables have created_at and updated_at timestamp attributes. When displayed, those dates are formatted in the user’s locale; for example, the timestamp 2009-10-15 16:30:00.435 becomes the string 15.10.2009 - 16:30 (the date format for this example being dd.mm.yyyy - hh.mm).

The requirement is that the user must be able to search for records by date, as if they were strings formatted in the current locale. For example, searching for 15.10.2009 would return records with dates on October 15th 2009, searching for 15.10 would return records with dates on October 15th of any year, searching for 15 would return all dates that match 15 (be it day, month or year). Since the user can use any part of a date as a search term, it cannot be converted to a date/timestamp for comparison.

One (slow) way would be to retrieve all records, format the dates, and perform the search on that. This could be sped up by retrieving only the id and dates at first, performing the search, and then fetching the data for the matching records; but it could still be slow for large numbers of rows.

Another (not database-agnostic) way would be to cast/format the dates to the right format in the database with PostgreSQL functions or operators, and have the database do the matching (with the PostgreSQL regexp operators or whatnot).

Is there a way to do this efficiently (without fetching all rows) in a database-agnostic way? Or do you think I am going in the wrong direction and should approach the problem differently?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:35:01+00:00Added an answer on May 13, 2026 at 5:35 pm

    Building on the answer from Carlos, this should allow all of your searches without full table scans if you have indexes on all the date and date part fields. Function-based indexes would be better for the date part columns, but I’m not using them since this should not be database-specific.

    CREATE TABLE mytable (
        col1 varchar(10),
        -- ...
        inserted_at timestamp,
        updated_at timestamp);
    
    INSERT INTO mytable
    VALUES
        ('a', '2010-01-02', NULL),
        ('b', '2009-01-02', '2010-01-03'),
        ('c', '2009-11-12', NULL),
        ('d', '2008-03-31', '2009-04-18');
    
    ALTER TABLE mytable
        ADD inserted_at_month integer,
        ADD inserted_at_day integer,
        ADD updated_at_month integer,
        ADD updated_at_day integer;
    
    -- you will have to find your own way to maintain these values...
    UPDATE mytable
    SET
        inserted_at_month = date_part('month', inserted_at),
        inserted_at_day = date_part('day', inserted_at),
        updated_at_month = date_part('month', updated_at),
        updated_at_day = date_part('day', updated_at);
    

    If the user enters only Year use WHERE Date BETWEEN ‘YYYY-01-01’ AND ‘YYYY-12-31’

    SELECT *
    FROM mytable
    WHERE
        inserted_at BETWEEN '2010-01-01' AND '2010-12-31'
        OR updated_at BETWEEN '2010-01-01' AND '2010-12-31';
    

    If the user enters Year and Month use WHERE Date BETWEEN ‘YYYY-MM-01’ AND ‘YYYY-MM-31’ (may need adjustment for 30/29/28)

    SELECT *
    FROM mytable
    WHERE
        inserted_at BETWEEN '2010-01-01' AND '2010-01-31'
        OR updated_at BETWEEN '2010-01-01' AND '2010-01-31';
    

    If the user enters the three values use SELECT …. WHERE Date = ‘YYYY-MM-DD’

    SELECT *
    FROM mytable
    WHERE
        inserted_at = '2009-11-12'
        OR updated_at = '2009-11-12';
    

    If the user enters Month and Day

    SELECT *
    FROM mytable
    WHERE
        inserted_at_month = 3
        OR inserted_at_day = 31
        OR updated_at_month = 3
        OR updated_at_day = 31;
    

    If the user enters Month or Day (you could optimize to not check values > 12 as a month)

    SELECT *
    FROM mytable
    WHERE
        inserted_at_month = 12
        OR inserted_at_day = 12
        OR updated_at_month = 12
        OR updated_at_day = 12;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Simply include() the file. The interpreter will drop out of… May 14, 2026 at 10:29 pm
  • Editorial Team
    Editorial Team added an answer <configuration> <configFile>path/to/yourConfigFile.xml</configFile> </configuration> https://docs.sonatype.org/display/FLEXMOJOS/compile-swf-mojo.html#compile-swf-mojo.html-configFile May 14, 2026 at 10:29 pm
  • Editorial Team
    Editorial Team added an answer Maybe not the most efficient way but $html = new… May 14, 2026 at 10:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.