For getting current date i use this..
select extract(year from sysdate) from dual;
For getting the date that my database hold i use this..
select extract(year from startdate) from staff;
But now i am trying to update a field call serviceYears inside staff, by using
current year - year of start date
to get the amount of years the staff have committed to work. how do i achieve it..
i am using oracle sql
Thanks!
I tried to use
SQL> select dual.sysdate-staff.startdatefrom dual,staff;
select dual.sysdate-staff.startdatefrom from dual,staff
*
ERROR at line 1:
ORA-01747: invalid user.table.column, table.column, or column specification
I also tried
select SYSDATE-to_date('01-jan-2007','dd-mon-yyyy') from dual;
But it return me
SYSDATE-TO_DATE('01-JAN-2007','DD-MON-YYYY')
--------------------------------------------
2136.93719
How do i just get the year?
You can do this
UPDATE STAFF
SET serviceYear = ROUND((sysdate - startDate)/365)
Ex:
select ROUND((sysdate - to_date('01-JAN-2007','DD-MON-YYYY'))/365)
from dual; -- returns 6
select ROUND((sysdate - to_date('01-JAN-2005','DD-MON-YYYY'))/365,2)
from dual; -- returns 7.85
Updated:
SELECT
FLOOR((SYSDATE - TO_DATE('01-JAN-2005','DD-MON-YYYY'))/365) YEARDOWN,
CEIL((SYSDATE - TO_DATE('01-JAN-2005','DD-MON-YYYY'))/365) YearUP
FROM DUAL;
you do not need dual.sysdate - you can just reference sysdate.
select sysdate-staff.startdatefrom from staff
I think you might be better served by a combination of FLOOR and MONTHS_BETWEEN.
SQL> CREATE TABLE t (start_date DATE);
Table created.
SQL> INSERT INTO t VALUES (TO_DATE('20070930','YYYYMMDD'));
1 row created.
SQL> SELECT FLOOR(MONTHS_BETWEEN(TRUNC(SYSDATE), start_date)/12) yrs_between_start_dt_and_today FROM t
2 ;
YRS_BETWEEN_START_DT_AND_TODAY
------------------------------
5
SQL>
You can adjust your rounding as needed. Moreover, this solution plays better with leap years and such, as compared to dividing by a hard-coded 365.
If you can measure it in months, try the months_between function:
http://www.techonthenet.com/oracle/functions/months_between.php
http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions094.htm#i78039
Could take that and divide by 12 to get the year.
SELECT extract(year from sysdate)-extract(year from TDATE)
FROM R_M
CONNECT BY LEVEL <=1
Related
My output without where clause ( sample )
ID
NAMEN
NAME
BEGINN
ENDE
108129
Kürbis, Gartenkürbis, Feldkürbis
Blüten
Juli
Juli
126611
Nussbaum, Walnuss, Christnuss
Schalen
August
September
126611
Nussbaum, Walnuss, Christnuss
Früchte
Oktober
Oktober
126611
Nussbaum, Walnuss, Christnuss
Blätter
Juni
Juni
92542
Beifuß Stabkraut, Besenkraut
Blätter
Juni
Juni
92542
Beifuß Stabkraut, Besenkraut
Wurzeln
September
November
Hi i try to select entries between beginn (e.g Juli) and ende (e.g. Dezember) (varchar2).
select * from (
select * from VIEW_USER_CON_PFLANZE_ERNTE
)
where sysdate
between to_date(beginn, 'MONTH', 'NLS_DATE_LANGUAGE=German')
and to_date(ende, 'MONTH', 'NLS_DATE_LANGUAGE=German')
but i get the error: ORA-01843: not a valid month
at the end, i want to realize this:
where sysdate
...
and last_day(to_date(ende, 'DD.MONTH', 'NLS_DATE_LANGUAGE=German'))
the select of to_date(beginn, 'MONTH', 'NLS_DATE_LANGUAGE=German')* and *to_date(ende, 'MONTH', 'NLS_DATE_LANGUAGE=German') from dual works and i get MM/DD/YYYY for both
Most likely there are invalid months names in your table. Another proof that you should never store date/time values as string. Use always proper DATE/TIMESTAMP data type.
If you use Oracle 18 or newer, then you can find invalid values like this:
SELECT *
FROM VIEW_USER_CON_PFLANZE_ERNTE
WHERE
TO_DATE(beginn DEFAULT NULL ON CONVERSION ERROR, 'MONTH', 'NLS_DATE_LANGUAGE=German') IS NULL
OR
TO_DATE(ende DEFAULT NULL ON CONVERSION ERROR, 'MONTH', 'NLS_DATE_LANGUAGE=German') IS NULL
or this one:
WITH t AS (
SELECT beginn, ende,
VALIDATE_CONVERSION(beginn AS DATE, 'MONTH', 'NLS_DATE_LANGUAGE=German') as begin_valid,
VALIDATE_CONVERSION(ende AS DATE, 'MONTH', 'NLS_DATE_LANGUAGE=German') as end_valid
FROM VIEW_USER_CON_PFLANZE_ERNTE)
SELECT *
FROM t
WHERE begin_valid= 0 or end_valid = 0
okay my solution is simple... I found some exceptions in my field values of the underlying table and removed them... it works...
learnings
just because the view works, the underlying table doesn't work, so you can't use a where between clause even if your view works
if it not work, search for exception in your fields
and don't use varchar2 for date comparison unless absolutely necessary
It is really a bad practice to store date fields as varchar2. I have seen it many times and it is always a source for problems and issues.
Looking are your data sample, it looks to me like the reason why you got the error is because you really have invalid months in your data. I am not even considering that the "between" you are applying lacks of basic logic, as between should include both respectively. The reason is the conversion from to_date from a month that gives you the first day at midnight. Perhaps you should consider using last_day as well in order to include the data of the second month used in the between condition.
Demonstration
SQL> create table t1 ( c1 varchar2(30) , c2 varchar2(30) ) ;
Table created.
SQL> insert into t1 values ( 'JANUAR' , 'MAI' ) ;
1 row created.
SQL> insert into t1 values ( 'JANUAR' , 'FEBRUAR' );
1 row created.
SQL> select * from t1 ;
C1 C2
------------------------------ ------------------------------
JANUAR MAI
JANUAR FEBRUAR
So, now we have two fields with varchar2 data type but they contain the month in German Language. Applying your filter works perfectly
SQL> select to_date(c1,'MONTH', 'NLS_DATE_LANGUAGE=German' ) as c1 ,
2 to_date(c2, 'MONTH', 'NLS_DATE_LANGUAGE=German' ) as c2,
3 sysdate-180 as date_calculation
4 from t1
5 where sysdate-180 between to_date(c1,'MONTH', 'NLS_DATE_LANGUAGE=German' )
6* and to_date(c2, 'MONTH', 'NLS_DATE_LANGUAGE=German' )
SQL> /
C1 C2 DATE_CALC
--------- --------- ---------
01-JAN-21 01-MAY-21 25-MAR-21
The only possible explanation is that you have indeed data which is not a valid month. You can use the answer provided by #Wernfried Domscheit to find out which rows don't contain valid months.
db<>fiddle
I am new to Oracle APEX, i am using Oracle APEX version 20.2, i have page item called P720_DDL_MONTH_FROM.
We are getting the value for that page item for date picker Month From is like below query.
From the list of values page attribute:
SELECT MON.NAME D, MON.MONTH R
FROM (SELECT DISTINCT(NAME) "NAME", TO_NUMBER(MONTH) "MONTH"
FROM
MONTH_YEAR) MON
ORDER BY MON.MONTH
From the Default values for pageItem:
DECLARE L_VALUE NUMBER;
BEGIN
SELECT DISTINCT(MONTH) INTO L_VALUE FROM MONTH_YEAR
WHERE UPPER(NAME) IN (SELECT TO_CHAR(ADD_MONTHS(TO_DATE(SYSDATE), -1), 'MON') FROM DUAL);
RETURN L_VALUE;
END;
I am facing an NO DATA FOUND issues while getting the default value for the above sql and plsql block, can anyone please clarify what is the issues from the above query, and let me due to some functionalities may be deprecated in apex latest version
Thanks,
If you got no_data_found, it means that there are no rows that satisfy condition(s) you wrote.
For sample table
SQL> SELECT * FROM month_year;
MONTH NAME
---------- --------------------
4 APR
6 JUN
query returns something (a value for June) (note that you don't need a subquery; directly use TO_CHAR(...)):
SQL> SELECT DISTINCT month
2 FROM month_year
3 WHERE UPPER (name) = TO_CHAR (ADD_MONTHS (TO_DATE (SYSDATE), -1), 'MON');
MONTH
----------
6
SQL>
If you got nothing, you should check contents of the month_year table and see what's going on.
Really basic question but i have zero experience with SQL. I'm using Tableau to do visualisation with data stored in my company's Oracle server, which contains multiple sheets. The primary table i am working with is named YQ005. One of the fields in the primary table I'm working with contains dates but stored as a String in YYYYMMDD format.
I have to convert this to Date format but doing it through Tableau raises the error "ORA-01843: Not a valid month". How can i do a custom SQL query to select this field, convert it to Date-time format and place this new data in a new column?
Littlefoot has a solid answer but it is definitely not for the inexperienced.
The basic function to convert the string to a date is:
select to_date(yyyymmdd, 'yyyymmdd')
If you are having problems with the month, you can just extract it out to check it:
select (case when substr(yyyymmdd, 5, 2) between '01' and '12'
then to_date(yyyymmdd, 'yyyymmdd')
end)
You can also add a check that the value is all numbers:
select (case when regexp_like(yyyymmdd, '^[0-9]{8}') and
substr(yyyymmdd, 5, 2) between '01' and '12'
then to_date(yyyymmdd, 'yyyymmdd')
end)
Validating dates in Oracle gets much more complicated if you have to validate the whole date -- each month has a different number of days and leap years further complicate matters. But months should always be between 01 and 12.
Error you got means that some values in that table - on 5th and 6th place - don't have a valid month value. For example, it might be 20191823 (there's no 18th month, is there?).
Unfortunately, that's what happens when people store dates as strings. There's no easy way out. If you want to do it with SQL only, you might fail or succeed (if you're VERY lucky). For example, have a look at this example:
SQL> desc yq005
Name Null? Type
----------------------------------------- -------- ----------------
DATUM VARCHAR2(8)
SQL> select * From yq005;
DATUM
--------
20191221
13000815
00010101
19302533 -- 25th month and 33rd day
2013Ab12 -- Ab month
2ooo0513 -- year with letters "o" instead of digits "0"
6 rows selected.
SQL>
A query whose where clause tries to identify invalid values:
SQL> alter session set nls_date_format = 'dd.mm.yyyy.';
Session altered.
SQL> select to_date(datum, 'yyyymmdd') result
2 from yq005
3 where substr(datum, 1, 4) between '0000' and '9999'
4 and substr(datum, 5, 2) between '00' and '12'
5 and substr(datum, 7, 2) between '01' and '31'
6 and regexp_like(datum, '^\d+$');
RESULT
-----------
21.12.2019.
15.08.1300.
01.01.0001.
SQL>
lines #3, 4 and 5 are trying to identify valid year/month/day. The first two are OK, more or less, but - it'll miserably fail on at least half of all months because e.g. 20191131 is "valid", but there are no 31 days in November
line #6 eliminates values that aren't all digits
Just to check that 20191131:
SQL> insert into yq005 values ('20191131');
1 row created.
SQL> select to_date(datum, 'yyyymmdd') result
2 from yq005
3 where substr(datum, 1, 4) between '0000' and '9999'
4 and substr(datum, 5, 2) between '00' and '12'
5 and substr(datum, 7, 2) between '01' and '31'
6 and regexp_like(datum, '^\d+$');
ERROR:
ORA-01839: date not valid for month specified
no rows selected
SQL>
As I said, it won't work; the same goes for other 30-days months, as well as February.
You could try to create a function which converts string to date; if it succeeds, fine. If not, skip that value:
SQL> create or replace function f_valid_date_01 (par_datum in varchar2)
2 return number
3 is
4 -- return 1 if PAR_DATUM is a valid date; return 0 if it is not
5 l_date date;
6 begin
7 -- yyyymmdd is format you expect
8 l_date := to_date(par_datum, 'yyyymmdd');
9 return 1;
10 exception
11 when others then
12 return 0;
13 end;
14 /
Function created.
SQL>
Let's use it:
SQL> select datum original_value,
2 to_char(to_date(datum, 'yyyymmdd'), 'dd.mm.yyyy') modified_value
3 from yq005
4 where f_valid_date_01 (datum) = 1;
ORIGINAL MODIFIED_V
-------- ----------
20191221 21.12.2019
13000815 15.08.1300
00010101 01.01.0001
SQL>
Just the opposite - fetch invalid dates:
SQL> select datum
2 from yq005
3 where f_valid_date_01 (datum) = 0;
DATUM
--------
19302533
2013Ab12
2ooo0513
20191131
SQL>
This is just one option you might use; there certainly are others, just Google for them. The bottom line is: always store dates into a DATE datatype column and let the database take care about (in)valid values.
[EDIT: how to populate a new column with a valid date]
If there's no date datatype column in the table, add it:
SQL> alter table yq005 add new_datum date;
Table altered.
Now run the update; mind the where clause:
SQL> update yq005 set
2 new_datum = to_date(datum, 'yyyymmdd')
3 where f_valid_date_01(datum) = 1;
3 rows updated.
SQL> select * From yq005;
DATUM NEW_DATUM
-------- -----------
20191221 21.12.2019.
13000815 15.08.1300.
00010101 01.01.0001.
19302533
2013Ab12
2ooo0513
20191131
7 rows selected.
SQL>
The best solution would be to have whoever maintains your database alter the table definition to store dates using the DATE datatype instead of some form of string.
But if you can't or don't wish to alter the Oracle schema, then I would try using the DATEPARSE() function in Tableau, as follows (assuming your date field is named XXX_DATE)
In Tableau, rename XXX_DATE to XXX_DATE_ORGINAL
Define a calculated field called XXX_DATE as DATEPARSE("YYYYMMdd", [XXX_DATE_ORIGINAL])
Hide the original field XXX_DATE_ORIGINAL
Now you can use your XXX_DATE field as a date in Tableau
The renaming and hiding of the original field is not strictly necessary. I just find it helps keep the data source understandable. For more info, see the Tableau online help for DateParse
In my application am trying to format and sort the date, i am using to_char() function to format the date to my required format, but when i sort them it is sorting it as string sorting. But i want them to be sorted as date.
I need some help to achieve both in the same query. Kindly help me on the same.
The query which i used was,
SELECT to_char( t1.your_date_column1, your_format_mask ) as alias,
FROM your_table1 t1,your_table2
ORDER BY t1.your_date_column1
It sounds like you want something like
SELECT to_char( your_date_column, your_format_mask )
FROM your_table
ORDER BY your_date_column
In the SELECT list, you want to return a character string that represents the date in your preferred format. In the ORDER BY clause, you want to order by the actual date. Using the standard EMP and DEPT tables, for example
SQL> ed
Wrote file afiedt.buf
1 select to_char( hiredate, 'DD-MM-YYYY' )
2 from emp,
3 dept
4 where emp.deptno = dept.deptno
5* order by hiredate
SQL> /
TO_CHAR(HI
----------
17-12-1980
20-02-1981
22-02-1981
02-04-1981
01-05-1981
09-06-1981
08-09-1981
28-09-1981
17-11-1981
03-12-1981
03-12-1981
23-01-1982
19-04-1987
23-05-1987
14 rows selected.
If you add a DISTINCT, the problem is that Oracle doesn't know that the function you are applying (in this case TO_CHAR) provides a one-to-one mapping from the data in the table to the data in the output. For example, two different dates (October 1, 2010 10:15:15 and October 1, 2010 23:45:50) might generate the same character output, forcing Oracle to eliminate one of the two '01-10-2010' strings but the two dates would sort differently. You can rectify that problem by nesting your query and converting the string back to a date after doing the DISTINCT and before doing the ORDER BY
SQL> ed
Wrote file afiedt.buf
1 select hire_date_str
2 from (
3 select distinct to_char( hiredate, 'DD-MM-YYYY' ) hire_date_str
4 from emp,
5 dept
6 where emp.deptno = dept.deptno
7 )
8* order by to_date(hire_date_str,'DD-MM-YYYY')
SQL> /
HIRE_DATE_
----------
17-12-1980
20-02-1981
22-02-1981
02-04-1981
01-05-1981
09-06-1981
08-09-1981
28-09-1981
17-11-1981
03-12-1981
23-01-1982
19-04-1987
23-05-1987
13 rows selected.
SELECT
to_char( your_date_column, your_format_mask ) as formate_purpose,
FROM your_table
ORDER BY to_date (formate_purpose)
Try the above code
The easiest way is to retrieve the same field with the query again and doing sorting based upon that filed
In your example
SELECT
to_char( your_date_column, your_format_mask ) as formate_purpose,
your_date_column as sorting_purpose
FROM your_table
ORDER BY your_date_column
You don't say what your application is written in, but in some environments (e.g. Oracle APEX, Oracle Reports) the solution is to not use to_char in the query, but then to apply the desired formatting in the tool's "column properties" or similar.
If you let Oracle sort (recommended), just do it like described in Justin Cave's answer. If, for some reason, you do the sorting in Java, do not use to_char; get the dates as Date objects instead and use e.g. a SimpleDateFormat to do the formatting in Java (after sorting).
For sqlplus, use alter session set nls_date_format to what you want the date format to be, then just use the column name in your select statement and sort order.
I wanted to Group By and Order By the Date field but the Date field included the Time and I didn't want to include the Time in the grouping and sorting. So I converted the Date to Character and then converted the character back to Date to eliminate the Time and sort by date not by text. That grouped the data by the date and sorted by the date.
-- Projects initiated by Day.
select to_date(to_char(psd.PROJECTSTATUSDATE, 'mm/dd/yyyy'),'mm/dd/yyyy') as "Date", count(*)
from project pj, project_status_date psd
where PJ.PROJECTTOKEN = PSD.PROJECTTOKEN
and psd.PROJECTSTATUSDATE > '01-JAN-2001'
and PSD.PROJECTSTATUSCODE = 'BL'
group by to_date(to_char(psd.PROJECTSTATUSDATE, 'mm/dd/yyyy'),'mm/dd/yyyy')
order by to_date(to_char(psd.PROJECTSTATUSDATE, 'mm/dd/yyyy'),'mm/dd/yyyy')
Date Count
8/16/2013 102
8/19/2013 77
8/20/2013 257
8/21/2013 30
8/22/2013 173
8/23/2013 125
8/26/2013 41
8/27/2013 25
8/28/2013 14
i have a holiday table which contains the data are
HOLIDAYDA DESCRIPTION
--------- --------------------
19-JAN-11 to
17-JAN-11 to
10-JAN-11 new day
Now I want the first business day of the week. IE: If I pass "12-JAN-2011" as input, I want the o/p as 11-JAN-2011 as the 1st business day because 10-JAN-2011 is holiday.
here is my code :
create or replace procedure sample as
l_dStartDay date;
l_dHolidayDate date;
begin
select trunc(to_date(sysdate),'Day')
into l_dStartday
from dual;
dbms_output.put_line('first day of the week ');
dbms_output.put_line(l_dStartDay);
for i in 2..5 Loop
select holidaydate
from holiday
into l_dHolidayDate
where holidaydate = (l_dStartDay + i);
if(l_dHolidaydate is null) then
dbms_output.put_line(l_dStartDay+i);
end if;
exit;
end loop;
end;
i compiled the above program but with "Procedure created with compilation errors."
Newly Added :
Compliation errors :
LINE/COL ERROR
-------- -----------------------------------------------------------------
9/1 PL/SQL: SQL Statement ignored
9/33 PL/SQL: ORA-00933: SQL command not properly ended
Error:
BEGIN sample; END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00905: object SYSTEM.SAMPLE is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
can any one tell me the reason for the error? if possible tell me the solution?
"i compiled the above program but with
Procedure created with compilation
errors"
If you are using an IDE such as TOAD or SQL Developer it would show the compilation errors automatically. Otherwise they are accessible in SQL*Plus using this command:
SQL> show errors
There are also views such as USER_ERRORS which we can query.
The problem is most likely the SELECT statement, as the INTO clause should follow immediately after the projection:
select holidaydate
into l_dHolidayDate
from holiday
where holidaydate = l_dStartDay + i);
Mind you, this also looks wrong:
select trunc(to_date(sysdate),'Day')
SYSDATE is a DATE already, although the more recent versions of Oracle tend to be more forgiving of using TO_DATE on a DATE column. When truncating the time element from a date it is not necessary to include a format mask as this is the default behaviour:
trunc(some_date_variable)
We only need to include a mask if (say) we want the first day of the month:
trunc(some_date_variable, 'MON')
If you want to find the first day of the week, this will do it:
SQL> select
2 trunc(to_date('01-DEC-2010', 'DD-MON-YYYY'), 'D') start_of_wk
3 from dual
4 /
START_OF_
---------
29-NOV-10
SQL>
Note that the first day of the week is dependent on the territory setting. In some territories the first day of the week is a working day (for instance Monday in the UK) in others it is not (Sunday is day 1 in the US). So it may be necessary to add an offset.
Once you solve the compilation errors you'll find soem runtime errors, probably relating to unhandled NO_DATA_FOUND exceptions. This is because your lookup query won't return NULL when it doesn't find a matching record, it will fail.
This is a simple procedure. It uses a SQL solution, because SQL is the most efficient way of doing things. The inner query uses the CONNECT BY trick to generate a result set of dates. This is then reduced by the MINUS set operator, which will filter out any holidays in that week's range. Finally the outer query returns the earliest date from the query.
create or replace procedure get_first_working_day
( p_tgt_date in date )
is
l_st_day date := trunc(p_tgt_date, 'D');
l_working_day date := trunc(p_tgt_date, 'D');
begin
dbms_output.put_line('first day of week = '||l_st_day);
select min(day_of_wk)
into l_working_day
from ( select l_st_day + (level-1) as day_of_wk
from dual
connect by level <= 5
minus
select holidaydate
from hols
where holidaydate between l_st_day and l_st_day + 4 );
dbms_output.put_line('first working day of week = '||l_working_day
||'::'|| to_char(l_working_day, 'DAY'));
end get_first_working_day;
/
Given this test data (which reflects the byzantine state of British bank holidays) ...
SQL> select holidate from hols
2 order by 1
3 /
HOLIDAYDA
---------
25-DEC-10
26-DEC-10
27-DEC-10
28-DEC-10
01-JAN-11
03-JAN-11
6 rows selected.
SQL>
... here's the procedure in action:
SQL> set serveroutput on size unlimited
SQL>
SQL> exec get_first_working_day (sysdate)
first day of week = 10-JAN-11
first working day of week = 10-JAN-11::MONDAY
PL/SQL procedure successfully completed.
SQL>
SQL> exec get_first_working_day (to_date( '04-JAN-2011', 'DD-MON-YYYY'))
first day of week = 03-JAN-11
first working day of week = 04-JAN-11::TUESDAY
PL/SQL procedure successfully completed.
SQL>
SQL> exec get_first_working_day (to_date( '01-JAN-2011', 'DD-MON-YYYY'))
first day of week = 27-DEC-10
first working day of week = 29-DEC-10::WEDNESDAY
PL/SQL procedure successfully completed.
SQL>
Incidentally, this is very bad practice:
PLS-00905: object SYSTEM.SAMPLE is invalid
Don't use the built-in SYS or SYSTEM accounts for your own work. There is too great a chance of breaking something. Create a new user account instead.
I'm guessing that the line
where holidaydate = l_dStartDay + i);
is wrong as it has a ) where it's not supposed to be.
Aside from the errors already mentioned, try removing the 'EXIT' clause as this loop will iterate a fixed number of times. Also, try specifying the block name when ending the block as in the following:
LOOP
...
END LOOP;
END ObjectName;
Where ObjectName is your top level program. Here, it would be 'sample', so:
LOOP
...
END LOOP;
END sample;