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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:38:26+00:00 2026-06-15T22:38:26+00:00

I’m trying to get difference in days, casting result to decimal: SELECT CAST( TO_DATE(‘2999-01-01′,’yyyy-mm-dd’)

  • 0

I’m trying to get difference in days, casting result to decimal:

SELECT
CAST( TO_DATE('2999-01-01','yyyy-mm-dd') - TO_DATE('2909-01-01','yyyy-mm-dd') AS DECIMAL )
;

Now if I add 1 month to the 2nd date:

SELECT
CAST( TO_DATE('2999-01-01','yyyy-mm-dd') - (TO_DATE('2909-01-01','yyyy-mm-dd') + INTERVAL '1 MONTH' * (1) ) AS DECIMAL )
;

I’m getting an error:
ERROR: cannot cast type interval to numeric

OK, I can cast to char to get result:

SELECT
CAST( TO_CHAR( TO_DATE('2909-02-10','yyyy-mm-dd') - (TO_DATE('2909-01-01','yyyy-mm-dd') + INTERVAL '1 MONTH' * (1) ), 'DD') AS DECIMAL )
;

But in this case the 1st query modified with TO_CHAR casting stop working:

SELECT
CAST( TO_CHAR(TO_DATE('2999-01-01','yyyy-mm-dd') - TO_DATE('2909-01-01','yyyy-mm-dd'), 'DD') AS DECIMAL )
;

I’m getting ERROR: multiple decimal points.

So, my question is, how can I get days using the same sql statement? For both sql queries.

  • 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-06-15T22:38:27+00:00Added an answer on June 15, 2026 at 10:38 pm

    Look at your first two examples again. If you remove the outer CAST … AS DECIMAL you get

     ?column? 
    ----------
        32872
    
      ?column?  
    ------------
     32841 days
    

    Clearly the difference is in the “days”. The second is an interval value rather than a simple number. You only want the number (because you always just want days) so you need to extract that part. Then you can cast to whatever precision you like:

    SELECT extract(days FROM '32841 days'::interval)::numeric(9,2);
     date_part 
    -----------
      32841.00
    

    Edit responding to Alexandr’s follow-up:

    Your first example fails with a fairly specific error:

    SELECT extract(days FROM (TO_DATE('2999-01-01','yyyy-mm-dd') - TO_DATE('2909-01-01','yyyy-mm-dd'))::interval)::numeric(9,2);
    
    ERROR:  cannot cast type integer to interval
    LINE 1: ...yyyy-mm-dd') - TO_DATE('2909-01-01','yyyy-mm-dd'))::interval...
    

    Here you’ve got an integer (which is what you originally wanted) and try to cast it to an interval (for reasons I don’t understand). It’s complaining it doesn’t know what units you want. You want 32872 what in your interval – seconds, hours, weeks, centuries?

    The second example is complaining because you are trying to extract the “day” part from a simple integer, and of course there’s no extract() function in the system to do that.

    I think you probably need to take a step back and just take the time to understand the values your various expressions return.

    Subtracting one date from another gives the number of days separating them – as an integer. There is no other sensible measure, really.

    Adding (or subtracting) an interval to a date gives you a timestamp (without time zone) since the interval may contain whole days, days and hours, seconds etc.

    Subtracting a timestamp from a date will give you an interval since the result may contain days, hours, seconds etc.

    If you have an interval and you just want the days part then you use extract() on it and you will get an integer number of days back.

    You will need an integer (or floating-point) number of days if you want to cast to numeric, not an interval because casting an interval to an scalar number makes no sense without units.

    So – either stick to dates and date arithmetic (easy), or realise you are using timestamps (flexible) but understand which it is.

    To get an illustration of what’s happening you can do something like this (in psql):

    CREATE TEMP TABLE tt AS SELECT
      ('2909-01-02'::date - '2909-01-01'::date) AS a,
      ('2909-01-02'::date - '2909-01-02 00:00:00'::timestamp) AS b;
    \x
    SELECT * FROM tt;
    \d tt
    

    That will show you the values and types you are dealing with. Repeat for as many columns as you find useful.

    HTH

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to select an H1 element which is the second-child in its group
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

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.