I have a date string listed as below
2015-03-14 11:00:00 AM
I am trying to convert this into a timestamp
I have tried
cast(from_unixtime(unix_timestamp(mydate,'yyyy-MM-dd HH:mm:ss aa'), 'yyyy-MM-dd HH:mm:ss aa') as timestamp) as new_date,
and
cast(from_unixtime(unix_timestamp(mydate,'yyyy-MM-dd hh:mm:ss aa'), 'yyyy-MM-dd HH:mm:ss aa') as timestamp) as new_date,
This will always return null
i also tried
unix_timestamp(mydate 'yyyy-MM-dd hh:mm:ss aa') as new_date
this returns a large int number.
How can i convert this string date into a timestamp?
This should do it
from_unixtime(unix_timestamp(mydate,'yyyy-MM-dd HH:mm:ss aa'))
Related
I have a impala table where date column values are stored in dd-mm-yy HH:mm:ss string format, e.g.
30-11-20 12:34:45
I want to convert them into yyyy-mm-dd HH:mm:ss.SSS, e.g.
2020-11-30 12:34:45.000
If anyone could suggest a way to achieve this!!
first use to_timestamp to convert to timestamp. Then use from_timestamp to convert to string.
select from_timestamp(
to_timestamp('30-11-20 12:34:45','dd-MM-yy HH:mm:ss')
,'yyyy-MM-dd HH:mm:ss.SSS') as str_timestamp
I want to convert dd/mm/yyyy hh:mm:ss string to date format.
select to_timestamp('19/05/2010 20:03:55', 'dd/mm/yyyy hh:mm:ss');
this is not converting the string to timeformat and always returns me NULL.
Please guide where I am going wrong.
You need to use correct format.
select to_timestamp('19/05/2010 20:03:55', 'dd/MM/yyyy HH:mm:ss') as ts;
The pattern string supports the following subset of Java SimpleDateFormat.
Pattern Description
y Year
M Month
d Day
H Hour
m Minute
s Second
S Fractional second
A date string including all fields could be 'yyyy-MM-dd HH:mm:ss.SSSSSS', 'dd/MM/yyyy HH:mm:ss.SSSSSS', 'MMM dd, yyyy HH.mm.ss (SSSSSS)' or other combinations of placeholders and separator characters.
I have a problem converting varchar to DateTime/timestamp.
Here is the case
ID EVENT_TIME(Varchar)
1 2020-04-12T09:25:53+0800
2 2020-04-12T09:25:53+0700
3 2020-04-12T09:25:53+0900
return I want, all timestamp convert to +0700
ID EVENT_TIME(Datetime)
1 2020-04-12 10:25:53
2 2020-04-12 09:25:53
3 2020-04-12 11:25:53
is this possible? and how can I do it using oracle?
thanks
Please use below query to convert varchar to timestamp using to_timestamp_tz and again convert it to the required time format using to_char
select ID, to_char(to_timestamp_tz(EVENT_TIME, 'YYYY-MM-DD"T"HH24:MI:SS.FF TZH:TZM'), 'YYYY-MM-DD HH24:MI:SS') as event_date from table_name;
Example:
select to_char(to_timestamp_tz('2020-04-12T09:25:53+0800', 'YYYY-MM-DD"T"HH24:MI:SS.FF TZH:TZM'), 'YYYY-MM-DD HH24:MI:SS') as event_date from dual;
As #Jim said, you can use to_timestamp_tz() with a character literal to convert the string to a timestamp value:
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
The to normalise to the +07:00 offset you can use at time zone:
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00'
If you don't want to keep the time zone part you can cast to a plain timestamp:
cast(
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00'
as timestamp)
Or you can cast (... as date) as you don't have fractional seconds.
Or you can convert that back to a string for display:
to_char(
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00',
'SYYYY-MM-DD HH24:MI:SS')
db<>fiddle
I have a START_DATE column in my table with values in the format YYYY-MM-DD HH:MM:SS AM/PM GMT. I'm trying to insert those values into another table but i'm getting a date format not recognized error. How do I fix this?
Sample SQL statement
INSERT INTO TABLE2 (
DATE_WITH_TIMEZONE,
DATE_WITHOUT_TIMEZONE
)
SELECT
TO_TIMESTAMP(START_DATE, 'YYYY-MM-DD HH:MI:SS AM TZD'),
TO_DATE(TO_TIMESTAMP(START_DATE, 'YYYY-MM-DD HH:MI:SS AM TZD'), 'YYYY-MM-DD HH:MI:SS AM')
FROM TABLE_DATE
Sample Data
2016-01-21 09:31:49 AM GMT
2016-02-22 06:37:32 PM GMT
2016-02-23 07:10:52 PM GMT
2016-03-15 08:54:40 PM GMT
2016-03-16 12:10:52 AM GMT
If it helps, these are the datatypes of the two columns in TABLE2
DATE_WITH_TIMEZONE TIMESTAMP WITH TIME ZONE
DATE_WITHOUT_TIMEZONE DATE
I have a START_DATE column in my table with values in the format YYYY-MM-DD HH:MM:SS AM/PM GMT
Assuming that your START_DATE column is of the DATE data type then your statement is incorrect; a DATE column has no format and it is stored internally as 7-bytes. It is only when it is passed to the user interface you are using to access the database that that UI will format the date (and not the database). SQL/Plus and SQL developer will format the date using the NLS_DATE_FORMAT session parameter (which is set per user session and can be changed) so relying on this format can lead to "interesting" bugs where the code you are using does not change but will start and stop working for different users depending on their session settings.
You can just do:
INSERT INTO TABLE2 (
DATE_WITH_TIMEZONE,
DATE_WITHOUT_TIMEZONE
)
SELECT FROM_TZ( CAST( START_DATE AS TIMESTAMP ), 'GMT' ),
START_DATE
FROM TABLE_DATE;
If your START_DATE column is of a string datatype (why would you do this?) then you will need to convert it to the appropriate type:
INSERT INTO TABLE2 (
DATE_WITH_TIMEZONE,
DATE_WITHOUT_TIMEZONE
)
SELECT TO_TIMESTAMP_TZ( START_DATE, 'YYYY-MM-DD HH12:MI:SS AM TZR' ),
CAST( TO_TIMESTAMP_TZ( START_DATE, 'YYYY-MM-DD HH12:MI:SS AM TZR' ) AS DATE )
FROM TABLE_DATE;
Oracle DB
START_DATE |END_DATE
--------------------|-------------------
2016-02-01 00:00:00 |2016-02-29 23:55:00
2016-02-01 00:00:00 |2016-02-29 23:55:00
2016-02-01 00:00:00 |2016-02-29 23:55:00
2016-02-01 00:00:00 |2016-02-29 23:55:00
2016-02-01 00:00:00 |2016-02-29 23:55:00
Query
`select * from VM_REPORT_TEMP_US where to_date(START_DATE,'YYYY-MM-DD HH24:MI:SS') >= '2016-02-01 00:00:00' AND to_date(END_DATE,'YYYY-MM-DD HH24:MI:SS') <= '2016-`02-24 24:59:00'`
Im trying to run this query but im getting some error. Can some one know where im going in this query ?
Im getting the below error
SQL Error [1861] [22008]: ORA-01861: literal does not match format string
java.sql.SQLDataException: ORA-01861: literal does not match format string
Do NOT use to_date() with a DATE column. That first converts the date value into a varchar value just to convert that back to a date which it was to begin with.
to_date() expects a varchar value, so Oracle first converts the DATE value into a varchar value using the current NLS settings. Then it tries to convert that varchar back into a date, using the format mask you supplied which most probably doesn't match the default NLS format you have and therefor you get an error.
You should also use proper date values in the condition rather then strings that are (again) implicitly converted to a DATE based on the current NLS settings:
select *
from VM_REPORT_TEMP_US
where START_DATE >= timestamp '2016-02-01 00:00:00'
AND END_DATE <= timestamp '2016-02-24 23:59:00'
Note that the hour 24 is invalid in an ISO timestamp literal.
If you want to provide the date/timestamp value in a format other then the ISO format, you need to use to_date() for those:
select *
from VM_REPORT_TEMP_US
where START_DATE >= to_date('01.02.2016 00:00:00', 'dd.mm.yyyy hh24:mi:ss')
AND END_DATE <= to_date('24.02.2016 23:59:59', 'dd.mm.yyyy hh24:mi:ss')
start_date and end_date are dates already, so you don't have to convert them to date. Convert the strings instead or even better use datetime literals.
select *
from VM_REPORT_TEMP_US
where START_DATE >= timestamp '2016-02-01 00:00:00'
and END_DATE <= timestamp '2016-02-24 00:59:00'
Bit different from both the above answers, but this is what I would use to avoid confusion between YYYY-MM-DD and YYYY-DD-MM.
select * from VM_REPORT_TEMP_US
where
START_DATE >= to_date('2016-02-01 00:00:00','YYYY-MM-DD HH24:MI:SS')
AND
END_DATE <= to_date('2016-02-24 00:59:00','YYYY-MM-DD HH24:MI:SS')