Convert VCHAR to TIMESTAMP or DATE in Teradata - sql

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')

Related

correct to_char date syntax to have trailing zeroes after milliseconds

My current query in oracle sql for getting a timestamp format is TO_CHAR(c2.start_on,'DD-MM-YY HH:MI:SS.FF PM'), it outputs the timestamp like this 25-11-20 07:00:13.36 PM
However I want it to display the date in this way 25-11-20 07:00:13.360000000 PM
What should I add in the timestamp format for this to be possible ?
I have tried doing it like this HH:MI:SS.FM00000 as suggested here
but it gives me the error. ORA-01821: date format not recognized
what is the correct way to get the date in the desired format ?
If you want fractional seconds, you don't want a DATE, you want a TIMESTAMP. So here's a timestamp formatted with 6 digits of precision
select to_char(systimestamp, 'HH:MI:SS.FF6') from dual;
If you have a date, you could convert it to a TIMESTAMP (using CAST AS TIMESTAMP), but better to look at updating your data model to use the proper type for the source column as starters.

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

Oracle Database Table Column Timestamp

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.

Comparing Datetime in Oracle

Do I need to convert TIMESTAMP to DATE before doing a comparison? EX:
AND SORD.CREATED >= TO_DATE(FROM_TZ(CAST(TO_DATE('','DD-MON-YYYY HH24:MI:SS') AS TIMESTAMP), 'Asia/Kuala_Lumpur') AT TIME ZONE 'UTC','DD-MON-YYYY HH24:MI:SS')
AND SORD.CREATED < TO_DATE(FROM_TZ(CAST(TO_DATE('','DD-MON-YYYY HH24:MI:SS') AS TIMESTAMP), 'Asia/Kuala_Lumpur') AT TIME ZONE 'UTC','DD-MON-YYYY HH24:MI:SS')
Just looking for an opinion, since I'm still new and learning about databases.
It's hard to tell exactly what you database you are using. From the functions in the code you provided, I am guessing Oracle? Please confirm.
In general, I can tell you the following:
You aren't passing any values. The first parameter to TO_DATE is empty, and that would contain the string to convert to a date.
You need to know what the values in your database are. Is SORD.CREATED a DATE or TIMESTAMP datatype? Does it keep UTC values? If so, then yes, you should convert local times to UTC before querying. If possible, you should do this outside of the query itself.
If the values in your database are of type TIMESTAMP WITH TIME ZONE, then you can use a DATE or TIMESTAMP with the AT TIME ZONE function, like you showed in your question.
Don't cast to/from a string unnecessarily. If your source data is already in a DATE or TIMESTAMP datatype, there's no need to convert to a string. Once a date is a date, you should keep it a date. Introducing string formatting can sometimes introduce errors.

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')