Oracle Database Table Column Timestamp - sql

I am trying to create a table in my Oracle Database with the following structure:
CREATE TABLE orderlines(collectionTime TIMESTAMP NOT NULL);
An example of a timestamp that will be going in a row is:
20-01-2015 11:33:48-04:00
What would be the best datatype to use to store this timestamp?
TIMESTAMP, or DATE?
Also, what would the format be?
When I tried to import my CSV file containing all of the timestamps, SQL Developer Import Wizard didnt like the formatting. I tried
DD-MM-YYYY HH24:SS:MM-TZ

What would be the best datatype to use to store this timestamp? TIMESTAMP, or DATE?
The TIMESTAMP datatype is an extension to the DATE datatype. In addition to the datetime elements, the TIMESTAMP datatype holds fractions of a second. It comes in two forms, TIMESTAMP WITH TIME ZONE and TIMESTAMP WITH LOCAL TIME ZONE. So, if you want the precision till fraction of seconds along with the timezone elements, go for TIMESTAMP. Else, a DATE data type will give you datetime elements.
Also, what would the format be?
Format is only for DISPLAY. So use the format model which you would like to display. You need to use TO_CHAR and a proper format model. For example,
SQL> select to_char(sysdate, 'mm/dd/yyyy hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'MM
-------------------
01/21/2015 11:02:27
SQL> select to_char(sysdate, 'mm/dd/yyyy hh:mi:ss am') from dual;
TO_CHAR(SYSDATE,'MM/DD
----------------------
01/21/2015 11:03:04 am
SQL>
When I tried to import my CSV file containing all of the timestamps,
SQL Developer Import Wizard didnt like the formatting.
That is the issue with your locale-specific NLS_DATE_FORMAT.

Try 'DD-MM-YYYY HH24:MI:SS-TZH:TZM'. This is the mask that would be recognized by to_timestamp_tz. Notice that MM is for months and MI is for minutes. Also, you had hours:seconds:minutes in your mask, but that would result in a wrong value not an error.

Related

Converting string to timetsamp in Hive

I have a pipeline where im getting data from sqlserver and load it into Hive table.I have a timestamp column in the source which is like 'YYYY-MM-DD HH:MM:SS'
Sql table(datetime) ---> Hive stage table(string)---->Hive final table(timestamp)
The source table is in US/Pacific time zone. In the middle stage table, the format is like 'YYYY-MM-DD HH:MM:SS.0'.
How do i convert into a timestamp field for the final table? I want the final table column to look like 'YYYY-MM-DD HH:MM:SS'
I see from_unixtime being used, but when i try like below,it returns null.
FROM_UNIXTIME(UNIX_TIMESTAMP('date column','yyyy-mm-dd HH.mm.ss')) as ts
Im pretty new to using Hive and need some suggestion on what should i do here, Thanks.
If the timestamp string is in format 'yyyy-MM-dd HH:mm:ss.S' then you can cast it to timestamp type using timestamp() function.
timestamp(col)
Also you can insert string directly into timestamp column.
It works because 'yyyy-MM-dd HH:mm:ss.S' - is a default timestamp format.
You need conversion using FROM_UNIXTIME(UNIX_TIMESTAMP(col, format)) if the format is not 'yyyy-MM-dd HH:mm:ss.S'. This format you should convert to, not from. Specify correct FROM format, it is case-sensitive: MM is not the same as mm, delimiters do matter: dot is not the same as semicolon or space, etc.
See format manual here: SimpleDateFormat
Also see this post about timestamp with nanoseconds

How to convert date to datetime in Oracle?

i have a date in oracle with this format DD-MM-YYY and i want to convert it to datetime with this other format DD-MM-YYY HH24:MI how can i proceed?
I've tried this but nothing is working :
to_date(the_date,'DD-MM-YYY HH24:MI')
and also this:
to_date(to_char(date_debut_p),'DD-MM-YYY HH24:MI')
i have a date in oracle with this format DD-MM-YYY and i want to convert it to datetime with this other format DD-MM-YYY HH24:MI
No, you are confused. Oracle does not store dates in the format you see. It is internally stored in 7 bytes with each byte storing different components of the datetime value.
DATE data type always has both date and time elements up to a precision of seconds.
If you want to display, use TO_CHAR with proper FORMAT MODEL.
For example,
SQL> select to_char(sysdate, 'mm/dd/yyyy hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'MM
-------------------
11/25/2015 22:25:42
Oracle DATE datatype ALWAYS contains (stores) time.
If you want to see it, you can use function TO_CHAR.
If you want to add, for example, 1 hour, you can just use date_debut_p+1/24.
If you want to covert to timestamp, you can do the following:
Select to_timestamp(date_column, 'DD-MM-YYY') from table;
However, if you want in the required format, you can do the following:
Select to_char(to_timestamp(date_column, 'DD-MON-YY'), 'DD-MM-YYY HH24:MI')
from table;
Hope it helps..

Convert VCHAR to TIMESTAMP or DATE in Teradata

I uploaded a table using fastload and want to convert the event_time column to a timestamp or just get the dates since I don't need the time component.
The text format is 'MM/DD/YYYY HH:MM:SS AM/PM', for example, '05/24/2013 08:12:00 AM'
I have tried
CAST(event_time AS TIMESTAMP(0))
and
CAST(SUBSTRING (event_time,1,10) AS date)
but they don't work for me. The error I get is invalid timestamp/date.
As a bonus question,
I don't think there are any outliers in the data (i.e. every row is in format as described above), but if there were, how do I account for those errors? or will Teradata automatically pass on those rows?
The CASTs don't work because this is not the default timestamp/date format.
The easiest way utilizes the TO_DATE function which is supported since TD14:
TO_DATE(event_time, 'MM/DD/YYYY HH:MM:SS AM')
Before TD14 it's:
CAST(CAST(event_time AS TIMESTAMP FORMAT 'MM/DD/YYYYbHH:MI:SSbT') AS DATE)
Regarding outliers, they will result in an "invalid date/timestamp" error. When you cast them during FastLoad to a Timestamp those rows will fail to load and will be inserted into the ET-error table. But you can't use the newer CAST-syntax in FL, must be the old Teradata-style cast:
:event_time (TIMESTAMP, FORMAT 'MM/DD/YYYYbHH:MI:SSbT')

Convert string to date in Oracle SQL

I'm trying to convert string column to date in Oracle SQL.
Here is my an example of value from the data:
'03/12/14 11:00:00'
Here is my query:
select to_date(A,'DD/MM/YY HH24:MI:SS')
from MyTable
Here is an example of the output:
03-DEC-14
Why my query return only the date but not the hour?
Assuming you are using SQL*Plus (or SQL Developer) the default NLS_DATE_FORMAT is applied when a DATE value is displayed. To verify your current format you can run:
select value
from nls_session_parameters
where parameter = 'NLS_DATE_FORMAT';
To adjust this, run:
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
Then you should see the time as well.
You are trying to DISPLAY the datetime value. Use TO_CHAR along with proper FORMAT MODEL.
For example,
select to_char(sysdate, 'mm/dd/yyyy hh24:mi:ss') from dual;
A DATE consists of both date and time portions. You just need to make sure you use appropriate format model to display it. Else, you end up with a format which depends on your locale-specific NLS settings.
You can use timestamp
select to_timestamp(A, 'DD/MM/YY HH24:MI:SS') from MyTable
If you want a query that returns the time portion of the date without having to alter the nls_date_format you can convert the date you got from the to_date function to a text representation (the default text representation is dd-Mon-yy as you found out) like this:
select to_char(
to_date(A,'DD/MM/YY HH24:MI:SS'),
''DD/MM/YY HH24:MI:SS')
from MyTable;
Ideally, column A of MyTable should have been defined as a date column instead of a varchar2 column. There are several reasons to do that; data integrity, and better SQL optimization.

Is it possible to create a timestamp column which includes the meridian indicator in PostgreSQL?

I'm trying to create a column of type TIMESTAMP such that would store timestamps in the following format (i.e. including the "AM"/"PM" indicator):
20-04-2013 06:56:37 AM
I suppose I could use the to_char(..) function and store the timestamps as TEXT values, however isn't it possible to achieve the same effect with a vanilla TIMESTAMP column?
If you want to format a timestamp, use to_char when you SELECT it, or format the date in the client.
Timestamps don't have a format. They're stored as the number of fractional seconds since 1970-01-01 UTC inside the database and they're formatted into ISO timestamps for input and output. Inside the database they're just a number.
There is no way to override the default timestamp output format, since that would lead to a setting that could confuse applications that expect a single consistent format.
If the app wants a different date output format it has to ask for it with an appropriate to_char call.
Is this roughly what you are looking for?
select to_char(now(), 'DD-MM-YYYY HH12:MI:SS AM')