INSERT
INTO TABLE_NAME
(
SEQ_ID,
FIRST_ENTRY_DATE,
UPDATE_DATE,
TRIGGER_DATE,
EMPLOYEE_NAME
)
VALUES
(
'I001247476',
NVL('27/12/2014',SYSDATE),
SYSDATE,
TO_DATE('26/10/2014', 'MM/DD/YYYY HH24:MI:SS'),
'JHON'
);
Date is coming in DD/MM/YYYY format but in query declared as MM/DD/YYYY.
How to handle this thing explicitly in query we cannot directly change the format or date in the above query.
Any function available in oracle to handle this kind of scenarios?
Just swap the format mask around:
INSERT INTO TABLE_NAME (
SEQ_ID,
FIRST_ENTRY_DATE,
UPDATE_DATE,
TRIGGER_DATE,
EMPLOYEE_NAME
) VALUES (
'I001247476',
NVL( TO_DATE( :first_date, 'DD/MM/YYYY' ), SYSDATE ),
SYSDATE,
TO_DATE( :trigger_date, 'DD/MM/YYYY' ),
'JHON'
);
Related
have some PLSQL code that generates a list of dates from a range, which seems to be working fine.
As part of a larger project I want to generate a procedure that will create a list of absences for each employee.
My first step is to use the MINUS command to remove all the holidays, which fall into the range of dates. Is there an easy way of doing this instead of comparing each holiday one at a time (there maybe several in the table) against the GENERATED dates.
If possible, I would prefer breaking all these tasks into small procedures or functions for easy debugging and legibility.
If there is an easier way to do this I am open to all suggestions. Thanks in advance for your help, expertise and patience.
ALTER SESSION SET
NLS_DATE_FORMAT = 'MMDDYYYY HH24:MI:SS';
create table holidays(
holiday_date DATE,
holiday_name VARCHAR2(20)
);
INSERT into holidays
(holiday_date,
holiday_name)
VALUES
(
TO_DATE('2021/07/21 00:00:00', 'yyyy/mm/dd hh24:mi:ss'), 'July 21 2021');
CREATE OR REPLACE PROCEDURE generate_dates
(
p_start_date IN DATE,
p_end_date IN DATE
)
AS
l_day DATE := p_start_date;
BEGIN
WHILE l_day <= p_end_date
LOOP
DBMS_OUTPUT.PUT_LINE(l_day);
l_day := l_day + 1;
END LOOP;
END generate_dates;
EXEC generate_dates(TRUNC(SYSDATE),TRUNC(SYSDATE+10));
Create table employees(
employee_id NUMBER(6),
first_name VARCHAR2(20),
last_name VARCHAR2(20),
card_num VARCHAR2(10),
work_days VARCHAR2(7)
);
ALTER TABLE employees
ADD ( CONSTRAINT employees_pk
PRIMARY KEY (employee_id));
INSERT INTO employees
(
EMPLOYEE_ID,
first_name,
last_name,
card_num,
work_days
)
WITH names AS (
SELECT 1, 'Jane', 'Doe', 'F123456', 'NYYYYYN' FROM dual UNION ALL
SELECT 2, 'Madison', 'Smith', 'R33432','NYYYYYN'
FROM dual UNION ALL
SELECT 3, 'Justin', 'Case', 'C765341','NYYYYYN'
FROM dual UNION ALL
SELECT 4, 'Mike', 'Jones', 'D564311','NYYYYYN' FROM dual
) SELECT * FROM names
-- check to see if working for that day. Byte=Y for Yes
SELECT SUBSTR( work_days, to_char(TRUNC(SYSDATE), 'D'),1) Work_Day
FROM employees
create table timeoff(
seq_num integer GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
employee_id NUMBER(6),
timeoff_date DATE,
timeoff_type VARCHAR2(1),
constraint timeoff_chk check (timeoff_date=trunc(timeoff_date, 'dd')),
constraint timeoff_pk primary key (employee_id, timeoff_date)
);
INSERT INTO timeoff (EMPLOYEE_ID,TIMEOFF_DATE,TIMEOFF_TYPE
)
WITH dts AS (
SELECT 1, to_date('20210726 00:00:00','YYYYMMDD HH24:MI:SS'),'V' FROM dual UNION ALL
SELECT 2, to_date('20210726 00:00:00','YYYYMMDD HH24:MI:SS'),'V' FROM dual UNION ALL
SELECT 2, to_date('20210727 00:00:00','YYYYMMDD HH24:MI:SS'),'V' FROM dual )
SELECT * FROM dts
CREATE TABLE emp_attendance(
seq_num integer GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
employee_id NUMBER(6),
start_date DATE,
end_date DATE,
week_number NUMBER(2),
create_date DATE DEFAULT SYSDATE
);
create table absences(
seq_num integer GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
employee_id NUMBER(6),
absent_date DATE,
constraint absence_chk check (absent_date=trunc(absent_date, 'dd')),
constraint absence_pk primary key (employee_id, absent_date)
);
INSERT INTO emp_attendance ( EMPLOYEE_ID, START_DATE,END_DATE,WEEK_NUMBER)
WITH dts AS (
SELECT 1, to_date('20210728 13:10:00','YYYYMMDD HH24:MI:SS'),
to_date('20210728 23:15:00','YYYYMMDD HH24:MI:SS'), 30 FROM dual UNION ALL
SELECT 2, to_date('20210728 12:10:10','YYYYMMDD HH24:MI:SS'),
to_date('20210728 20:15:01','YYYYMMDD HH24:MI:SS'), 30 FROM dual)
SELECT * FROM dts
CREATE OR REPLACE TYPE obj_date IS OBJECT (
date_val DATE
);
CREATE OR REPLACE TYPE nt_date IS TABLE OF obj_date;
CREATE OR REPLACE FUNCTION generate_dates(
p_from IN DATE
,p_to IN DATE)
RETURN nt_date PIPELINED
IS
-- normalize inputs to be as-of midnight
v_from DATE :=
TRUNC(NVL(p_from, SYSDATE));
v_to DATE := TRUNC(NVL(p_to, SYSDATE));
BEGIN
LOOP
EXIT WHEN v_from > v_to;
PIPE ROW (obj_date(v_from));
v_from := v_from + 1; -- next. calendar day
END LOOP;
RETURN;
END generate_dates;
INSERT INTO absences
(employee_id, absent_date)
SELECT e.employee_id,
c.date_val
FROM employees e
INNER JOIN table(generate_dates(date '2021-07-20', DATE '2021-07-31')) c
PARTITION BY ( e.employee_id )
ON (SUBSTR(e.work_days,
TRUNC(c.date_val) -
TRUNC(c.date_val, 'IW') + 1, 1) = 'Y')
WHERE NOT EXISTS (
SELECT 1
FROM holidays h
WHERE c.date_val = h.holiday_date
)
AND NOT EXISTS(
SELECT 1
FROM timeoff t
WHERE e.employee_id = t.employee_id
AND t.timeoff_date = c.date_val
)
AND NOT EXISTS(
SELECT 1
FROM emp_attendance ea
WHERE e.employee_id = ea.employee_id
AND TRUNC(ea.start_date) = c.date_val
)
ORDER BY
e.employee_id,
c.date_val
;
Don't use lots of procedures and/or a functions; just use a single query:
SELECT e.employee_id,
c.day
FROM employees e
INNER JOIN (
WITH calendar ( start_date, end_date ) AS (
SELECT DATE '2021-07-01', DATE '2021-07-30' FROM DUAL
UNION ALL
SELECT start_date + 1, end_date
FROM calendar
WHERE start_date + 1 <= end_date
)
SELECT start_date AS day
FROM calendar
) c
PARTITION BY ( e.employee_id )
ON (SUBSTR(e.work_days, TRUNC(c.day) - TRUNC(c.day, 'IW') + 1, 1) = 'Y')
WHERE NOT EXISTS (
SELECT 1
FROM holidays h
WHERE c.day = h.holiday_date
)
AND NOT EXISTS(
SELECT 1
FROM timeoff t
WHERE e.employee_id = t.employee_id
AND t.timeoff_date = c.day
)
ORDER BY
e.employee_id,
c.day
Notes:
This assumes that your work_days column aligns with the ISO week; if it does not then you will need to adjust the substring.
Do not use TO_CHAR(date_value, 'D') as users will get different results depending on their NLS_TERRITORY session setting.
db<>fiddle here
My query(when fired by application) fails to execute with this error in spite of using the TO_TIMESTAMP function.
INSERT INTO MY_TABLE_NAME (
UPDATED_DATE,
CREATED_DATE,
TEST_SUBJECT,
THIRD_DATE
) VALUES (
TO_TIMESTAMP('2018-05-31 14:45:32.000', 'YYYY-MM-DD HH24:MI:SSxFF'),
TO_TIMESTAMP('2018-05-31 14:45:32.000', 'YYYY-MM-DD HH24:MI:SSxFF'),
'test',
TO_TIMESTAMP('2018-06-09 14:45:00.000', 'YYYY-MM-DD HH24:MI:SSxFF')
)
Here's the error message -
{FAILED after 2 ms}
java.sql.SQLDataException: ORA-01830: date format picture ends before converting entire input string
This error is thrown only on one environment, but works fine on other environments.
Manually executing the query works fine too.
The session timestamp format on all the environments is the same (updated by trigger upon login).
[TL;DR] Use Timestamp literals to avoid this whole issue:
INSERT INTO MY_TABLE_NAME (
UPDATED_DATE,
CREATED_DATE,
TEST_SUBJECT,
THIRD_DATE
) VALUES (
TIMESTAMP '2018-05-31 14:45:32.000',
TIMESTAMP '2018-05-31 14:45:32.000',
'test',
TIMESTAMP '2018-06-09 14:45:00.000'
);
If you cannot then check the NLS_NUMERIC_CHARACTERS database/session parameter. If a decimal point is not . then the x format model will not match a . but will match whatever character the database/session is using and the string will not be matched.
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE MY_TABLE_NAME (
UPDATED_DATE TIMESTAMP,
CREATED_DATE TIMESTAMP,
TEST_SUBJECT VARCHAR2(20),
THIRD_DATE TIMESTAMP
);
Query 1:
-- Set decimal separator to "." and thousands separator to ","
ALTER SESSION SET NLS_NUMERIC_CHARACTERS = '.,'
INSERT INTO MY_TABLE_NAME (
UPDATED_DATE,
CREATED_DATE,
TEST_SUBJECT,
THIRD_DATE
) VALUES (
TO_TIMESTAMP('2018-05-31 14:45:32.000', 'YYYY-MM-DD HH24:MI:SSxFF'),
TO_TIMESTAMP('2018-05-31 14:45:32.000', 'YYYY-MM-DD HH24:MI:SSxFF'),
'test',
TO_TIMESTAMP('2018-06-09 14:45:00.000', 'YYYY-MM-DD HH24:MI:SSxFF')
)
SELECT * FROM MY_TABLE_NAME
Results:
| UPDATED_DATE | CREATED_DATE | TEST_SUBJECT | THIRD_DATE |
|-----------------------|-----------------------|--------------|-----------------------|
| 2018-05-31 14:45:32.0 | 2018-05-31 14:45:32.0 | test | 2018-06-09 14:45:00.0 |
Query 2:
-- Set decimal separator to "," and thousands separator to " "
ALTER SESSION SET NLS_NUMERIC_CHARACTERS = ', '
INSERT INTO MY_TABLE_NAME (
UPDATED_DATE,
CREATED_DATE,
TEST_SUBJECT,
THIRD_DATE
) VALUES (
TO_TIMESTAMP('2018-05-31 14:45:32.000', 'YYYY-MM-DD HH24:MI:SSxFF'),
TO_TIMESTAMP('2018-05-31 14:45:32.000', 'YYYY-MM-DD HH24:MI:SSxFF'),
'test',
TO_TIMESTAMP('2018-06-09 14:45:00.000', 'YYYY-MM-DD HH24:MI:SSxFF')
)
Results:
ORA-01830: date format picture ends before converting entire input string
Another solution is to use 'YYYY-MM-DD HH24:MI:SS.FF' as the format model rather than relying on the x format model to always be consistent across instances/sessions.
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.
I am trying to figure out how to create an SQL query that will check for (:FROM_DATE) and (:TO_DATE) parameters and if NULL to put the past month dates in for the two values, and if not NULL to accept whatever values are entered in the parameters.
For example:
if the user enters (01-JAN-17) as FROM_DATE, and (31-JAN-17) as TO_DATE, I want the query to not automatically pass any values for the TO_DATE and FROM_DATE.
if the user does not enter any values for TO_DATE and FROM_DATE or there are NULL values passed in, I want the query to automatically enter the the past months values (i.e., if query is run July 1st 2017, the FROM_DATE would be 01-JUN-17 and the TO_DATE would be 30-JUN-17).
I was hinted to use a coalesce statement to handle multiple values and NULLS (i.e., AND ( (coalesce(null, :P_ORG) is null) or (ORG.ORGANIZATION_ID in :P_ORG)))???
Any help would be greatly appreciated.
Something like:
SELECT *
FROM your_table
WHERE your_date_column BETWEEN TO_DATE( :from_date, 'DD-MON-YYYY' )
AND TO_DATE( :to_date, 'DD-MON-YYYY' )
OR ( ( :from_date IS NULL OR :to_date IS NULL )
AND your_date_column BETWEEN ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), -1 )
AND TRUNC( SYSDATE, 'MM' ) - 1
);
If either (or both) :from_date or :to_date is NULL then the dates will be compared to the previous month.
If your table has dates where the time component is not always set to midnight then you will need to use:
SELECT *
FROM your_table
WHERE your_date_column BETWEEN TO_DATE( :from_date, 'DD-MON-YYYY' )
AND TO_DATE( :to_date, 'DD-MON-YYYY' )
OR ( ( :from_date IS NULL OR :to_date IS NULL )
AND your_date_column >= ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), -1 )
AND your_date_column < TRUNC( SYSDATE, 'MM' )
);
Proof of concept: consider the following query, where we have dates and values, and we want to sum the values for the dates that fall between :from_date and :to_date. If either of them is null, the query will use the first day of the prior month for from_date and the last day of the prior month for to_date. Note that this will cause problems if one date is given an actual value and the other is left null - you didn't explain how you would want that handled. But that's a different issue.
I use SQL developer, and in it I don't know how to pass in dates; I show passing in strings, and converting them to dates.
with
test_data ( dt, val ) as (
select date '2017-05-29', 200 from dual union all
select date '2017-06-13', 150 from dual union all
select date '2017-06-18', 500 from dual
)
select sum(val) as sum_val
from test_data
where dt between coalesce(to_date(:from_date, 'yyyy-mm-dd'),
add_months(trunc(sysdate, 'mm'), -1))
and coalesce(to_date(:to_date , 'yyyy-mm-dd'), trunc(sysdate, 'mm') - 1)
;
Yes, you can use COALESCE (or Oracle's NVL). When a parameter is null, replace it with the default date.
select *
from mytable
where mydate >= coalesce(:from_date, trunc(sysdate - interval '1' month), 'month')
and mydate <= coalesce(:to_date, last_day(sysdate - interval '1' month));
I have a table called PRTIME with a date field called TR_DATE. Based on the SYSDATE, I need to query the PRTIME table for the month prior to SYSDATE.
In SQL Server I would do the following:
select * from PRTIME
WHERE datepart(month,TR_DATE) = datepart(month,dateadd(month,-1,current_timestamp))
and datepart(year,TR_DATE) = datepart(year,dateadd(month,-1,current_timestamp))
How would I do this in Oracle?
I'd just do
WHERE tr_date >= trunc( add_months( sysdate, -1 ), 'MM' )
AND tr_date < trunc( sysdate, 'MM' )