We receive data in this format from informix tables
Run Date Run Time
2017-05-22 0
2017-05-22 100
2017-05-22 200
2017-05-22 300
2017-05-22 400
Expected Output:
2017-05-22 04:00
I want combine both to get the datetime, so that manipulations can be done. Please help.
The question doesn't identify the types of the columns for the run date and run time values. However, the plausible types for run date are DATE, DATETIME YEAR TO DAY, and CHAR(10); the plausible types for run time are INTEGER and CHAR(4). For this exercise, there's no difference between INTEGER and SMALLINT, or CHAR(n) and VARCHAR(n), and you could (but shouldn't) use a DECIMAL for the run time. A properly specified question would avoid these problems.
Fortunately, it doesn't matter much — the same basic SQL code handles all the variations above — though you might need to add more casts for some of the more outré type combinations.
This code assumes you have DBDATE="Y4MD-"
or equivalent set in the environment; you have to make changes if you both use a DATE column and a different value for $DBDATE
.
DROP TABLE IF EXISTS rd_rt_date_integer;
CREATE TABLE rd_rt_date_integer
(
run_date DATE NOT NULL,
run_time INTEGER NOT NULL
CHECK (run_time >= 0 AND run_time < 2400 AND MOD(run_time, 100) < 60),
PRIMARY KEY(run_date, run_time)
);
INSERT INTO rd_rt_date_integer VALUES('2017-05-22', 0);
INSERT INTO rd_rt_date_integer VALUES('2017-05-22', 100);
INSERT INTO rd_rt_date_integer VALUES('2017-05-22', 200);
INSERT INTO rd_rt_date_integer VALUES('2017-05-22', 300);
INSERT INTO rd_rt_date_integer VALUES('2017-05-22', 400);
SELECT run_date, run_time,
EXTEND(run_date, YEAR TO MINUTE) +
MOD(run_time, 100) UNITS MINUTE +
(run_time / 100) UNITS HOUR AS run_date_time
FROM rd_rt_date_integer;
Example output
run_date run_time run_date_time
DATE INTEGER DATETIME YEAR TO MINUTE
2017-05-22 0 2017-05-22 00:00
2017-05-22 100 2017-05-22 01:00
2017-05-22 200 2017-05-22 02:00
2017-05-22 300 2017-05-22 03:00
2017-05-22 400 2017-05-22 04:00
DROP TABLE IF EXISTS rd_rt_char_char;
CREATE TABLE rd_rt_char_char
(
run_date CHAR(10) NOT NULL
CHECK (run_date MATCHES '[0-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]'),
run_time CHAR(4) NOT NULL,
PRIMARY KEY(run_date, run_time)
);
INSERT INTO rd_rt_char_char VALUES('2017-05-22', '0');
INSERT INTO rd_rt_char_char VALUES('2017-05-22', '100');
INSERT INTO rd_rt_char_char VALUES('2017-05-22', '200');
INSERT INTO rd_rt_char_char VALUES('2017-05-22', '300');
INSERT INTO rd_rt_char_char VALUES('2017-05-22', '400');
SELECT run_date, run_time,
EXTEND(run_date::DATE, YEAR TO MINUTE) +
MOD(run_time, 100) UNITS MINUTE +
(run_time / 100) UNITS HOUR AS run_date_time
FROM rd_rt_char_char;
Example output
run_date run_time run_date_time
CHAR(10) CHAR(4) DATETIME YEAR TO MINUTE
2017-05-22 0 2017-05-22 00:00
2017-05-22 100 2017-05-22 01:00
2017-05-22 200 2017-05-22 02:00
2017-05-22 300 2017-05-22 03:00
2017-05-22 400 2017-05-22 04:00
For the most part, Informix is very good about automatically converting between types — the need to cast to DATE
here (it could be to DATETIME YEAR TO DAY
instead) is necessary because the optimizer doesn't know which of the myriad DATETIME
sub-types to use when considering run_date
as a DATETIME
. (OK: so 'myriad' is an exaggeration. There are 56 distinct DATETIME
sub-types and 7 synonyms for some of those types.)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments