How do I get the current year using SQL on Oracle? - sql

I need to add the current year as a variable in an SQL statement, how can I retrieve the current year using SQL?
i.e.
BETWEEN
TO_DATE('01/01/**currentYear** 00:00:00', 'DD/MM/YYYY HH24:MI:SS')
AND
TO_DATE('31/12/**currentYear** 23:59:59', 'DD/MM/YYYY HH24:MI:SS')

Using to_char:
select to_char(sysdate, 'YYYY') from dual;
In your example you can use something like:
BETWEEN trunc(sysdate, 'YEAR')
AND add_months(trunc(sysdate, 'YEAR'), 12)-1/24/60/60;
The comparison values are exactly what you request:
select trunc(sysdate, 'YEAR') begin_year
, add_months(trunc(sysdate, 'YEAR'), 12)-1/24/60/60 last_second_year
from dual;
BEGIN_YEAR LAST_SECOND_YEAR
----------- ----------------
01/01/2009 31/12/2009

Another option is:
SELECT *
FROM TABLE
WHERE EXTRACT( YEAR FROM date_field) = EXTRACT(YEAR FROM sysdate)

Use extract(datetime) function it's so easy, simple.
It returns year, month, day, minute, second
Example:
select extract(year from sysdate) from dual;

Yet another option would be:
SELECT * FROM mytable
WHERE TRUNC(mydate, 'YEAR') = TRUNC(SYSDATE, 'YEAR');

Since we are doing this one to death - you don't have to specify a year:
select * from demo
where somedate between to_date('01/01 00:00:00', 'DD/MM HH24:MI:SS')
and to_date('31/12 23:59:59', 'DD/MM HH24:MI:SS');
However the accepted answer by FerranB makes more sense if you want to specify all date values that fall within the current year.

Why not use YEAR function?
SELECT * FROM table WHERE YEAR(date_field)=YEAR(SYSDATE);

To display the current system date in oracle-sql
select sysdate from dual;

Related

Query to 'get the records from the actual month' doesn't get me the records that have a bigger day that the current one

I have the following query to get the records from a table from the current month
select *
from
myTable
where
my_date BETWEEN trunc (sysdate, 'mm') AND SYSDATE;
This query works if the records have a lower day compared to the current one
example: if today is 27/10/2016 and I have a record that have this date: 28/10/2016
The record with date 28/10/2016 is not showing
I insert the records using this format TO_DATE( '28/10/2016 18:02:44', 'dd/mm/yyyy hh24:mi:ss')
I want to show all the records from the curren month even if the day is bigger than the actual date
Either:
select *
from
myTable
where
my_date BETWEEN trunc (sysdate, 'mm') AND add_months(trunc (sysdate, 'mm'),1)- 1/(24*3600)
or
select *
from
myTable
where
trunc(my_date,'mm') = trunc (sysdate, 'mm')
The first is sargable, the second is more readable.
If you need the dates in the current month
trunc(my_date, 'mm') = trunc(sysdate, 'mm')
If you need the dates from the current month and on:
my_date >= trunc(sysdate, 'mm')

How to_date function with sysdate

select TO_CHAR(sysdate, 'YEAR')-365 FROM DUAL;
I get error
'invalide number'
See the result of TO_CHAR(sysdate-365, 'YEAR') and then think if you can cast this to a numeric value to be able to subtract 365.
'twenty fourteen' - 365
You probably want the previous year's date:
add_months(sysdate, -12)
Or if you need the year exactly 365 days before today:
extract(year from sysdate - 365)
Your code
select TO_CHAR(sysdate, 'YEAR')-365 FROM DUAL;
doesn't work because you try to make an mathematical operation on string value TO_CHAR(sysdate, 'YEAR') which is not properly.
This will work
select EXTRACT(YEAR from sysdate)-365 FROM DUAL;
result
1650
but it doesn't make sense, so maybe you need this
select EXTRACT(YEAR from sysdate)-1 FROM DUAL;
result
2014
TO_CHAR(sysdate, 'YEAR')
It converts DATE into string. For date arithmetic, do the calculation on the date itself.
For example,
SELECT TO_CHAR(SYSDATE - 365, 'YEAR') FROM DUAL;

Insert date in oracle

When i executed the below query in Oracle
select TO_CHAR((CURRENT_DATE),'DD-Mon-YYYY HH24:MI:SS') from dual;
O/P : 04-Mar-2014 14:25:14
I would like to select current date only without current time as below
select TO_CHAR(trunc(CURRENT_DATE),'DD-Mon-YYYY HH24:MI:SS') from dual;
O/P : 04-Mar-2014 00:00:00
To achieve the only way is to apply function trunc() on the query? Is there any another way?
Edit : Thanks for your ans.Can it be done without any function?(wihout using to_char or trunc)
{sorry for missing this info}
The answer is simply no, there is no function that only gets the date part of the date / time (even current_date or sysdate are functions after all).
You should always use trunc to get the current date, without the time.
It isn't necessary to do a trunc and a to_char together. Keep to_char and don't specify the time part.
This is sufficient:
To get the date as varchar:
select TO_CHAR(CURRENT_DATE,'DD-Mon-YYYY') from dual
To get the date as date, with the time part as 00:00:00:
select trunc(CURRENT_DATE) from dual
You can do this:
select TO_CHAR(CURRENT_DATE,'DD-Mon-YYYY')||' 00:00:00' from dual;
there is also EXTRACT function which can be used like that:
SELECT extract(DAY FROM sysdate)
||'-' ||
extract(MONTH FROM sysdate)
|| '-' || extract(YEAR FROM sysdate)
FROM dual;
result: 4-3-2014
Use This Query...
select (TO_CHAR(TRUNC(CURRENT_DATE),'DD-Mon-YYYY HH24:MI:SS')) from dual

Oracle Timestamp Conversion with Dates

Assuming this has a simple solution, but I can't find it.
I'm trying to do some logic on a DATE field in Oracle. My desire is to take a DATE field and subtract X hours from it.
For instance: SELECT A.MyDATE - 100 Hours from dual;
however, I need a result in a timestamp format 'YYYY-MM-DD hh:mm'.
I've tried CAST(A.MyDATE as TIMESTAMP) - NUMTODSINTERVAL(100/24,'day') however it didn't work.
I found out that the issue is that the MyDATE field when cast to a timestamp still contained some residual time elements. How can I reset these??
Thanks!
You can just do this with subtraction:
select a.MyDate - 100.0/24
To convert to varchar:
select to_char(a.MyDate - 100.0/24, 'YYYY-MM-DD')
And, if you want to get rid of that pesky time on the date:
select trunc(a.MyDate - 100.0/24) as JustTheDate
The formats and dates in my example can be changed to any other formats and dates:
SELECT To_Timestamp(To_Char(Sysdate - INTERVAL '100' HOUR, 'MM/DD/YYYY HH24:MI'), 'MM/DD/YYYY HH24:MI')
FROM dual
/
Output:
2/4/2013 10:18:00.000000000 AM
To remove time element add Trunc() to any of your dates...:
SELECT Trunc(To_Timestamp(To_Char(Sysdate - INTERVAL '100' HOUR, 'MM/DD/YYYY HH24:MI'), 'MM/DD/YYYY HH24:MI'))
FROM dual
/
Output: 2/4/2013
Conversion/Casting - when using other dates in place of sysdate then add formats as in my other examples:
SELECT CAST(SYSDATE AS TIMESTAMP) - INTERVAL '100' HOUR FROM dual
/
Output: 2/4/2013 10:26:35.000000000 AM
SELECT start_date tstamp_to_date, CAST(start_date AS timestamp) date_to_tstamp FROM
(
SELECT to_date(to_char(to_timestamp ('2013-02-07 10:07:47.000' , 'YYYY-MM-DD HH24:MI:SS.FF'),'DD-MON-YYYY HH24:MI:SS'), 'DD-MON-YYYY HH24:MI:SS') start_date
FROM dual
)
/
Output:
tstamp_to_date date_to_tstamp
-------------------------------------------------------
2/7/2013 10:07:47 AM 2/7/2013 10:07:47.000000 AM
In Oracle, a DATE always has a day and a time component. Depending on the tool you are using and your session's NLS_DATE_FORMAT, it is entirely possible that the tool may not display the time component when you look at the data. But that is simply a display question, it has no impact on the actual data.
If you want to subtract 100 hours from midnight on the day that MyDate represents
SELECT TRUNC(MyDate) - interval '100' hour
FROM dual
This will return a DATE. If you want to return a string in a particular format
SELECT TO_CHAR( TRUNC(MyDate) - interval '100' hour, 'YYYY-MM-DD hh:mi am' )
FROM dual
Note that I'm assuming that there was a typo in your question. I assume that you want to display the minutes after the hour (mi) rather than the month (mm).
I am trying to fetch the records which is older than 30 days (from Mod_date) and I am using the below query and it is returning all the data and I want only 30 days old data.
Sample :- Mod_date 03-NOV-12 12.00.00.000000000 AM
Query :-
select Mod_date from fil_cnfact where Mod_date <= sysdate -30 order by Mod_date asc ;

Oracle, Make date time's first day of its month

I have a datevariable, I would like to have convert it to first day of its monh,
Eg: 10/10/2010 -> 01/10/2010
Eg: 31/07/2010 -> 01/07/2010
According to http://psoug.org/reference/date_func.html, this should work a dandy...
SELECT TRUNC(yourDateField, 'MONTH') FROM yourTable
SQL> select to_date('31/07/2010', 'DD/MM/YYYY') from dual;
TO_DATE('
---------
31-JUL-10
SQL> select trunc(to_date('31/07/2010', 'DD/MM/YYYY'), 'MM') from dual;
TRUNC(TO_
---------
01-JUL-10
SQL>
select trunc(sysdate, 'mm') from dual;
try this one
select trunc(sysdate, 'MM')firstday , trunc(last_DAY(sysdate)) lastday from dual;
SELECT trunc(to_date('22-AUG-03'), 'MON') FROM dual;
More in the manual.
About Oracle needing a dummy FROM: Select without a FROM clause in Oracle
Here is a good example:
select trunc(to_date('15.11.2019', 'DD.MM.YYYY'), 'MONTH') from dual;