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 6367559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:33:49+00:00 2026-05-25T00:33:49+00:00

I came across a function in sqlite that I need to convert to postgres

  • 0

I came across a function in sqlite that I need to convert to postgres syntax. It’s date(date, '-'||strftime('%w',date)||' days').

Can anyone help me convert this sqlite segment to postgres?

  • 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-25T00:33:50+00:00Added an answer on May 25, 2026 at 12:33 am

    This SQLite:

    date(date, '-' || strftime('%w', date) || ' days')
    

    Is, AFAIK, subtracting the day-in-the-week number days (i.e. 0 for Sunday, 1 for Monday, …) from date and then converting the result back to a date; see the date function reference for details.

    I think the equivalent for PostgreSQL would be:

    d - extract(dow from d)::int
    

    where d is your date; subtracting an integer from a date subtracts that number of days. If d is a timestamp, then you might need to add some casting. There is date_trunc('week', 'd') as well but that starts numbering the days from Monday so you’d be off by one with that.


    Here’s a quick breakdown of the SQLite with the date variable replaced by d to avoid confusion with the date() function:

    date(d, '-' || strftime('%w', d) || ' days')
    

    First of all, || is the standard SQL string concatenation operator. The strftime function is a general purpose date and time formatter that comes from POSIX; the %w format specifier means “day of the week as a number with Sunday being day zero”; so the strftime call gives you 0 for Sunday, 1 for Monday, and so on up to 6 for Saturday. If d is a Tuesday, then the strftime call will yield 2 and the whole thing ends up as:

    date(d, '-2 days')
    

    The modifiers for the SQLite date function have various forms but '-2 days' means just what you’d think: subtract two days from d. The overall result is that you get d truncated to the week (where Sunday is considered to be the first day of the week).

    On the PostgreSQL side:

    d - extract(dow from d)::int
    

    we can start with extract; extract is used to extract specific parts of a date or time and dow means “day of the week as a number with Sunday being day zero”. Sound familiar? Then the ::int casts the DOW number to an integer and is needed because the DOW actually comes out as a double precision value and there is no operator defined for subtracting a double from a date in PostgreSQL; the cast can also be written in the standard form as cast(x as int). When you subtract an integer from a date in PostgreSQL, you subtract that many days; you can be more explicit by saying things like - interval '3 days' but that would just add more noise in this case so I opted for simplicity. If it is Tuesday, then our PostgreSQL version looks like:

    d - 2
    

    and that’s the same as:

    d - interval '2 days'
    

    And after the subtraction we’d be back on Sunday. There’s also date_trunc in PostgreSQL but that would truncate to Monday not Sunday.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So today I just came across the 'live()' function that binds any future and
I came across some legacy code that contains a function like this: LPCTSTR returnString()
Came across this error today. Wondering if anyone can tell me what it means:
I just came across that ANSI(ISO) ain't allowing nesting of function.. i want to
I am using this IP Validation Function that I came across while browsing, it
I was doing a Junit tutorial and I came across this normalize function that
I came across an is_equals() function in a C API at work that returned
I came across this piece of Java function to convert an XML node to
I came across this syntax recently for try-catch for function. struct A { int
I was reading about volatile member function and came across an affirmation that member

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.