Conversion of Standard Datetime format to Unix (Epoch) time

Hi,
I am having trouble getting efficient record selection for time ranges on DB2 tables that use the UNIX epoch time stamp.
E.g., for a 1 minute time range we would currently use SQL such as:
SELECT * FROM DATABASE.TABLE WHERE  ((TIMESTAMP('1970-01-01-00.00.00.000000') + (("EPOCH_TIME" + 3600000)/1000) SECONDS)>={ts '2008-09-04 14:29:00'} AND (TIMESTAMP('1970-01-01-00.00.00.000000') + (("EPOCH_TIME" + 3600000)/1000) SECONDS)<{ts '2008-09-04 14:30:01'})
This is the equivalent of:
select * from DATABASE.TABLE where EPOCH_TIME between 1220534940000 and 1220535000000.
Now clearly the problem is the top SQL doing a calculation for every record (up to 5,000,000 records) to select the data. It takes approximately 5 minutes to return the data. The bottom SQL takes LESS THAN A SECOND.
Please, please, please could someone help us with this? We need to be able to enter a datetime variable, and have the SQL sent to the DB2 table in the format of the lower SQL above.
We are using a BO XI Enterprise server to do this.

OK Everyone.
This is how to do it:
Use the following (for Crystal Reports XI)
DateDiff (intervalType, startDateTime, endDateTime)
as:
Datediff ("s",(datetime(date(1970,01,01)),time(00,00,00)),currentdatetime)
Looks a bit too simple, but does work. Look out for differences between the results due to daylight saving.

Similar Messages

  • SSRS 2008 DateTime picker to UNIX/EPOCH field

    Im trying to write a report that presents data based on a date range(selected by the person running the report) using SQL Server reporting.
    I build the start date and the end date as parameters and as a selectable date/Time data type and create a dataset with the following simple query:
    SELECT callclass AS CallType, COUNT(callclass) AS Total
    FROM opencall
    WHERE (companyname = @name) AND (status <> 17) AND (logdate >= @StartDate) AND (logdate <= @EndDate)
    GROUP BY callclass
    The problem I have is that the logdate field is stored as an EPOCH/UNIX value and therefore the report does not run. How can I get the date/time picker to convert the selected value into an epoch string and return the correct data for the date range selected?
    Help muchly appreciated.

    Hi  Josh/Sylove1
    I'm trying to do exactly the same thing (and I think using the same application, judging by the field names) but keep getting a syntax error notification.
    Is the syntax below correct?
    SELECT callclass AS CallType, COUNT(callclass) AS Total
    FROM opencall
    WHERE (companyname = @name) AND (status <> 17) AND (logdate BETWEEN DATEDIFF(second, '1970-01-01 00:00:00', @StartDate) AND DATEDIFF(second, '1970-01-01 00:00:00', @EndDate))
    GROUP BY callclass
    Whats the datatype of @StartDate and @EndDate?
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • How to return start and end time in Unix epoch format

    Hi: I have a integer field (utime) in a table that stores the UNIX epoch time for a record. Every sunday around 2:00AM, I am running a query which should return records from last week. I want my condition to have the following in it's where
    WHERE utime between
    (convert sysdate-7 12:00AM to Unix time)
    AND
    (convert yesterday 11:59PM to Unix time)
    Can someone help me create this convert?
    Thanks
    Ray

    Yes, you seem to have worked out the logic of the expression correctly. Though, in your earlier post you had said that you want records within the range:
    WHERE utime between
    (convert sysdate-7 12:00AM to Unix time)
    AND
    (convert yesterday 11:59PM to Unix time)But now you are adding 14400 (4hrs) and deducting 72000 (20hrs) in your expression
    SELECT * from TABLE
    WHERE utime between
    ((TRUNC(SYSDATE,'DD')-TO_DATE('01/01/1970','MM/DD/YYYY'))*(24*60*60))+14400 AND
    ((TRUNC(SYSDATE-6,'DD')-TO_DATE('01/01/1970','MM/DD/YYYY'))*(24*60*60))-72000;Which makes your new condition to be:
    WHERE utime between
    (convert sysdate 04:00AM to Unix time)
    AND
    (convert sysdate-7 04:00AM to Unix time)
    Are you sure you need records from 4am-to-4am and not midnight-to-midnight? And yes, with BETWEEN condition you need to use expression '<low-value> AND <high-value>' and not '<high-value> AND <low-value>'!
    Besides this,
    1. You might want to use bind variables rather than hard-coded expressions in your statement. Infact your expression uses SYSDATE, so Oracle would need to get current SYSDATE for each row to evaluate the expression. Which is going to have its impact on performance. As your start-time and end-time values are not dependent on table data maybe you could have a code like this (assuming it is PLSQL code):
    start_time constant number := (convert sysdate-7 12:00AM to Unix time);
    end_time constant number := (convert yesterday 11:59PM to Unix time);
    <DML or Cursor with condition>
    WHERE utime between start_time and end_time;
    2. You said that your table already has values in unix-epoch-time in column 'utime'. How are these values inserted in the first place? Is there a function in your application that converts any given time to unix-epoch-time? Is yes, then use that in your code.
    3. If such a function does not exists, than rather than using hard-coded expressions it might be a good idea to create new functions for converting to and from unix-epoch-time and to get current unix-epoch-time from server.
    Using a function in the above case:
    start_time constant number := f_convert_to_unix_time( trunc( sysdate-7 ));
    end_time constant number := f_convert_to_unix_time( trunc( sysdate )) - 1;
    <DML or Cursor with condition>
    WHERE utime between start_time and end_time;
    And lastly, some of us might not know about Unix time, so here is a useful link http://en.wikipedia.org/wiki/Unix_time

  • How to convert Epoch time to Datetime (YYYYMMDDHH24MMSS)

    Hi,
    In our Oracle db, Datetime is stored as Epoch time (seconds since january 1st 1970). To get a readable date, I have converted those fields as followed:
    TO_CHAR(TO_DATE('19700101000000', 'YYYYMMDDHH24MISS')+((DATE_FIELDS-18000) /(60*60*24)),'YYYYMMDDHH24MISS')
    Result ex: 20100830135422
    The problem is that the resulting fields are in VARCHAR2 instead of DATE so it causes issues in SAP BW reporting development. BW developpers can manage something to make it eventually work but I would like to correct this at the source if it is possible.
    I have already tried to add another "TO_DATE" function at the beginning of the previous expression but I get a date in "DD-MMM-YY" format as a result even if I have forced another date format.
    Any idea on how I could solve this?
    Thanks a lot
    Pascale

    Ok, there is clearly a disconnect here.
    Oracle stores dates in a DATE datatype. This means that, internally, it will store enough information for a 4 digit year, a month, date, hour in 24 hour time, minute, and second. All that information is stored in the DATE datatype inside the database, but, it's stored in an internal format, that has nothing to do with what it will look like when you print it. When you want to retrieve it, Oracle must have a date format string, so that it knows how to display that information. If you do not provide a date format string via the TO_CHAR() function, Oracle has a parameter called NLS_DATE_FORMAT that provides the default format to use. If you have not set the NLS_DATE_FORMAT, it defaults to DD-MON-YY.
    So, having said all that, what are you actually trying to do?
    You started with this expression:
    TO_CHAR(TO_DATE('19700101000000', 'YYYYMMDDHH24MISS')+((DATE_FIELDS-18000) /(60*60*24)),'YYYYMMDDHH24MISS')How is that expression used? In a SELECT statement? Then you probably want it the way it's written above. In an INSERT statement? If so, then if the datatype of the column you're inserting into is DATE, then you probably need to eliminate that outer TO_CHAR conversion, so that the expression returns a date that can be inserted into DATE column.
    Tell us more about what you want to do, and perhaps we can offer further help.
    -Mark

  • Time from UNIX epoch

    I have the number of micro seconds from the UNIX epoch time 00:00:00 UTC 1
    Jan 1970. (1092727073895638 usec)
    I want to know the time in CST.
    I have been trying to work with the Calendar class and GregorianCalendar.
    But I am not quite getting it. Could someone give me an idea of what I need
    to do.
    Thanks
    Ox

    You need to set a TimeZone in your Calendar or GregorianCalendar. Then format using SimpleDateFormat. Post the code that you tried, and explain what isn't coming out correctly.

  • Convert date/time to Unix epoch

    I have thousands of date time stamps I would like to convert to Unix epoch ten-digit strings. That is, take some thing like:
    Dec 9, 2005 10:24 AM
    and convert to
    1234567890 (only in the correct unix epoch time)
    What formula can I use? I've found that this "=DATE(1970,1,1)+P2/86400" can convert a 10 string epoch time down to just a date correctly, but without time. But I need to go in the opposite direction, with the time included.
    Using Numbers 09 ver 2.0.5 (368)

    CP,
    An up-to-date Unix Time would be provided by:
    =DUR2SECONDS((NOW()-"1/1/1970"))
    A somewhat more robust form would be:
    =DUR2SECONDS(NOW()-DATE(1970,1,1))
    If the time to be converted is in Cell P2, the expression would be:
    =DUR2SECONDS(P2-DATE(1970,1,1))
    Jerry

  • Converting EPOCH time stamp to time stamp in Oracle

    Hi,
    I need a help regarding how to convert the epoch Time stamp, to oracle date and time format.
    for example,
    for epoch time stamp:1204104116656
    the time stamp should be :2008-02-13 12:43:00.351
    Thanks in Advance
    Basil Abraham

    Are you sure you've got the right timestamp? Assuming this is a standard unix epoch time stamp with the epoch date starting at 1970-01-01 UTC the interval between 1970-01-01 UTC and 2008-02-13 is 13922 days, but your epoch timestamp divided by the number of miliseconds in a day 1204104116656/(1000*60*60*24) comes out to 13936.390... days a difference of 14 days 9 hours 21 minutes 56 seconds and 656 miliseconds.
    Anyway to convert you need to convert your epoch date into an interval and add it to the starting epoch date (as a timestamp):
    select to_timestamp_tz('1970-01-01 utc', 'yyyy-mm-dd tzr')+
           numtodsinterval(1204104116656/1000,'second') dstamp
    from dual;Message was edited by:
    Sentinel

  • Fetch Epoch time

    Hi: I want to build a small java code that returns the unix epoch time for 12:00AM yesterday and 11:59PM yesterday. Anybody have code which does the above that you can share ?
    TIA
    Ravi

    This should do what you're looking for:
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    public class Main {
      public Main() {
      public static void main(String[] args) {
        final long MS_PER_DAY = 24 * 60 * 60 * 1000;
        // Get the current time
        Date now = new Date();
        // Get a time from yesterday
        Date yesterday = new Date(now.getTime() - MS_PER_DAY);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(yesterday);
        // Get the time yesterday morning at 12:00AM
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date yesterdayStart = calendar.getTime();
        // Get the time yesterday night at 11:59:59PM
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date yesterdayEnd = calendar.getTime();
        System.out.println(
          "Yesterday start: " + yesterdayStart +
          "\t Since Epoch:" + yesterdayStart.getTime());
        System.out.println(
          "Yesterday end:   " + yesterdayEnd +
          "\t Since Epoch:" + yesterdayEnd.getTime());
    }If you want to be slightly more efficient, you could take the "yesterdayStart.getTime()" and add (MS_PER_DAY - 1000) to get the yesterdayEndTime (or, even better, use -1 instead of -1000 if you want to get all the way to 11:59:59:999).

  • Invalid datetime format (null) Elements 4 and 7

    I have about 20,000 photos in Elements 4 organizer (windows XP SP2). I am trying to get the catalog in shape to be converted to Elements 7 format--it will not convert at present, telling me the file has errors in which need to be fixed before conversion. In Elements 4 I've run "Recovery" which completes successfully (several times). I've tried "reconnect all files" which reports all files are connected.
    But I think I have found the problem. There is one file/photo with the same name (it is the same photo) both inside and outside of a version set. I do not know how or when this happened.
    Whenever I try to do any of the following I get the same error
    1. Delete either version of the file (i.e. inside or outside of the version set)
    2. Stack the outside file/photo with the 2 in the version set
    3. Convert the version set to individual photos
    The Error is
    The ODBC data source reported the following error
    "[Microsoft][ODBC Microsoft Access Driver] Invalid datetime format (null)"
    By the way, the metadata for this photo has a proper date. I have tried various tricks like deleted the file outside of Elements, adding it back with a reconnect, etc. No resolution.
    I am completely stumped. By the way, other than this, the Organizer seems to work OK (a bit slow sometimes, but that is relative. I was hoping 7 would be faster).
    Please advise.

    John
    Thanks for the tips. The following are the last several lines from the conversion log:
    created media a:107486 for file "J:/Album of Photos/2005 Photos/2005_04_22 Barcelona/133_3382_edited-1.jpg"
    created media a:107502 for file "J:/Album of Photos/2005 Photos/2005_04_22 Barcelona/133_338920050423.JPG"
    created media a:107518 for file "J:/Album of Photos/2005 Photos/2005_04_22 Barcelona/133_3387.JPG"
    created media a:107534 for file "J:/Album of Photos/2005 Photos/2005_04_22 Barcelona/133_3388.JPG"
    created media a:107550 for file "J:/Album of Photos/2005 Photos/2005_04_23 Barcelona/133_3386.JPG"
    The file 133_3386 is the problem file I noted. However the first instance in the log above has a 2005_04_22 date, which does not exist in the actual file.
    I don't have PSE5 and I don't know ACCESS. Does any of this make sense?
    Ralph

  • How can i create a photobook with a layout that respect standard photo format?

    I used iphoto to create a photobook but there is serious problem, i can use only fixed layout and this layouts are problems.
    I can't put my shoot in standard photographic format, so i need to zoom and move my photo to fit well in your fixed layout.
    I can't change the composition of all my shoots to fit in wrong layout.
    There is a way to disable layout and put manually the photo in original format on pages?
    Thank you
    Mario Aprea

    iPhoto books can only be created using the fixed templetes supplied - you can not modify them
    I have no idea what a "standard Photographic Format is" - neither I nor Google knows that that is
    So I do not understand what you want to do
    One possible suggestion is that in a book you can right click on a photo (in most temes) and use the fit to frame option to show an entire photo that is in the wrong dimensions
    LN

  • How To Get Only Month or Only Year from datetime format of yyyy-mm-dd

    Hi SQL gurus,
    I have field has datetime format of yyyy-mm-dd (ie. 2014-11-28).  Could anyone please educate me on how to extract only month (ie. November but not 11) and only year (ie, 2014) from 2014-11-28.  I writing two report have title of Number of
    sick leaves on November  and Number of sick leaves in 2014.  I am planning to extact 11 from 2014-11-28 and display as November on report title and the same goes for 2014.  Unless you have better non complicated way.   Thank you
    very much in advance.  DingDong!!

    There are multiple ways
    Month name
    SELECT DATENAME(mm,@DateParam)
    SELECT FORMAT(@DateParam,'MMMM')
    Year
    SELECT FORMAT(@DateParam,'yyyy')
    SELECT DATEPART(yy,@DateParam)
    Please Mark This As Answer if it solved your issue
    Please Mark This As Helpful if it helps to solve your issue
    Visakh
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • How to convert epoch time to datetime in sql*loader Oracle

    Hello,
    I wan't to question how to convert epoch time to datetime in sql*loader Oracle. I try this script for convert epoch time to datetime in sql*loader, but error:
    Record 1: Rejected - Error on table TEMP_TEST_LANGY, column DATADATA.
    ORA-01722: invalid number
    Record 2: Rejected - Error on table TEMP_TEST_LANGY, column DATADATA.
    ORA-01722: invalid number
    Record 3: Rejected - Error on table TEMP_TEST_LANGY, column DATADATA.
    ORA-01722: invalid number
    This is my loader:
    LOAD DATA INFILE 'C:\Documents and Settings\Administrator\My Documents\XL_EXTRACT_211\load.csv'
    into table TEMP_TEST_LANGY append
    FIELDS TERMINATED BY ','
    TRAILING NULLCOLS
    DATADATA CHAR "TO_DATE('01-JAN-1970','DD-MON-YYYY')+:datadata/86400"
    This is my csv file:
    79314313.7066667
    79314336.2933333
    79314214.3466667
    This is my table:
    CREATE TABLE TEMP_TEST_LANGY
    DATADATA DATE
    Thanks
    Edited by: xoops on Sep 21, 2011 8:56 AM
    Edited by: xoops on Sep 21, 2011 8:58 AM

    thanks for your answer, but I asked to use sql loader instead of the external table, which so my question is why can not the epochtime converted to datetime, if there is no way to convert a datetime epochtime using sql loader, so I'm required to use the external table. thank you.
    This is my error log:
    Column Name Position Len Term Encl Datatype
    DATADATA FIRST * , CHARACTER
    SQL string for column : "TO_DATE('1-Jan-1970 00:00:00','dd-MM-YYYY hh24:mi:ss') + (:DATADATA/60/60/24)"
    Record 1: Rejected - Error on table TEMP_TEST_LANGY, column DATADATA.
    ORA-01722: invalid number
    Record 2: Rejected - Error on table TEMP_TEST_LANGY, column DATADATA.
    ORA-01722: invalid number
    Record 3: Rejected - Error on table TEMP_TEST_LANGY, column DATADATA.
    ORA-01722: invalid number
    Edited by: xoops on Sep 21, 2011 12:33 PM

  • How to upload pdf file format in unix file directory?

    how to upload pdf file format in unix file directory?

    Hi Karthikeyan,
    If you have PDF internal table with you, You can follow the below method to upload into Unix directory.
        DATA:file_path TYPE char100 VALUE '/usr/sap/tmp/rep_out.PDF'.
        OPEN DATASET file_path FOR OUTPUT IN BINARY MODE.
        IF sy-subrc EQ 0.
          LOOP AT it_pdf.
            TRANSFER it_pdf TO file_path.
          ENDLOOP.
        ENDIF.
        CLOSE DATASET file_path.
    Thanks
    Venkat.O

  • Migration Issues from Informix to Oracle (datetime format)

    Hello, i am performing a Migration from Informix to Oracle, drive by Migration Workbench, i have an error migrating data from datetime and date fields. I suppose its related by datetime and date format, cause the error tells "Incorrect day or month" when i am migrating data from informix. Targets Oracle database date format is DD/MM/YYYY and source`s Informix database date format like MM/DD/YYYY. Is there any way, in OMW, to specify the mask of datetime or date format to convert date strings from informix to oracle format? Or have i got migrate offline and modify control file to do that with sql*loader?
    How can i do that?
    Example, especify to_date('....', 'MM/DD/YYYY H24:MI:SS') format for migrating informix datetime fields to Oracle.

    The problem i have is, several tables witch contains date and datetime fields, at the same table in informix, by migrating drive by omwb to oracle, the date format i receive from informix at reading is DD/MM/YYYY and datetime format receiving is HH24:MI:SS. Migrating date and datetime types from informix to date, both types to date in oracle, i get an error while matching DD = HH24, MM = MI and YYYY = SS where nls_date_format is DD/MM/YYYY and, the same error matching HH24 = DD, MI = MM and SS = YYYY when nls_date_format is HH24:MI:SS.
    If i set nls_date_format as "DD/MM/YYYY HH24:MI:SS" i get the same error, cause reading data from informix results in data format "DD/MM/YYYY" for date fields and "HH24:MI:SS" for datetime fields.
    I have more than 200 tables in informix with this problem to migrate to oracle, i can solve by migrating offline, modifying datacontrol file to casting to_date format, but over 200 tables, 200 datacontrol files... is there any way for solving that by online migration in omwb?

  • Xsd datetime format

    Hi, I'm using a javascript popup calendar on a web page which can return a timestamp in either of 3 formats (i can choose whichever):
    dd-mm-yyyy hh:mm:ss
    mm/dd/yyyy hh:mm:ss
    yyyy-mm-dd hh:mm:ss
    I want to convert this form value into the xsd datetime format yyyy-MM-ddThh:mm:ss.
    How can I do this?
    I have been using the following code, but this just deals with today's date. I need to alter it so that it can deal with the value from the popup calendar:
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
    StringBuffer sb = new StringBuffer( sdf.format(calendar.getTime()) );

    There is no piece ion your code which would set any value.
    Check the API for Calendar, it has many useful methods.

Maybe you are looking for

  • SQL error 1562  while import client copy

    hi basis guys, 1st of all i export the client thro scc8 of (my prod server) after that i copy the request in dev client.( i extend the table space also.) but when i m import the client thro stms i m getting the following error Main import            

  • Bank Account Reconciliation

    FI Experts, client do not have a clearing accoutn set-up for outgoign checks, therefore I am reconciling account manually first, before I introduce them to teh clearing accoutn. I  updated the encashment field for all outstanding checks, now I need t

  • What controls the icon size on the toolbars in Crystal 2008?

    Is there any way to change the size of the icons on the toolbars in Crystal 2008 other than changing the screen resolution? I would like the icons to be larger without changing everything else that I work with.

  • How to process sales without posting to FI

    Hi guyz, Please i have this issue from my client, they are telecoms coy, when they bring in phones for sale, they want to give some to their employees to test for some days to confirm that the quality is ok. After the testing, this phone are to be re

  • Invalid Database Settings

    Hi, I am facing error: Error opening database connection to:jdbc:plumtree:sqlserver://pdcdt158.patni.com:1433;DatabaseName=alidb, exception:[plumtree][SQL Server JDBC Driver] Error establishing socket to host and port: pdcdt158.patni.com:1433,Reason: