Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

sql - Convert from date to epoch-Oracle

I need to convert a date from a TextBox from date to epoch time so that I can insert it into Oracle DB.

I managed to convert from epoch to date as below, but couldn't find a way to convert it the other way.

SelectCommand="SELECT ID,
            COMPANY,
            FIRST_NAME,
            LAST_NAME,
            ID_NUMBER,
            (SELECT TO_CHAR(TO_DATE('01-JAN-1970','DD/MM/YYYY')
            +(TRAINING_DATE/60/60/24), 'MM/DD/YYYY') FROM dual) AS TRAINING_DATE,
            (SELECT TO_CHAR(TO_DATE('01-JAN-1970','DD/MM/YYYY')
            +(TRAINING_VALABILITY/60/60/24),'MM/DD/YYYY') FROM dual) AS TRAINING_VALABILITY
    FROM CONTRACTORS
    ORDER BY COMPANY"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Subtracting DATE '1970-01-01' from the value will give the number of days (and fractional hours/minutes/seconds) difference and then you can multiply by 24*60*60:

(date_value - DATE '1970-01-01')*24*60*60

Update:

Typically, epoch time is measured from 1970-01-01T00:00:00 UTC. If your date is not in UTC then you will need to convert time zones.

For example, if your date has the time zone Europe/Berlin:

( CAST(
    FROM_TZ(
      CAST( date_value AS TIMESTAMP ),     -- Cast to timestamp
      'Europe/Berlin'                      -- Convert to expected Time Zone
    )
    AT TIME ZONE 'UTC'                     -- Convert Time Zone to UTC
    AS DATE                                -- Cast back to DATE data type
  )
  - DATE '1970-01-01'
)*24*60*60

db<>fiddle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...