Oracle SQL Select Current Timestamp without Timezone and 24hr Format - sql

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'

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.

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;

EXTRACT the date and time - (Teradata)

I am trying to extract the date and time from a field in Teradata.
The field in question is:
VwNIMEventFct.EVENT_GMT_TIMESTAMP
Here is what the data look like:
01/02/2012 12:18:59.306000
I'd like the date and time only.
I have tried using EXTRACT(Date, EXTRACT(DAY_HOUR and a few others with no success.
DATE_FORMAT() does not appear to work since I'm on Teradata.
How would I select the date and time from VwNIMEventFct.EVENT_GMT_TIMESTAMP?
If the datatype of EVENT_GMT_TIMESTAMP is a TIMESTAMP, it's simple Standard SQL:
CAST(EVENT_GMT_TIMESTAMP AS DATE)
CAST(EVENT_GMT_TIMESTAMP AS TIME)
If it's a CHAR you need to apply a FORMAT, too:
CAST(CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP FORMAT 'dd/mm/yyyyBhh:mi:SS.s(6)') AS DATE)
CAST(CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP FORMAT 'dd/mm/yyyyBhh:mi:SS.s(6)') AS TIME)
Edit:
For simply changing the display format you need to add a FORMAT and a CAST to a string:
CAST(CAST(EVENT_GMT_TIMESTAMP AS FORMAT 'YYYYMMDDHHMI') AS CHAR(12))
or
CAST(CAST(EVENT_GMT_TIMESTAMP AS FORMAT 'YYYYMMDDHHMISS') AS CHAR(14))
If you don't care about display, just want to truncate the seconds:
EVENT_GMT_TIMESTAMP - (EXTRACT(SECOND FROM EVENT_GMT_TIMESTAMP) * INTERVAL '1.000000' SECOND)
Working with timestamps is a bit tricky :-)
I know this is an old topic, but I've struggled with this too. Try:
CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP(0))
The result will be
01/02/2012 12:18:59
The datatype will still be timestamp, but it will just be the date and time with no microseconds (looks just like a datetime object in Microsoft SQL).

Oracle DateTime query with time zones

I have a SQL Builder library that direcltly uses ADO.NET. I have a means of creating a select query with a greater-than-or-equal operator, like:
select *
from book
where book.date_created >= {some date}
My issue is that {some date} is going to always be in the UTC time zone, but it's being compared to the book.date_created column which is a TIMESTAMP(6) WITH TIME ZONE column, which will not be in the UTC timezone.
I can execute the query, but my results are off becuaes of timezone comparisons. My query is for all books where the date_created >= x, but some of the results returned are not greater than x because after subtracting 5 hours for the time zone, they are now less than x. The IDataRecord DateTime fields returned are converted to UTC using DateTime.SpecifyKind()
Can I form my query such that it interprets book.date_created in the UTC timezone?
Note: While I'd love to change my Oracle DB columns to not specify timezones, changing table structures is not something I can do.
Edit:
Currently, {some date} is a SQL Parameter. It's backing datatype is a DateTime with UTC as the timezone. As a parameter, it is a TimestampWithTZ. The Value of the parameter is a DateTime with the kind specified as UTC as well.
Update:
The issue seems to be related to my results set from the IDataRecord. When I pull DateTimes off, I use DateTime.SpecifyKind() to put them in UTC mode. The problem is, the date times come out as DateTimeKind.Unspecified. When converting from Unspecified to UTC, it just drops the timezone and declares it is UTC without changing the underlying value. I'm not sure how to have the IDataRecord pull in the TimeZone value.
You need to use the FROM_TZ function that transforms a TIMESTAMP into a TIMESTAMP WITH TIME ZONE. For example, if you know that your variable is in UTC time (+0:00):
SELECT *
FROM book
WHERE date_created >= from_tz(<timestamp>, '+0:00');
Here's a sample script that shows the behaviour you describe (your local time zone should be set to +1:00):
CREATE TABLE t (tz TIMESTAMP(6) WITH TIME ZONE);
INSERT INTO t VALUES
(to_timestamp_tz('20000101 00:00:00 +1:00','yyyymmdd hh24:mi:ss tzh:tzm'));
INSERT INTO t VALUES
(to_timestamp_tz('20000101 00:00:00 -1:00','yyyymmdd hh24:mi:ss tzh:tzm'));
-- This will return two values instead of one
SELECT *
FROM t
WHERE tz >= to_timestamp('20000101 00:00:00', 'yyyymmdd hh24:mi:ss');
-- This query will return only one row
SELECT *
FROM t
WHERE tz >= from_tz (to_timestamp('20000101 00:00:00',
'yyyymmdd hh24:mi:ss'), '+0:00');
below links will help you.
Datetime Datatypes and Time Zone Support
TIMESTAMP WITH TIME ZONE Data Type
Write Time Zone Aware Code in Oracle
ORACLE timezone summary
Oracle Date and Time data types

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