I am trying following query to insert data from a file. How do I calculate TOTAL_HOURS from in_date and out_date? I know we can not select them as "as in_date" and use it later. It was for illustration purpose only.
The script is reading data from a file and it is not stored in database yet.
insert into Table_A (
Id,
IN_DATE,
OUT_DATE,
TOTAL_HOURS,
)
values (
1,
'2021-10-04 07:00:00' as in_date,
'2021-10-04 07:00:00' as out_date,
DATEDIFF(second, out_date, in_date) / 3600.0
);
If you want to INSERT a static datetime using TIMESTAMPDIFF:
INSERT INTO Table_A (Id, IN_DATE, OUT_DATE, TOTAL_HOURS)
SELECT 1, '2021-10-04 07:00:00', '2021-10-04 08:00:00',
TIMESTAMPDIFF(HOUR, '2021-10-04 07:00:00', '2021-10-04 08:00:00')
If you want to INSERT a dynamic datetime using data from another table (i.e. Table_B to Table_A using a key):
INSERT INTO Table_A (Id, IN_DATE, OUT_DATE, TOTAL_HOURS)
SELECT Id, IN_DATE, OUT_DATE,
TIMESTAMPDIFF(HOUR, IN_DATE, OUT_DATE) FROM Table_B WHERE Id = 1
Result:
Id
IN_DATE
OUT_DATE
TOTAL_HOURS
1
2021-10-04 07:00:00
2021-10-04 08:00:00
1
Fiddle here.
To INSERT from a .csv file use the LOAD DATA INFILE command and add the TIMESTAMPDIFF calculation to the SET clause:
LOAD DATA INFILE 'c:/tmp/discounts_2.csv'
INTO TABLE Table_A
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(Id,#IN_DATE,#OUT_DATE)
SET IN_DATE=#IN_DATE,
OUT_DATE=#OUT_DATE,
TIMESTAMPDIFF(HOUR, #IN_DATE, #OUT_DATE);
Related
I have to insert the data like as below from 01-01-2018 to 31-12-2018.
insert into "schema"."tablename" (FISCAL_DATE,FISCAL_DT,REGION_CD,FISCAL_REGION_CD,FISCAL_DATE1,NEW_REGION_CD) values ('2018-01-01', '2018-01-01','EMEA','EUR','01/01/2018', 'EURO');
insert into "schema"."tablename" (FISCAL_DATE,FISCAL_DT,REGION_CD,FISCAL_REGION_CD,FISCAL_DATE1,NEW_REGION_CD) values ('2018-01-01', '2018-01-01','EMEA','EUR','01/01/2018', 'EURO');
insert into "schema"."tablename" (FISCAL_DATE,FISCAL_DT,REGION_CD,FISCAL_REGION_CD,FISCAL_DATE1,NEW_REGION_CD) values ('2018-01-01', '2018-01-01','EMEA','EUR','01/01/2018', 'EURO');
insert into "schema"."tablename" (FISCAL_DATE,FISCAL_DT,REGION_CD,FISCAL_REGION_CD,FISCAL_DATE1,NEW_REGION_CD) values ('2018-01-01', '2018-01-01','EMEA','EUR','01/01/2018', 'EURO');
...
insert into "schema"."tablename" (FISCAL_DATE,FISCAL_DT,REGION_CD,FISCAL_REGION_CD,FISCAL_DATE1,NEW_REGION_CD) values ('2018-12-31', '2018-12-31','EMEA','EUR','12/31/2018', 'EURO');
Is there any alternative SQL statements to achieve this or else need to go with manually update the dates one by one.
Please share your inputs/suggestions.
If I understood you correctly, you want to insert multiple records (day by day) with dates from 2018-01-01 to 2018-12-31.
You can achieve this using WHILE statement.
DO
BEGIN
DECLARE INPUT_DATE date := '2018-01-01';
WHILE INPUT_DATE <= '2018-12-31' DO
INSERT INTO "schema"."tablename" (FISCAL_DATE,FISCAL_DT,REGION_CD,FISCAL_REGION_CD,FISCAL_DATE1,NEW_REGION_CD) values (:INPUT_DATE, :INPUT_DATE,'EMEA','EUR',:INPUT_DATE, 'EURO');
INPUT_DATE = ADD_DAYS(:INPUT_DATE,1);
END WHILE;
END
You can also use a SQL dates table on HANA database and it is very easy to create using SQLScript.
Please check following example (please replace table name and column names according to your model)
do
begin
declare date_start date := '01.01.2018';
declare date_end date := '31.12.2018';
insert into mySampleTransactionData (tdate)
SELECT generated_period_start
FROM SERIES_GENERATE_DATE('INTERVAL 1 DAY', :date_start, add_days(:date_end,1));
end
I have successfully inserted data with the below Query.
insert into "Schema"."tablename" (select to_nvarchar(gen_date, 'YYYY-MM-DD') as fiscal_date,
to_nvarchar(gen_date, 'YYYY-MM-DD') as fiscal_dt,
'AMR' as region_cd,
'AMR' as fiscal_region_cd,
to_nvarchar(gen_date, 'MM/DD/YYYY') as fiscal_date1,
'AMR' as led_region_cd
from (
select generated_period_start as gen_date from SERIES_GENERATE_DATE ('INTERVAL 1 DAY', '2018-01-01', '2018-12-31')
));
Thank You all for your valuable suggestions!!
i want retrieve rows based on Date that within 2 dates or
it will be greater than date or less than date
where id =5
AND ( :fromDate is null or trunc ( :fromDate ) >= trun(TABLE.DEPOSIT_DATE ))
AND ( :toDate is null or trunc ( :toDate )<= trunc (TABLE.DEPOSIT_DATE ))
when i pass paramters to :fromDate and :ToDate at this format (7/25/2018 1:21:55 PM) for example
the query return 0 rows
Try using "BETWEEN" operator as below example
SELECT * FROM employee where joining_date BETWEEN '2006-02-14 04:34:33' AND '2016-02-15 04:34:12';
If you are using sql server
then first set the date format
like : - set daqteformat dmy
and date condition u can use between
like : Entrydate between '01/01/2018' and '31/01/2018'
Why you cannot use between ? check below example using plsql block statement to use bind variable the same as your example
SET serveroutput ON size 2000
/
declare
T_DATE date;
F_DATE DATE;
C_ID number(2);
begin
T_DATE := to_date('01/07/2018','dd/mm/yyyy');
F_DATE := to_date('01/06/2018','dd/mm/yyyy');
delete from ex_employee;
insert into ex_employee (id, X_NAME, T_ADDED) values (1, 'aa', sysdate);
insert into ex_employee
(id, X_NAME, T_ADDED)
values
(2, 'bb', sysdate - 1);
insert into ex_employee
(id, X_NAME, T_ADDED)
values
(3, 'cc', to_date('20/06/2018','dd/mm/yyyy'));
commit;
select id
into C_ID
from ex_employee
where T_ADDED between F_DATE and T_DATE or F_DATE is null or T_DATE is null;
dbms_output.put_line(c_id);
end;
/
PL/SQL procedure successfully completed
3
I have a VARRAYS TYPE wanted to "flatten" the structure with the TABLE expression.
The example data
CREATE OR REPLACE TYPE ARIS.NUMBER_VARRAY_5 AS VARRAY (5) OF NUMBER NOT NULL;
CREATE TABLE ARIS.TEST_2
(
DATE_START DATE,
DATE_END DATE,
O3 ARIS.NUMBER_VARRAY_5,
CATEGORY NUMBER
)
SET DEFINE OFF;
Insert into TEST_2
(DATE_START, DATE_END, O3, CATEGORY)
Values
(TO_DATE('01/01/2005 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('01/05/2005 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), NUMBER_VARRAY_5(281.25,-9999,291.5,310.5,298.75), NULL);
Insert into TEST_2
(DATE_START, DATE_END, O3, CATEGORY)
Values
(TO_DATE('01/02/2005 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('01/06/2005 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), NUMBER_VARRAY_5(-9999,291.5,310.5,298.75,300.75), NULL);
COMMIT;
using the SQL statement
select O3.* from test_2 t, table(t.o3) O3
I can get the result
COLUMN_VALUE
281.25
-9999
291.5
310.5
298.75
-9999
291.5
310.5
298.75
300.75
The question is how I get the results like
DATE_START DATE_END 03.VALUE1 03.VALUE2 03.VALUE3 03.VALUE4 03.VALUE5
01/01/2005 01/05/2005 281.25 -9999 291.5 310.5 298.75
01/02/2005 01/06/2005 -9999 291.5 310.5 298.75 300.75
Try This,
SELECT
*
FROM
(
select
t.Date_start,
t.Date_end,
row_number() OVER ( PARTITION BY DATE_START, DATE_END
ORDER BY
ROWNUM) rn,
O3.*
from
test_2 t,
table(t.o3) O3
)
PIVOT ( MAX(column_value) FOR rn in
(
1 as "03.VALUE1",
2 as "03.VALUE2",
3 as "03.VALUE3",
4 as "03.VALUE4",
5 as "03.VALUE5"
)
) ;
I have table like below
ID StartDate EndDate
1 15-MAR-2009 8-DEC-2011
2 14-JAN-2010 18-FEB-2013
I need output something like this -
ID StartDate EndDate
1 15-MAR-2009 31-DEC-2009
1 1-JAN-2010 31-DEC-2010
1 1-JAN-2011 8-Dec-2011
2 14-JAN-2010 31-DEC-2010
2 1-JAN-2011 31-DEC-2011
2 1-JAN-2012 31-DEC-2012
2 1-JAN-2013 18-FEB-2013
I have 9i oracle as database, I would perfer to do this with informatica but seems difficult to find solution there, cant create a procedure so need sql query. If anyone can provide in informatica 8.6 then that would be even great.
Edited :-
Create table test_range
(Sr_No number,
start_date date,
end_date date)
create table yr_range
(years date);
Insert statements :-
insert into test_range values (1,'15-MAR-2009','8-DEC-2011');
insert into test_range values (2,'14-JAN-2010','18-FEB-2013');
insert into yr_range values ('31-Dec-2005');
insert into yr_range values ('31-Dec-2006');
insert into yr_range values ('31-Dec-2007');
insert into yr_range values ('31-Dec-2008');
insert into yr_range values ('31-Dec-2009');
insert into yr_range values ('31-Dec-2010');
insert into yr_range values ('31-Dec-2011');
insert into yr_range values ('31-Dec-2012');
insert into yr_range values ('31-Dec-2013');
As suggested in one of the answer, I have created another table to get desired output, is it possible using one table only(i.e. test_range)? I m want exploer all the options before finalizing the one.
If you had something like a years dimension, where each row represented a year, then you would join with it based on whether the year falls within the range. This will give you the number of rows you need, and then for each row you conditionally output the start/end ranges. This should also handle cases where your range is less than a year and thus would not be split up.
y.Year is assumed to be an integer.
See working example here: http://sqlfiddle.com/#!4/d4589/13/11
Create table test_range
(Sr_No number,
StartDate date,
EndDate date);
create table yr_range
(years number);
insert into test_range values (1,'15-MAR-2009','8-DEC-2011');
insert into test_range values (2,'14-JAN-2010','18-FEB-2013');
insert into yr_range values ('2005');
insert into yr_range values ('2006');
insert into yr_range values ('2007');
insert into yr_range values ('2008');
insert into yr_range values ('2009');
insert into yr_range values ('2010');
insert into yr_range values ('2011');
insert into yr_range values ('2012');
insert into yr_range values ('2013');
select
r.Sr_No,
CASE
When to_char( r.StartDate, 'yyyy') = y.years Then r.StartDate
Else to_date( y.years || '/01/01', 'yyyy/mm/dd')
END as StartDate,
CASE
When to_char( r.EndDate , 'yyyy') = y.years Then r.EndDate
Else to_date( y.years || '/12/31', 'yyyy/mm/dd')
END as EndDate
from test_range r, yr_range y
where
to_char( r.StartDate, 'yyyy') <= y.years
AND to_char( r.EndDate, 'yyyy') >= y.years;
You could also generate your years sequence using some of these techniques:
ORACLE SQL:Get all integers between two numbers
I have col1 in myTable which is varchar, and I have to insert here timestamp eg:- 09-MAY-11 10.23.12.0000 AM.
Now please tell me:
How to insert into myTable with taking sysdate in above format...
How to retrieve data from col1 in tha same timestamp format..
INSERT:
insert into myTable (col1) VALUES (to_char(systimestamp, 'dd-mon-yyyy hh.mi.ss.ff4 AM') );
SELECT:
select to_timestamp(col1, 'dd-mon-yyyy hh.mi.ss.ff4 AM') from myTable ;
But it is much better to store the data directly as a timestamp.
Then you can compare the values or modify them directly.
create table myTable1( col1 timestamp default systimestamp);