I have a file contains a string like 2011-03-14 11:57:42+08:00 and I need to store it in a column with DATE datatype. I use Oracle 11g.
I have tried TO_DATE function but it didn't work.
Can anyone help me?
thanks in advance
You have to use the function TO_TIMESTAMP_TZ to get the timezone properly parsed:
SELECT CAST(TO_TIMESTAMP_TZ('2011-03-14 11:57:42+08:00', 'YYYY-MM-DD HH24:MI:SS+TZH:TZM') AS DATE) AS my_date
FROM DUAL
;
(And as a side note to your example in the comment: no need for TO_CHAR, quoting was missing and format model MON (abbreviated month) was incorrect)
Related
I'm trying to achieve the MySQL equivalent of date_format( your_date_column, '%Y-%m-%d %H' ) as my_date in Hive. I've tried a few options from Hive date formatting but can't get the format right. I haven't found anything that has helped me yet.
Could I please request someone who may have already bumped into this situation or knows how to do it please?
Recent version of Hive have a date_format() function, it just uses java formatting codes instead of C. Try date_format(your_date_column, 'yyyy-MM-dd HH')
To convert date to given string format you have to use from_unixtime() function of hive
from_unixtime(bigint unixtime[, string format]) converts the number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a string representing the timestamp of that moment in the current system time zone.
The sample input and output can be seen from below image:
The final query is
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH') as my_date from table1;
where table1 is the table name present in my hive database.
I hope this help you to achieve date_format( your_date_column, '%Y-%m-%d %H' ) !!!
Let's say you have a column 'birth_day_H' in your table which is in string format,
you should use the following query to filter using birth_day_H
date_Format(birth_day_H, 'yyyy-MM-dd HH')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_day_H, 'yyyy-MM-dd HH') = '2019-04-16 10';
I worked around this by using concat(substr(your_date_column,1,13), ':00')
In case the date column has a reserved keyword such as timestamp as in my case, this works - concat(substr(`timestamp`,1,13), ':00')
Below is the example
2016-02-06T06:06:41+00:00
will be converted to
2/6/2016 6:06:00 AM
Thanks in advance
Use TO_CHAR to get a formatted string from a timestamp:
select to_char(timestamp '2016-02-06 06:06:41 +00:00',
'fmdd/mm/yyyy fmhh:mi:ss am') from dual;
(The FM in the format exist to suppress leading zeros.)
I am trying to convert the date from YYYYMMDD to DD-Mon-YYYY in Oracle, but to_char or to_Date is not working. Can you please advise?
select to_date(20150324,'DD-Mon-YY') from dual;
select to_char(20150324,'DD-Mon-YY') from dual;
I get an error message saying: - ORA-01861: literal does not match format string
Use this combination of to_char and to_date:
select to_char (to_date('20150324','YYYYMMDD'), 'DD-Mon-YY') from dual;
Your mistake was, that you used the wrong date pattern. Additionally it's recommended to add'', though it worked without them in this case.
Check this Fiddle.
I am getting invalid number error message while executing the below select statement.Can any one have an idea about the issue..Please let me know.
select TO_DATE(TO_CHAR('2015/01/22 00:00:00','YYYY/MM/DD'),'YYYY/MM/DD')
actually i want oracle standard date format without time stamp for this date '2015/01/22 00:00:00'
select to_date('2015/01/22 00:00:00','YYYY/MM/DD HH24:MI:SS') as dt
from dual
Fiddle - http://sqlfiddle.com/#!4/6a3a6/1/0
As an FYI, the Oracle DATE data type does include the time component (just not down to fractional seconds, as is the case with the TIMESTAMP data type).
If you are converting values and want to bring all the time values to zero you can use the trunc function like this (which changes 12:07:00 to 00:00:00):
select trunc(to_date('2015/01/22 12:07:00','YYYY/MM/DD HH24:MI:SS'),'DD') as dt_with_time_zerod
from dual
Fiddle - http://sqlfiddle.com/#!4/6a3a6/2/0
If the source is itself a date and you want to convert the date to a string in the Oracle default date format ('DD-MON-RR') you can achieve that by running:
select to_char(trunc(to_date('2015/01/22 12:07:00','YYYY/MM/DD HH24:MI:SS'),'DD'),'DD-MON-RR') as dt_with_time_zerod
from dual
Fiddle - http://sqlfiddle.com/#!4/6a3a6/3/0
If it's a date field, to_char without a mask will give you what you say you want.
actually i want oracle standard date format without time stamp for this date '2015/01/22 00:00:00'
I'm not sure what you mean by "Oracle standard date format." The format in which a date would appear would be based on your NLS settings (in particular, NLS_DATE_FORMAT). If you are just trying to format this string representing a date, then you might want something like the following:
SELECT TO_CHAR(TO_DATE('2015/01/22 00:00:00','YYYY/MM/DD HH:MI:SS'), 'YYYY/MM/DD')
FROM dual;
That is, you have the TO_CHAR() and TO_DATE() functions in the wrong order, and an incomplete date mask for the call to TO_DATE().
Try using date literals with the standard ISO 8601 format.
date '2015-01-22'
I suggest you not to give hour-minute-second if you do not want to show the time.
This is my simplest answer :
SELECT TO_DATE('2015/01/22','YYYY/MM/DD') FROM dual
I have the following statement being run on an oracle database.
SELECT br.Number
FROM Billing_History br
WHERE TRUNC(br.History_Date) BETWEEN to_date('01-Jan-99', 'DD-Mon-YY HH:MI:SS')
AND to_date('11-May-99', 'DD-Mon-YY HH:MI:SS')
There are definitely records in that table that fall between those dates. And they all have a Number that goes with them, but for some reason this isn't returning any Numbers. It's returning nothing at all.
The dates in the database are in this format '01-Jan-11'. So it seems like I'm putting the dates in the correct format too. Do you see anything wrong with the SQL I wrote?
The problem is not the time component of the format model, it's the 'YY' component, which would mean in your year is converted to 2099, not 1999. Try this to illustrate:
SQL> SELECT to_char(to_date('01-Apr-99','DD-Mon-YY'),'DD-Mon-YYYY') thedate
FROM dual;
THEDATE
-----------
01-Apr-2099
SQL>
Either use RR or YYYY as a format model component for year when using 20th century dates.
Edit:
You make the statement "The dates in the database are in this format '01-Jan-11'." This is a common, but incorrect, interpretation of dates in Oracle. DATE fields are always stored in the same internal format. It's all about how you use the format model in conversion functions that dictates how the data is converted to/from internal format.
Use RR in your date format instead of YY. It is probably picking up those dates as 2099 instead of 1999.
SELECT br.Number FROM Billing_History br WHERE
TRUNC(br.History_Date) BETWEEN to_date('01-Jan-99', 'DD-Mon-RR HH:MI:SS')
AND to_date('11-May-99', 'DD-Mon-RR HH:MI:SS')
Try removing the time part from the second to_date parameter:
to_date('11-May-99', 'DD-Mon-YY')
Or even better:
to_date('11-05-1999', 'DD-MM-YYYY')
This is more robust as it is language agnostic and doesn't need to guess the century.