Months between sysdate to earlier date - oracle

I want to get the months between current date to earlier date.
SELECT MONTHS_BETWEEN(to_date(fld_valid_from,'yyyy-mm-dd hh24:mi:ss'),TO_DATE(sysdate, 'yyyy-mm-dd hh24:mi:ss')) num_months
FROM tbl_customer
But it's not working. I don't know if that's correct or not.

sysdate is already a date, and does not need to be converted to one using to_date(). I suspect that fld_valid_from is also a date.

What error message are you getting? Did you try comparing the two variables without the TO_DATE command?
SELECT MONTHS_BETWEEN (trunc(fld_valid_from),trunc(sysdate))
FROM tbl_customer
This would work if your column fld_valid_from is a date type. You're comparing a date with another date. You use TO_DATE to convert a string data type to a date data type.
With the TRUNC function, you remove the timestamp from the date, and get only...well, the date:
SELECT SYSDATE, TRUNC(SYSDATE) FROM DUAL;
SYSDATE TRUNC(SYSDATE)
---------------------------------------
16/07/2013 10:45:53 16/07/2013
Hope it helps.
Regards.

Related

Why am I receiving an error for TRUNC function?

select TRUNC(TO_DATE('22-AUG-03'), 'YEAR')
from dual;
ORA-01843: not a valid month
1st example -> https://www.techonthenet.com/oracle/functions/trunc_date.php
I know "trunc" function takes in a date and optional fmt parameter.
Why am I getting this error?
I don't think the problem is trunc(). I think the problem is the date format. You are safer using the date keyword and an ISO-standard formatted date:
select TRUNC(DATE '2003-08-22', 'YEAR')
from dual;
The interpretation of a date string depends on the internationalization settings for your particular environment. The above does not have that dependency.
Here is a very simple check: IN THE SAME SESSION where your SELECT fails, and WITHOUT you altering the NLS_DATE_FORMAT, see what happens if you run the simpler statement,
SELECT TO_DATE('22-AUG-03') FROM DUAL
You will get the same error - which proves conclusively that it has nothing to do with TRUNC().
To make everything work exactly as in the tutorial, issue the command
ALTER SESSION SET NLS_DATE_FORMAT = 'dd-MON-yy'
first, before everything else. Note though that formats that have the year as two digits instead of four are a very bad idea in most cases.
It seems that you want to get the year of the date you receive in parameter
If you get '22-AUG-03' as parameter, add to TO_DATE function the date format you expect and then trunc it:
SELECT TRUNC(TO_DATE('22-AUG-03', 'dd-MON-yy'),'YEAR') FROM DUAL
Use SELECT EXTRACT(YEAR FROM TO_DATE('22-AUG-03', 'dd-MON-yy')) FROM DUAL;
to extract year from date.

How to change the value of the default systimestamp in oracle

Is there a way to change the value of the default timestamp in oracle, please find the below output of the query and it is one day behind. The google is returning results only to change the format of the default systimestmap, but i need to change the value itself. Please suggest.
select systimestamp from dual
SYSTIMESTAMP
12-02-17 07:29:26.843712000 PM +05:30
Do you want to provide your own value and turn in into a timestamp like this?
SELECT TO_TIMESTAMP ('10-Sep-02 14:10:10.123000', 'DD-Mon-RR HH24:MI:SS.FF')
FROM DUAL;
Oracle documentation
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions193.htm
Related question:
Oracle: how to add minutes to a timestamp?
What about this?
SELECT SYSTIMESTAMP + INTERVAL '2' SECOND FROM dual;
With this method you are able to add or remove 'second's, 'minute's, 'hour's and so on...
Hope this helps :-)

Date_part does not works in oracle

SELECT
DATE_PART('days',DATE_TRUNC('month', pr.DateTo) '1 MONTH'::INTERVAL - DATE_TRUNC('month', pr.datefrom) as PeriodDays
FROM HT_PayReg
This works well in postgres,but in oracle it does not works,Please correct it in oracle.
TRUNC gives the date excluding the time portion.
TO_CHAR with format mask gives you different parts of a date.
For example,
to_char(sysdate, 'DD') gives today's date as 09.
to_char(sysdate, 'MON') gives current month as OCT.
to_char(sysdate, 'YYYY') gives current year as 2014.
Play around with the different formats. For a specific output, mention your desired output.
Look at datetime format models in documentation here http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements004.htm

How to convert date object to timestamp in client?

for example
select to_timestamp(sysdate) from dual
return date object, not timestamp.
I try to change
NLS_TIMESTAMP_FORMAT='ss.ff'
but select return error.
If you are starting with sysdate then as #a_horse_with_no_name says you don't need to do a conversrion; use systimestamp or current_timestamp instead. (One is the server time, one is the client time, which will be the same unless your client is in a different timezone).
More generally though you can cast between data types:
select cast(date_field as timestamp) from your_table
You won't add any precision to the value though; the date already have a time down to second precision, even if that is midnight; and your timestamp will still have the fractional seconds part as zero.
If you just want to display your DATE as a string but show the time it already has then you need to specify the output format, e.g.
select to_char(sysdate, 'YYYY-MM-DD HH24:MI:SS') from dual

Covert into date format in Oracle

I have a character field in the database where Date value is stored.
Now I need to convert this character Date field value in below format
YYYY-MM-DDTHH24:MI:SS
I am using below conversion for it, but it's not working
to_char(lastupdate,'YYYY-MM-DD"T"HH24:MI:SS')
Could anyone please help out on this?
As you are stored your date in character literal like mm-dd-yyyy, you first need to convert it to date data_type using to_date and then to character using to_char as suggested by Nicholas Krasnov. Try like this,
SELECT to_char(to_date('01-01-2014', 'dd-mm-yyyy'),'YYYY-MM-DD"T"HH24:MI:SS')
FROM <table_name>;
If you want the date as 2014-01-17 00:00:00:
select TO_CHAR(TO_DATE(last_update, 'DD/MM/YYYY'), 'YYYY-MM-DD HH24:MI:SS') from <table>;

Resources