SQL & PL/SQL :: Finding MAX And MIN Value From Varchar Datatype Field
Mar 13, 2013
How to get max and min value from a varchar2 datatype column?
CREATE TABLE TEST
(
WEIGHT VARCHAR2(20)
);
INSERT INTO TEST VALUES('100');
INSERT INTO TEST VALUES('120');
INSERT INTO TEST VALUES('113');
INSERT INTO TEST VALUES('145');
INSERT INTO TEST VALUES('204');
INSERT INTO TEST VALUES('130');
I've to find the max and min weight from this data.
The below table and functions are examples, but issue is like this.
i have one issue while forming below sql , Application will form this query at run time wih required inputs it is failing because the application will not support the clob datatype.
Table : T_E (similar to EMP table structure)
This table has data similar to emp but bulk data around 10k records.
Query formed
select empno,ename, get_employees(deptno) from t_e;
This sql query this is failing when function return varchar2 string more than 4000 size.Because in sql query size should not exceed 4000 for varchar2 data type , function return size can be upto 20000
Function which CREATE OR REPLACE FUNCTION get_employees (p_deptno in emp.deptno%TYPE) RETURN varchar2 IS l_text varchar2(32767) := NULL; BEGIN FOR cur_rec IN (SELECT ename FROM t_e WHERE deptno = p_deptno) LOOP l_text := l_text || ',' || cur_rec.ename; END LOOP; RETURN LTRIM(l_text, ','); END;
I can try this with clob but application doesn't support that..so i tried with using clob in above function , after that while returning to application i want to conver the clob to char and return the entire string
select empno,ename, to_char(substring(get_employees(deptno),1,4000)) -- first 4000 characters , to_char(substring(get_employees(deptno),4000,8000)) -- next 4000 characters from t_e;
But this is failing how to return the entire string in above sql
I want to find the hours and minutes between two char field data type.Example I have two char columns one is "start_time" and another one is "end_time".The start time and end time is the machine reading of CNC MACHINE in manufacturing.I want to develope the package to capture the actual machine running time.But I have the start and end time reading in character field.The machine reading format is hour:minutes:seconds only.It will looks like 1234:45:23(the machine ran 1234 hours and 45 minutes and 23 seconds).See the below table to understand my requirements.
start_time end_time result in hours & minutes ---------- -------- ------------------------- 345 347 2 hrs 347 350 3 hrs 350 357.20 7 hrs and 20 minutes
If I subtract end_time - start_time I will get the result in char type only not in hours and minutes format.Another example
start_time end_time result in hours & minutes ---------- -------- ------------------------- 357.21 360.40 3.19(If subtract end_time - start_time)
I would need to convert the column datatype from BLOB to CLOB. currently in the table, the BLOB column has the data. the requirement is to convert this column from BLOB to CLOB datatype.
How to convert from BLOB datatype to CLOB datatype ?
display or get values stored in a long column.i tried the below code, but myvar is shown as null. am i missing something here or is there a better apporoach than this ?. There are actually 16 rows returned for this query but with empty values for 'high_value' column which is critical for me. How can i do this long to varchar convertion ?
SET SERVEROUTPUT ON DECLARE my_var long; BEGIN for x in ( SELECT high_value FROM all_tab_partitions WHERE table_name = 'SALES_DISCOUNT' AND table_owner = 'CED12' ORDER BY partition_position DESC ) [code]....
I've a large table on which I applying aggregate and group by functions to the the results.
Here are two of the columns in my table:
Name ==== Score John ==== 200* Zohaib ==== 299 Ali ==== 0* John ==== 200 Maria ==== 150* Ali ==== 0 Maria ==== absent John ==== absent
Here astrick (*) means with distinction....
The "score" column is a varchar column I want to run a query on this table to show the highest score for each student and the output should be like this:
Name ==== Score Zohaib ==== 299 John ==== 200* Maria ==== 150* Ali ==== 0*
Important note:
1. if there is a tie between two highest scores like for a student, incase of john 200 was made twice, but the score with asterick (*) will be the "maximum" becuase it is with distinction so the output should also show the the highest score with asterick.
2. the output should show the the 2nd column (score) in desc order of highest score by each student...again incase of a tie, the one with astrick will come first in the result..
I know with just mere numbers, that is pretty easy but in this case it is a varchar column and also i need the astrick along with the highest score.
I want the simplest and shortest query if possible to achieve this result
I hope I've been able to clearly explain my requirment. I am using 10G.
I have a varchar field in a table. When I store the following string, some undefined character is storing.
String to be stored: Test String String actually stored: Test ¿ string
Please note that here '' is not Hyphen. It is a special character that msword has. when you copy this and paste in a word document you find the difference.
I have listed the character set for CHAR and NCHAR from NLS_DATABASE_PARAMETERS
I see you can build an address object consisting of street name and number, city and so on, and use this in CREATE TABLE, so all tables use the same fields for addresses. And moreover you can add check functions to prevent street numbers to be negative for instance in the constructor. You can even use inheritance. I like that.
However, all teaching on this subject I found on the Internet starts with structs (points consisting of x and y, persons consisting of name and address, etc.). Then they go to inheritance and show how to build an employee who is just a person plus an employee number. They never start with simple types.
What I would like to do would be rather to start with basic types such as number or varchar2 and inherit from them. An EAN is a VARCHAR2 holding only digits where the last digit is a check sum that must be matched. An item number may be a VARCHAR2 matching 'NNN-AAAA'. A simple string may be a VARCHAR2 where no functional characters such as carriage return, etc. occur.
I would then write my create table statement thus:
CREATE TABLE item (itemno T_ITEMNO, ean T_EAN, itemname T_SIMPLESTRING); and inserts like INSERT INTO item values('123--BCD', '1234567890abc', 'toy') would fail with exceptions like 'item numbers must be formatted NNN-AAAA' or '1234567890abc is not a valid EAN'.
However this seems not possible:
create or replace type T_EAN under varchar2 ( constructor function T_EAN(p_string in varchar2) return self as result );
fails with "PLS-00580: supertype must be an object type".
Rather than creating completely new types, I would start with extending existing types, but this seems not possible. Is that really impossible or am I using the wrong approach?
select to_clob(1) from dual union select wm_concat(sysdate) from dual;
wm_concat returns a clob. To make both queries in the union have the same type columns i convert the column in the first query to a clob but Oracle gives me an [1]: ORA-00932: inconsistent datatypes: expected - got CLOB error even though both are returning a clob value.
Each of the queries work individually and both return a clob value.
Why it's not excluding '0-5' and '25-30', how I should write code to exclude this and Is there is any function in oracle to check for numeric in column and print.
Have table with two columns with datatypes as number and varchar and the values in A column like 1,2,3 AND B column values like a,b,c. Now need to display data in a single column as 1,a,2,b,3,c.
(both these fields a_std and a_time are coming as varchar from the parent table in a cursor.(basically they are time period and actual arrival time respectively)
i was juggling with the attempt to make varchar to timestamp or date..but caught with Round up /Round down)
Formula ->
A = Round down [A_TIME - A_STD] B = Round up [A_TIME) - 10 minute + A_STD]
where
A_TIME VARCHAR2(8) N Time (Format" HH:MM AM/PM") eg "3:50 PM" A_STD VARCHAR2(5) N Standard time (Format" HH:MM") eg "1:00"
Allowed values for A & B after round up/down = multiple of 10 ( 11:00,11:10,11:20 etc.)
I have a table where i need to update one field values based on another field of the same table , simply as it is.I have done this using one select all check box , on clicking that all check boxes of item_trans table will get selected , then i will un select some of check box and then using one button, i will update the value of the fields which are checked only.
I have put the sample code but when i am updating its taking long time and hanging.I am also attaching the form based on the test case provided.
insert into item_trans(TRANS_ITEM,TRANS_QTY,TRANS_ACT_QTY) VALUES ('TREE1',40,NULL); insert into item_trans(TRANS_ITEM,TRANS_QTY,TRANS_ACT_QTY) VALUES ('TREE2',20,NULL); insert into item_trans(TRANS_ITEM,TRANS_QTY,TRANS_ACT_QTY) VALUES ('TREE3',20,NULL);
--i want to set the value of trans_Act_qty as trans_qty
--i create one dummy or test block to keep the select all check box. for that table test script is
CREATE TABLE TEST ( C VARCHAR2(2000 BYTE), B NUMBER, A NUMBER );
insert into test (C,B,A) values ('A',1,1);
--code written in select all check box which is created on test.block.
BEGIN GO_BLOCK('item_trans'); FIRST_RECORD; LOOP :M_END_YN := :M_END_ALL; [code].......
--code written in M_END_YN ( actual check boxes where i will uncheck).
IF :M_END_YN = 'N' THEN :M_END_ALL := 'N'; END IF;
--code written on button to update those values which are checked.
BEGIN GO_BLOCK('item_trans'); FIRST_RECORD; LOOP IF :M_END_YN = 'Y' THEN [code]......