how to use trunc date() function in oracle.? - oracle

When I run following query I got error.Can Someone help me to solve this.
select trunc('27-oct-97','dd-mm-yy') form dual;
when I run this query i got following error:
Error:inline number function.

The TRUNC function has the signature:
TRUNC( date_value, format )
You are providing a string value instead of a date value and 'dd-mm-yy' is an invalid format (you just want to truncate to the start of the day using 'dd' as the format or the start of the month using 'mm' or the start of the year using 'yy' - but using all three together does not make sense).
You can use ANSI date literals to specify the date:
SELECT TRUNC(
DATE '1997-10-27',
'DD'
)
FROM DUAL
Or use the TO_DATE function:
SELECT TRUNC(
TO_DATE( '27-oct-97', 'DD-MON-RR' ),
'DD'
)
FROM DUAL
(and you can also omit the 'DD' format argument as the default is to truncate to the start of the day.)
But, in both cases, the truncation is redundant as the date's time component is already midnight so does not need truncating.
What you want is a date value that has a non-midnight time component:
SELECT TRUNC(
TO_DATE( '1997-10-27 12:34:56', 'YYYY-MM-DD HH24:MI:SS' ),
'DD'
)
FROM DUAL
or to truncate to the start of the month or year:
SELECT TRUNC(
DATE '1997-10-27',
'MM'
)
FROM DUAL

You have two options:
1) Remove the time portion from the date truncate into the nearest date:
TRUNC(DATE)
2) Use format, to specify which values to trunc:
TRUNC(DATE,year)
Truncates (will turn to 1) everything that is before the format , in this example the year.
So this:
trunc(to_date('27-oct-97','dd-mm-yy'),'YEAR')
Will output this:
01-jan-97

Related

how to get previous month or last two month data in oracle

how to get previous month or last two month data in oracle.
My date format is YYYY,MM,DD.
from google search i got those solution,
select * from IM_LAPTOP
where ADD_DATE >= add_months(sysdate, -12);
select *
from IM_LAPTOP
where ADD_DATE between add_months(trunc(sysdate,'mm'),-1) and last_day(add_months(trunc(sysdate,'mm'),-1));
But its showing not a valid month
My date format is YYYY,MM,DD
Unless you are using a string to store dates then, no, it is not; a DATE is a binary data type (consisting of 1-byte for each of: century, year-of-century, month, day, hour, minute and second and it always has those components) and it has no format.
how to get previous month or last two month data in oracle
To get the data from 2 months before this instant in time (i.e. if it is now 2021-04-05 16:39:24 and you want it from 2021-02-05 16:39:24, two months prior) then:
SELECT *
FROM your_table
WHERE date_column >= ADD_MONTHS( SYSDATE, -2 )
To get the data starting from midnight on the 1st day of last month:
SELECT *
FROM your_table
WHERE date_column >= ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), -1 )
If you only want the data from the preceding month then:
SELECT *
FROM your_table
WHERE date_column >= ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), -1 )
AND date_column < TRUNC( SYSDATE, 'MM' )
If your "date" is actually a VARCHAR2 column with the format YYYY,MM,DD then you should change it to a DATE column but if for some reason you cannot then at least the characters are in order of highest-to-least significance and you can perform an alphanumeric comparison and just wrap the right-hand side of the filters in TO_CHAR:
SELECT *
FROM your_table
WHERE date_column >= TO_CHAR( ADD_MONTHS( SYSDATE, -2 ), 'YYYY,MM,DD' )

Date of release from 1-jan-xx year until now

I create query which display all item which is cancel in period
start.date - end.date
select substr(tarifa,1,2) as tarifa, count(*) as komada
from pol p, uvod u, doppov d
WHERE (datum_dop >='1-jan-07') AND (datum_dop<='1-jul-13')
and izdavanje>='01-jul-10'
and p.orgjed = u.sorgz (+)
and p.polica=d.polica and d.pov_dopl='P'
--and DATUM_PREKIDA is not null
and d.status='F'
and cisti_ao(p.polica)!=0
group by substr(tarifa,1,2)
Now I want to edit this query column izdavanje. If user enter '27-sep-xx year' it need to display item in period start-date '01-jan-xx' until '27-sep-xx year'
So start date need to be always '1-jan-xx year' and end date need to be entered date like '19-aug-xx'.
Any idea how to fix this problem ?
This:
and izdavanje>='01-jul-10'
will become
and izdavanje between to_date('01.01.' || substr(:BLOK.IZDAVANJE, -2), 'dd.mm.rr')
and to_date(:BLOK.IZDAVANJE, 'dd-mon-rr')
substr will return xx year
to_date will convert the whole value into a valid date
Now, as it is Forms, you might need to adjust it a little bit (depending on column datatype as well as form item's datatype), but - that's the general idea.
You want to use TRUNC( date_value, 'YY' ) to truncate it to the start of the year:
AND izdavanje BETWEEN TRUNC( '01-jul-10', 'YY' ) AND '01-jul-10'
However, you should also note that '01-jul-10' is a text literal so, when a date is required, Oracle has to perform an implicit conversion from the text literal to a date. You would be better performing an explicit conversion (as Oracle's default format is the NLS_DATE_FORMAT session parameter and ANY user can change this value in their own session and changing that will break your query without any modification of your code).
For example, to perform an explicit cast to a date:
AND izdavanje BETWEEN TRUNC(
TO_DATE( '01-jul-10', 'DD-MON-RR', 'NLS_DATE_LANGUAGE = American' ),
'YY'
)
AND TO_DATE( '01-jul-10', 'DD-MON-RR', 'NLS_DATE_LANGUAGE = American' )
Also, if your izdavanje date values have time components then '01-jul-10' will be converted to a date at midnight (00:00:00) so you will not get any values returned for that day that have time components between 2010-07-01 00:00:01 and 2010-07-01 23:59:59. If this is relevant and you want those values returned then you should use:
AND izdavanje >= TRUNC(
TO_DATE( '01-jul-10', 'DD-MON-RR', 'NLS_DATE_LANGUAGE = American' ),
'YY'
)
AND izdavanje < TO_DATE( '01-jul-10', 'DD-MON-RR', 'NLS_DATE_LANGUAGE = American' )
+ INTERVAL '1' DAY
You can modify a date inside a query using TO_CHAR() function:
Instead of:
and izdavanje>='01-jul-10'
use this:
and izdavanje between TO_CHAR( :BLOK.IZDAVANJE, 'YYYY-01-01' )
and TO_CHAR( :BLOK.IZDAVANJE, 'YYYY-MM-DD 23:59:59' )
TO_CHAR( :BLOK.IZDAVANJE, 'YYYY-01-01' ) takes only the year from the date and adds '-01-01' (January 1st) to it.
TO_CHAR( :BLOK.IZDAVANJE, 'YYYY-MM-DD 23:59:59' ) adds the max time of the day to the date because 'between xxx and yyy' means equal or less than 00:00:00 of day yyy and all timestamps with a timestamp greater than 00:00:00 but the same day won't be covered when using time values in :BLOK.IZDAVANJE.

how to truncate yyyy/mm/dd hh:mm:ss.SSS to mm/dd/yyyy in oracle

When I ran a query to get date it is retrieved in this format 'yyyy/mm/dd hh:mm:ss.SSS' but I need to convert it to mm/dd/yyyy.
I'm using this query for conversion
select
to_char(
add_months (
to_date(
to_char(
trunc(
TO_DATE('2016/01/01 00:00:00.0', 'YYYY/MM/DD HH24:MI:SS.SSS')
), 'MM/DD/YYYY' -- to char
),'MM/DD/YYYY' -- to date
), -2*1 -- add months
), 'MM/DD/YYYY' -- to char
) START_DATE,
to_char(
add_months (
to_date(
to_char(
trunc(
TO_DATE('2017/01/01 00:00:00.0', 'YYYY/MM/DD HH24:MI:SS.SSS')
), 'MM/DD/YYYY' -- to char
), 'MM/DD/YYYY' -- to date
), 3 -- add months
), 'MM/DD/YYYY' -- to char
) END_DATE
from dual;
Output is
ORA-01810: format code appears twice
01810. 00000 - "format code appears twice"
The problem is in the conversion of to_date itself. The below conversion itself is throwing the error you mentioned
select
TO_DATE('2017/01/01 00:00:00.0', 'YYYY/MM/DD HH24:MI:SS.SSS') END_DATE
from dual;
You need to use it like below if you want to convert the string with timestamp to timestamp
select TO_TIMESTAMP('2017/01/01 00:00:00.0', 'YYYY/MM/DD HH24:MI:SS.FF') from dual
This will simply satisfy your need rather than making so many conversions.
select to_char(
add_months(
TO_TIMESTAMP('2017/01/01 00:00:00.0', 'YYYY/MM/DDHH24:MI:SS.FF'),
-2),
'mm/dd/yyyy') from dual
ORA-01810: format code appears twice
That's because of SS.SSS. SSS is not a valid date format. You are trying to handle fractional seconds but:
the correct format mask for that is FF
DATE doesn't support fractional seconds, only TIMESTAMP
Really date format is a display issue and should be handled by the client's NLS settings. But if you really must do it in SQL this is all you need:
select
to_char(DATE '2015-11-01', 'MM/DD/YYYY') START_DATE
, to_char(DATE '2017-04-01', 'MM/DD/YYYY') END_DATE
from dual;
You don't need trunc() because the date literals are already set to midnight. You don't need add_months() because you can just change the value of the date literal. You don't need to cast the date to a string back to a date because you just don't.

Time format from 11:00:00 AM to 11:00:00 in Oracle

In the Table REC_ALL_TRADES I have the column as CALCTM which have the value like 11:00:00 AM and other also with out am.
I want to remove the AM from the value. Can you please let me know how to do this?
If the data type of the CALCTM column is a DATE or TIMESTAMP then the data does not have a format. Oracle stores DATE data types as 7-bytes (and similar for TIMESTAMP data types) and it is not until it is passed to a client program (i.e. SQL/Plus, SQL Developer, Toad, Java, Python, etc) and that client program formats it according to whatever rules it has that the date gets a format.
You can give DATE and TIMESTAMP columns a format using TO_CHAR( datevalue, format_model, [nls_settings] ) like this:
SELECT TO_CHAR( CALCTM, 'HH24:MI:SS' )
FROM REC_ALL_TRADES
If it is stored as a string then convert it to a DATE and then back to a string:
SELECT TO_CHAR(
TO_DATE( CALCTM, 'HH12:MI:SS AM' ),
'HH24:MI:SS'
)
FROM REC_ALL_TRADES
If you have mixed valid/invalid formatted strings then use a CASE statement to differentiate:
SELECT CASE
WHEN SUBSTR( TRIM( UPPER( CALCTM ) ), -1 ) IN ( 'AM', 'PM' )
THEN TO_CHAR(
TO_DATE( CALCTM, 'HH12:MI:SS AM' ),
'HH24:MI:SS'
)
ELSE CALCTM
END
FROM REC_ALL_TRADES

Get date of the previous day in Oracle

I need to bring the day immediately preceding date in Oracle using a truncate but not how. He was using the following line but bring me some records for the current day of execution and should not be. Neceisto only the previous day; investigation found the truncate with dates in Oracle but not how to use it.
and fnxs.FECHA_INGRESO BETWEEN (TO_CHAR (SYSDATE-1, 'DD-MON-YY')) AND (TO_CHAR (SYSDATE, 'DD-MON-YY'));
I appreciate your help
Using BETWEEN with dates in Oracle is generally a bad idea. I see it all the time, and most of the time people get it wrong (like in the accepted answer above). Even when they fully understand that the two dates are included, they still make logical errors because they forget about timestamps.
The OP is asking for yesterday dates. The following sql shows that today falls within "BETWEEN TRUNC( SYSDATE ) - 1 AND TRUNC( SYSDATE )"
with adate as (
select trunc(sysdate) today from dual
) select today from adate where today between trunc(sysdate) -1
and trunc(sysdate);
16-Apr-15 00:00:00
[returns the record for today]
I find it easier to be correct with dates when you're more explicit about the end points:
SELECT * from your_table
WHERE fnxs.FECHA_INGRESO >= TRUMC(SYSDATE) - 1
AND fnxs.FECHA_INGRESO < TRUNC(SYSDATE);
Upon looking closer, the OP's date-like column might be a VARCHAR2 (could still be a date that was implicitly cast in the comparison he gave). If it is a VARCHAR, then it needs to be converted first (using an appropriate format string):
SELECT * FROM your_table
WHERE TO_DATE(fnxs.FECHA_INGRESO, 'DD-MON-YY') >= TRUMC(SYSDATE) - 1
AND TO_DATE(fnxs.FECHA_INGRESO, 'DD-MON-YY') < TRUNC(SYSDATE);
Assuming your column is of type DATE
SELECT *
FROM TABLE_NAME
WHERE FECHA_INGRESO BETWEEN TRUNC( SYSDATE ) - 1
AND TRUNC( SYSDATE );
If it is a character string then:
SELECT *
FROM TABLE_NAME
WHERE TO_DATE( FECHA_INGRESO, 'DD-MON-YY' )
BETWEEN TRUNC( SYSDATE ) - 1
AND TRUNC( SYSDATE );

Resources