This page is based on examples to be easier to follow. The Oracle Timestamp is more accurate than Oracle Date and it keeps a location stamp. They are the two main reasons why programmers are using the Oracle Timestamp. The location stamp is important because the same time in hours can mean different moments in different places on world. For example 1 pm in London means 8 am in New York, so using Oracle Date we do think about different moments. Timestamp in Oracle can solve the problem and we do have more exact time for transactions.
The first example is with Oracle CURRENT_TIMESTAMP and the difference between mostly used Oracle SYSTIMESTAMP is that the Oracle CURRENT_TIMESTAMP shows time location in words. The CURRENT_TIMESTAMP is used to show us – to people more readable way the Oracle timestamp value.
SELECT CURRENT_TIMESTAMP FROM DUAL;
The second Online Tech Support example is with Oracle SYSTIMESTAMP and as you the time location has changed to the GMT number +1 that is the same as Europe/London.
SELECT SYSTIMESTAMP FROM DUAL;
To take out some value from Oracle Timestamp you can use Oracle Extract function. You can extract for example SECOND, HOUR, MINUTE, DAY, MONTH or YEAR value using the same keywords. The Oracle Extract syntax is following.
EXTRACT(<extracting_value_name> FROM <timestamp_type>)
And on the example below we have taken out a day number as this moment it was April 11th 2013.
SELECT EXTRACT(DAY FROM SYSTIMESTAMP) AS Timestamp_Day FROM DUAL;
To turn an Oracle Timestamp a date you can use the Oracle Trunc function. The function not only will turn the Timestamp a date but also removes all stamp values. You can truncate date model names like minutes (“MI“), hours (“HH“), DAY, MONTH or YEAR.
Using Oracle Trunc without any additional setting will truncate the date from all time value. Take a look at the example below.
SELECT TRUNC(SYSTIMESTAMP) AS Truncated_Timestamp FROM DUAL;
The second example with Oracle Trunc we have set the truncation limit to minutes (“MI“). The Oracle Trunc function will truncate from Timestamp all values that are smaller than minutes and stamps will be included into the removing list.
SELECT TRUNC(SYSTIMESTAMP,'MI') AS Truncated_Timestamp FROM DUAL;
Another way to turn Oracle Timestamp to a date without truncating anything is to use Oracle Cast. You can cast different values to other types and the same you can to with Oracle Timestamp. The Oracle Cast syntax is:
CAST(<timestamp_value> AS <new_value_name>)
On the Oracle Cast example the Timestamp will be cast to Oracle Date. As you see on the following output all time values are remained in the date.
SELECT CAST(systimestamp AS DATE) AS THE_DATE FROM DUAL;
To know more deep about the Oracle Timestamp attributes you can take a look at function Oracle Dump. The function returns description of timestamp where you can find in present the data type code, length in bytes, and internal representation of expr. Those attributes can be useful when you need to check if your timestamp value has been converted correctly.
The example above demonstrating the output of Oracle Dump function with Oracle Timestamp.
SELECT DUMP(SYSTIMESTAMP) FROM DUAL;
It is important to know that using Oracle Timestamp in Oracle database table may cause problems for some applications. For example application Oracle Forms belongs to the same company as Oracle database but Oracle Forms would not compile when table has Timestamp type columns. In this case the Timestamp should be stored in VARCHAR2 and using Oracle Cast function can be returned to Oracle Timestamp type. Still if it’s possible keep the Oracle Timestamp values in their own type to avoid the extra work of converting types.
Home Oracle Select