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
Related
I am using following function.
TO_DATE(TO_CHAR (SYSDATE, 'YYYY-MON-DD HH24:MI:SS'),'yyyy/mm/dd hh24:mi:ss')
Its working fine if I am updating data from a simple query like:--
set modified_on= TO_DATE(TO_CHAR (SYSDATE, 'YYYY-MON-DD HH24:MI:SS'),'yyyy/mm/dd hh24:mi:ss')
But not working in case of stored procedure.
Note:- In stored procedure I am using dynamic query. Execute Immediate....
No error but only date getting inserted or updated not time.
Now:-- "31-07-2017"
Need Something like:-- "31-07-2017 hh:mi:ss"
Thanks in Advance for any help.
I might be going mad, but I'm sure this exact question has been posted verbatim before, in all its craziness. Anyway.. You wrote:
TO_DATE(TO_CHAR (SYSDATE, 'YYYY-MON-DD HH24:MI:SS'),'yyyy/mm/dd hh24:mi:ss')
This doesnt make sense. You have a date, you convert it to a string looking like one format and immediately try and convert it back to date, but using a different format. You can't do this: it doesnt make sense to take the date right now, and convert it to a string looking like 2017-JUL-31 18:28:00 and then ask oracle to interpret it using slashes and a numerical month - the string you just had it prepare has dashes, not slashes. It has a text month name not a numerical month identifier.. It won't work. Oracle will hit the first - and expect a \ and it'll choke.
Just use the date you have to start with, as a date:
SYSDATE
i.e:
set modified_on= SYSDATE
In Oracle, Current Date + Time = Sysdate. select sysdate from dual would produce 2017-07-31 23:18:40
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.....
I have the following query:
select *
from mytable
where to_char(mydate,'mm/dd/yyyy') between ('05/23/2013')
and ('06/22/2013')
I need to change it to make dynamically so that I won't modify it every month from 05/23/2013 to 06/23/2013 for example:
('05/23/' + (select to_char(sysdate, 'yyyy') from dual))
but this is giving an error. Any suggestions?
What I need to do: Every month I need to run this query to get the records between 23rd of this month and 23rd of the last month.
Oracle uses || as the concatenation operator:
('05/23/' || (select to_char(sysdate, 'yyyy') from dual))
BTW, David is right. If you really want to compare string representations of dates (but why?), use a date format that is ordered the same way as dates:
to_char(mydate,'yyyy/mm/dd')
You're performing a comparison on strings, not on dates, so your code doesn't work the way you think it does.
Based on the string logic, "05/23/2000" is between "05/22/2013" and "06/24/2000".
Keep the data types as date and Oracle will get the comparison right.
Possibly what you want is:
select *
from my_table
where mydate >= add_months(trunc(sysdate,'MM'),-1)+22 and
mydate < trunc(sysdate,'MM')+22
but it's difficult to tell without a description of what the requirement actually is.
How about this one?
SELECT '(''05/23/'''||to_char(sysdate, 'yyyy')||'''' FROM DUAL
Have not testet because I have no Oracle database right now, needs checking for quote escapes...
Do you need exact days from the month? You can also substract days from sysdate:
SELECT (sysdate - 30) FROM DUAL
You may also use concat function
concat('05/23/', (select to_char(sysdate, 'yyyy') from dual))
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)