Currently I'm using MyTimeStampField-TRUNC(MyTimeStampField) to extract the time part from a timestamp column in Oracle.
SELECT CURRENT_TIMESTAMP-TRUNC(CURRENT_TIMESTAMP) FROM DUAL
This returns
+00 13:12:07.100729
This works OK for me, to extract the time part from a timestamp field, but I'm wondering if there is a better way (may be using a built-in function of ORACLE) to do this?
What about EXTRACT() function?
You could always do something like:
select TO_DATE(TO_CHAR(SYSDATE,'hh24:mi:ss'),'hh24:mi:ss') from dual
I believe this will work with timestamps as well.
You want just date then use
to_char(cast(SYSDATE as date),'DD-MM-YYYY')
and if you want just time then use
to_char(cast(SYSDATE as date),'hh24:mi:ss')
the parameters are making all the changed
'DD-MM-YYYY'
and
'hh24:mi:ss'
This may help:
Select EXTRACT(HOUR FROM (SYSDATE - trunc(sysdate)) DAY TO SECOND ) From dual;
select TO_DATE(TO_CHAR(SYSDATE,'hh24:mi:ss'),'hh24:mi:ss') from dual
This gives the timestamp for 1hour less than the actual.
select hour(CURRENT_TIMESTAMP)
Related
Quick help on this line of my code in my oracle database. So, I have a to_char with sysdate. However, I want to change the sysdate to say Jul-2020 but for some reason it tells me invalid number. Can anyone help me solve this small issue? thanks for the help.
here is what I have:
Before:
to_char(sysdate, 'YYYY')
After:
to_char('Jul-2020', 'MM-YYYY'
The problem is first you have let the dB know "Jul-2020" is a date format so the correct line should be to_char(to_date('Jul-2020','Mon-yyyy'), 'MM-YYYY')
Something along these lines should work as long as you provide input dates as below. You just need to be consistent, meaning you can't do 2020-July without changing output format to YYYY-MM
select to_char(to_date('07-2020','MM-YYYY'),'MM-YYYY') from dual;
select to_char(to_date('July-2020','MM-YYYY'),'MM-YYYY') from dual;
If you want to be able to use both sysdate and hardcoded values inter-changeably, you can provide date in a specific format that works for sysdate and hardcoded date
select to_char(to_date(sysdate,'DD-MM-YYYY'),'MM-YYYY') from dual;
select to_char(to_date('01-07-2020','DD-MM-YYYY'),'MM-YYYY') from dual;
select to_char(to_date('01-July-2020','DD-MM-YYYY'),'MM-YYYY') from dual
This is building on the question I asked yesterday Link
I'm pulling time from the 'Flight' column whose data looks like this:
Dayton 01:23:59
Which gives me this:
01:23:59
I then want to add 1 second to this. I'm using the following TO_DATE function:
to_date(substr(Flight,length(Flight)-8,8), HH:MI:SS') + interval '1' second
This works but the format includes the date:
2018-07-01T01:24:00.000+00:00
I need it to look like this:
01:24:00
I've tried using the SUBSTR to extract only the time to no avail.
Any ideas on how I can add 1 second to the above and preserve the HH24:MI:SS format?
The direct way is using to_char string conversion function with HH24:MI:SS pattern as
with t(myTime) as
(
select to_date(substr('Dayton 01:23:59',length('Dayton 01:23:59')-8,9), 'HH:MI:SS')
+ interval '1' second
from dual
)
select to_char(myTime, 'hh24:mi:ss') as myTime_Chr from t;
MYDATE_CHR
01:24:00
or alternatively regexp_replace function maybe used as (provided that the city name doesn't contain a digit):
select regexp_replace('Dayton 01:24:00','[^0-9:]') as myTime
from dual;
MYTIME
01:24:00
P.S. dual maybe replaced by your real table name.
But seperating this column into two seperate columns as city and time is better.
SQL Fiddle Demo
How can we convert timestamp to date?
The table has a field, start_ts which is of the timestamp format:
'05/13/2016 4:58:11.123456 PM'
I need to query the table and find the maximum and min timestamp in the table but I'm not able to.
Select max(start_ts)
from db
where cast(start_ts as date) = '13-may-2016'
But the query is not returning any values.
Please help me in finding the max timestamp for a date.
CAST(timestamp_expression AS DATE)
For example, The query is : SELECT CAST(SYSTIMESTAMP AS DATE) FROM dual;
Try using TRUNC and TO_DATE instead
WHERE
TRUNC(start_ts) = TO_DATE('2016-05-13', 'YYYY-MM-DD')
Alternatively, you can use >= and < instead to avoid use of function in the start_ts column:
WHERE
start_ts >= TO_DATE('2016-05-13', 'YYYY-MM-DD')
AND start_ts < TO_DATE('2016-05-14', 'YYYY-MM-DD')
Format like this while selecting:
to_char(systimestamp, 'DD-MON-YYYY')
Eg:
select to_char(systimestamp, 'DD-MON-YYYY') from dual;
If the datatype is timestamp then the visible format is irrelevant.
You should avoid converting the data to date or use of to_char. Instead compare the timestamp data to timestamp values using TO_TIMESTAMP()
WHERE start_ts >= TO_TIMESTAMP('2016-05-13', 'YYYY-MM-DD')
AND start_ts < TO_TIMESTAMP('2016-05-14', 'YYYY-MM-DD')
You can try the simple one
select to_date('2020-07-08T15:30:42Z','yyyy-mm-dd"T"hh24:mi:ss"Z"') from dual;
You can use:
select to_date(to_char(date_field,'dd/mm/yyyy')) from table
I'd go with the following:
Select max(start_ts)
from db
where trunc(start_ts) = date'13-may-2016'
If you have milliseconds in the date string, you can use the following.
select TO_TIMESTAMP(SUBSTR('2020-09-10T09:37:28.378-07:00',1,23), 'YYYY-MM-DD"T"HH24:MI:SS:FF3')From Dual;
And then convert it to Date with:
select trunc(TO_TIMESTAMP(SUBSTR('2020-09-10T09:37:28.378-07:00',1,23), 'YYYY-MM-DD"T"HH24:MI:SS:FF3')) From Dual;
It worked for me, hope it will help you as well.
This may not be the correct way to do it. But I have solved the problem using substring function.
Select max(start_ts), min(start_ts)from db where SUBSTR(start_ts, 0,9) ='13-may-2016'
using this I was able to retrieve the max and min timestamp.
I am new to SQL and have run into a problem. I want to have the epoch date i.e. 1970-01-01T00:00:00Z in this mentioned format.
I cannot use it as a constant (i.e. '1970-01-01T00:00:00Z') for programming reasons. I need a statement that gives this as an output. I have used this:
select to_char(TRUNC(add_months(sysdate,-555),'MM'), 'YYYY-MM-DD"T"HH24:MI:SS"Z"') from dual;
But the only problem with this statement is it will not give me the date I want next month i.e. it is month specific it will only work for April 2016. But I need a the date to always remain 1970-01-01T00:00:00Z.
Thanks in advance for the help.
PS: I am using Oracle SQL Developer (if that matters).
You can just do:
select '1970-01-01T00:00:00Z' from dual;
Or if you want to have it processed for some reason, which seems like pointless overhead:
select to_char(date '1970-01-01', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') from dual;
Either will give you the string you want. But it is a string, not a date. If you want it as a proper data type (which I don't think you do, but maybe this is for comparison) it needs to be a timestamp with time zone, which you can get with:
select timestamp '1970-01-01 00:00:00 UTC' from dual;
Well this might not be the most elegent way but it is a way that will work:
select regexp_substr('1970-01-01T00:00:00Z','^....................',1,1) from dual;
I've seen a few answers to questions similar to mine but I cannot get them to work. I have several date fields in my query that return the date and time like such 7/1/2014 12:00:00 AM. Is there a way I can just have the fields show 7/1/2014?
SELECT DISTINCT
C.RECEIPTDATE,
(I.CLIENTID ||' - '||PO.CLIENTNAME) AS CLIENT,
D.INVOICEID,
D.SVCFROMDATE,
D.SVCTODATE,
D.SVCCODE
FROM M_EQP_ORDERS
WHERE.....
I basically would like to cut down the two date fields to the shorter date format minus the time.
Thanks in advance!
Just use the function TRUNC.
SELECT DISTINCT
TRUNC(C.RECEIPTDATE),
(I.CLIENTID ||' - '||PO.CLIENTNAME) AS CLIENT,
D.INVOICEID,
TRUNC(D.SVCFROMDATE),
TRUNC(D.SVCTODATE),
D.SVCCODE
FROM M_EQP_ORDERS
WHERE.....
Use to_char function:
SELECT DISTINCT
to_char(C.RECEIPTDATE,'DD/MM/YYYY'),
(I.CLIENTID ||' - '||PO.CLIENTNAME) AS CLIENT,
D.INVOICEID,
D.SVCFROMDATE,
D.SVCTODATE,
D.SVCCODE
FROM M_EQP_ORDERS
WHERE.....
DEPENDS on the data type.
If the column is DATE data type, then, as suggested already, TRUNC would do the job to display. But, if your locale-specific NLS date settings are different, then you will still see the time portion as midnight.
Else, you need to use TO_DATE with proper FORMAT and apply TRUNC to it.
update
If you only want to display, use TO_CHAR, else, if you have a filter in your WHERE clause, then remember TO_CHAR doesn't return DATE, it converts it into literal.
Try this:
SQL> select to_char(sysdate, 'YYYY/MM/DD') dateonly, sysdate datetime from dual;
DATEONLY DATETIME
---------- -------------------
2014/09/26 2014-09-26 15:41:03
The Oracle date datatype always includes the time.
TRUNC will truncate the time to midnight, which you will need to do if you want to match the date parts of two datetimes. The time may still display, depending on how your client is configured, so use TO_CHAR with an appropriate format mask to display it whatever way you want.
SELECT DISTINCT
to_date(C.RECEIPTDATE,'DD/MM/YYYY'),
(I.CLIENTID ||' - '||PO.CLIENTNAME) AS CLIENT,
D.INVOICEID,
D.SVCFROMDATE,
D.SVCTODATE,
D.SVCCODE
FROM M_EQP_ORDERS
WHERE.....