Timezone date format in Oracle - sql

I have to convert a SYSDATE date to a specific date format. That format must be something like this: '2016-11-23T15:12:48Z'. I think this is a weird date format but is the requirements that I have.
This must be a date to send in a Web Service message.
In Oracle (12c or 11g) I have some function to transform a date in this specific format? Thanks.

That will give you ISO-8601 mentioned in comment:
select to_char(systimestamp,'YYYY-MM-DD"T"hh24:mi:sstzh:tzm') isodt from dual;
If you really want Z instead of timezone you can use:
select to_char(cast(systimestamp as timestamp) at time zone 'UTC',
'yyyy-mm-dd"T"hh24:mi:ss"Z"')
from dual;

Since the "Z" in the timestamp format you're after means "This is in UTC", you should first make sure your sysdate is returned in UTC. You can do this by using systimestamp and sys_extract_utc() like so:
select to_char(sys_extract_utc(systimestamp), 'yyyy-mm-dd"T"hh24:mi:ss"Z"') dt_as_utc
from dual;
DT_AS_UTC
--------------------
2016-11-28T10:33:49Z

Related

Oracle SQL Select Current Timestamp without Timezone and 24hr Format

I have a Oracle SQL statement where I have to get the current timestamp as one of the columns. But I dont require the Timezone which CURRENT_TIMESTAMP gives or the AM/PM given by LOCALTIMESTAMP.
I require the current timestamp in 24hr format without the timezone.
Is it possible to get that in Oracle SQL?
It seems you're mixing 2 concepts here: "datatype" and "date format mask".
data type: LOCALTIMESTAMP returns datatype TIMESTAMP and CURRENT_TIMESTAMP returns datatype TIMESTAMP WITH TIME ZONE. TIMESTAMP is similar to DATE but has a higher precision. As usual... checking the docs is worth it.
date format mask: determines how you display the date information. Americans can't read 24 hour format, the rest of the world is confused by AM/PM. Fortunately, you can decide how you want to display the date as explained in the oracle docs.
If you just want to return the current date in 24 hour format you could do something like:
SELECT
TO_CHAR(SYSDATE,'DD-MON-YYYY HH24:MI:SS') as mydate,
<other columns>
FROM
<table_name>
If you need the date to be more precise and you require fractional seconds then you can use SYSTIMESTAMP instead of DATE with a format mask 'DD-MON-YYYY HH24:MI:SS.FF9'

Convert Date datatype in oracle to UTC format using oracle query

I have a date column in this format "03-FEB-16" in a database table.
I want to query the database to get UTC format equivalent of that date value:
This is the UTC format I am looking for
"2015-12-17T14:30:33Z"
Try this:
SELECT TO_CHAR(cast(SYSDATE as timestamp) at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') FROM DUAL
Change SYSDATE to say SYSDATE-150 to test different times of the year
I think I found my answer but Please feel free to post alternative ways to do it.
The following query did the trick:
select to_char(sysdate,'YYYY-MM-DD')||'T'||to_char(sysdate,'HH24:MI:SS')||'Z' from dual

Oracle SQL how to convert time zone string to date

I have following 2015-06-17T00:00:00.000+05:00 string.
I want to convert this string to Date using oracle sql.
I tried lot of format mask but none works for me :
SELECT TO_DATE('2015-06-17T00:00:00.000+05:00','yyyy-mm-dd HH24:MI:SS TZR') FROM DUAL;
Any idea which format mask should i apply for above conversion.
Also please note that i only need date information i.e (mm-dd-yyyy). So its also ok if the conversion results in date information only (i.e skipping time information)
This should work:
SELECT TO_DATE(SUBSTR('2015-06-17T00:00:00.000+05:00',1,10),'yyyy-mm-dd') from dual
If you need to keep track of the time zone you should probably look at something like this:
SELECT CAST(TO_TIMESTAMP_TZ('2015-06-17T00:00:00.000+05:00','yyyy-mm-dd"T"HH24:MI:SS.FFTZH:TZM') AT TIME ZONE 'UTC' AS DATE) FROM DUAL;

ORA-01722 INVALID NUMBER in oracle

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

Formatting dates in PostgreSQL

I have a field which its format is date with time as: YYYY-MM-DD HH-MM-SS for example: 2000-08-12 00:00:00 I want to get just the date part and change its format to DD/MMM/YYYY for example the expected result of the previous example will be: 12/Aug/2000
The field definition is: Ddate timestamp without time zone DEFAULT now()
I read the whole page of Date/Time Functions and Operators and other sources as well but I couldn't find any information that is helpful.
You can use the to_char function to format your column:
SELECT TO_CHAR(ddatte, 'dd/Mon/yyyy') FROM mytable
try with:
to_char(your_Field, 'dd/mm/yyyy')