Reputation: 67
I have two columns in table both have datatype as varchar2. Now i want to get data based on input value and based on this my select query looks like this:
select fromtime, totime from temp_trd_shr where
fromtime <= '08:00' and totime >='10:00'
Fromtime: varchar2(50) totime : varchar2(50) 08:00 & 10:00 : input value (in string and formate is always fix like xx:xx) Oracle 11g
i tried with to_number but i am not able to do this.
Upvotes: 0
Views: 492
Reputation: 2182
CREATE TABLE temp_trd_shr
(
fromtime VARCHAR2(5)
, totime VARCHAR2(5)
);
INSERT INTO temp_trd_shr VALUES ('01:00', '18:00');
INSERT INTO temp_trd_shr VALUES ('02:00', '17:00');
INSERT INTO temp_trd_shr VALUES ('03:00', '16:00');
INSERT INTO temp_trd_shr VALUES ('04:00', '15:00');
INSERT INTO temp_trd_shr VALUES ('05:00', '14:00');
INSERT INTO temp_trd_shr VALUES ('06:00', '13:00');
INSERT INTO temp_trd_shr VALUES ('08:30', '09:30');
INSERT INTO temp_trd_shr VALUES ('08:45', '09:45');
SELECT COUNT(*)
FROM temp_trd_shr;
-- 8
SELECT *
FROM temp_trd_shr
WHERE TO_NUMBER(REPLACE(fromtime, ':')) <= TO_NUMBER(REPLACE('08:00', ':'))
AND TO_NUMBER(REPLACE(totime, ':')) >= TO_NUMBER(REPLACE('10:00', ':'))
ORDER BY
1
;
/*
01:00 18:00
02:00 17:00
03:00 16:00
04:00 15:00
05:00 14:00
06:00 13:00
*/
Upvotes: 3