How do i convert date time format in informatica - sql

I want to map a datetime mm/dd/yyyy hh24:mi:ss attribute in flatfile to Teradata table date yyyy-mm-dd attribute using informatica.
When I added to_date(date_field, 'yyyy-mm-dd') I'm encountering oracle fatal error. When I tried with to_date(to_char(date_field, 'yyyy-mm-dd')) it is giving invalid string input to to_date().
Can anyone help?

In Informatica, the format that you specify under to_date() function should be same as your source data format and not your target format.
So in your case, to_date function should be like this:
to_date (date_field, 'mm/dd/yyyy hh24:mi:ss')
This is because your flat file date has a format of mm/dd/yyyy hh24:mi:ss (Ensure that all the records in this column in your flat file date are really in this format - else you will encounter error)
Do not worry about target date format as long as the target column is a date datatype. By nature, date datatype does not have a format, it's only the display that needs format.

Related

SQL, Converting date timestamp variable into strings 'yyyymmdd' and 'yyyymm'

I would like to replace a date timestamp variable 'date' with the format yyyy-mm-dd hh:mm:ss e.g, 2021-12-28 00:00:00
with two other string variables; one named date with the format: 'yyyymmdd' e.g, 20211228, and one named month with the format: 'yyyymm' e.g, 202112.
Can you give me some suggestions using a SELECT statement?
Thanks
Just use TO_CHAR (or whatever the equivalent is for your DBMS) and the appropriate format string

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 a VARCHAR to a date in ORACLE SQL?

I have that field in my table:
2020-01-16T10:55:16..296518000
How to convert this Varchar into a date in format 'YYYY-MM-DD' ?
I tried:
select TRUNC(to_date(DATE_UPDATED ,'YYYY-MM-DD hh24:mi:ss')) from JOB_SCHEDULE_TBL
but I'm getting an error:
ORA-01830: date format picture ends before converting entire input string
Just use substr():
select to_date(SUBSTR(DATE_UPDATED, 1, 10) ,'YYYY-MM-DD')
The trunc() is unnecessary.
You are confusing the format of DATE with what you are seeing on the screen. A DATE data type has no "format". It's Oracle's internal, binary format. Even when you SELECT TO_DATE ..., the result is going to get displayed by the client, and to do so the client will have to peform (under the covers) a TO_CHAR on the resulting DATE. And that implied to_char will use the current system/session/ settings for TNS_DATE_FORMAT.

Changing date format from day of year format (DDDYYYY) to Date Format (MM/DD/YYYY)

Trying to convert data thats in DDDYYYY (i.e. 1112014) to a standard date format (Feb-02-2014). How would i go about doing this? I know how to do the inverse operation but I was unable to find any examples online of how to convert from DDDYYYY format to MM/DD/YYYY format.
Below is my attempt:
select to_date(to_char(1112018, 'DDDYYYY'), 'MM/DD/YYYY') from dual;

PL SQL - Convert timestamp to datetime/date

select
to_timestamp(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS.FF') as SCHEDULED_TIME,
TRUNC(to_date(to_timestamp(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS.FF'),'YYYY-MM-DD HH24:MI:SS'))
from S_TIDAL_STATUS
The error was:
ORA-01830: date format picture ends before converting entire input string
01830. 00000 - "date format picture ends before converting entire input string"
The goal is to return something like
2017-07-91 23:14:00
(without the content after the dot).
Here's what the SCHEDULED_TIME (timestamp) looked like:
The problem in your attempt is the function TO_DATE() applied to a timestamp. TO_DATE() takes a VARCHAR2 (string) input, not a timestamp. So Oracle converts the timestamp to a string first, implicitly, using your NLS_TIMESTAMP_FORMAT parameter, and then attempts to convert this string to a date. Depending on your NLS_TIMESTAMP_FORMAT, you may get different errors.
The way to convert a timestamp to a date (datetime) - while truncating off the fractions of a second - is with the CAST function. Example:
select systimestamp,
cast (systimestamp as date) as ts_cast_to_date
from dual
;
Alternatively, if all your strings are EXACTLY in that format, you can truncate the strings first and apply TO_DATE directly:
to_date(substr(scheduled_time, 1, 19), 'yyyy-mm-dd hh24:mi:ss')
This should do the trick:
select
to_char(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS.FF') as time_to_csecs,
to_char(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS') as time_to_secs,
TRUNC(to_date(to_char(SCHEDULED_TIME,'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')) as time_to_day
from S_TIDAL_STATUS
Please review the docs to see the difference between to_timestamp and to_char.