In my table one of column i have a value like below
Y-1
Y-2
Y-3
Y-4
Y-5
Y-6
Y-7
Y-8
Y-9
Y-10
Y-11
Y-12
Y-13
Y-14
when i am order by this column its working fine if the row has value up to Y-9 other wise my result is wrong like below.
Y-1
Y-10
Y-11
Y-12
Y-13
Y-14
Y-2
Y-3
Y-4
Y-5
Y-6
Y-7
Y-8
Y-9
But i want the output like below
Y-1
Y-2
Y-3
Y-4
Y-5
Y-6
Y-7
Y-8
Y-9
Y-10
Y-11
Y-12
Y-13
Y-14
How to acheive the above result.i am using oracle database.Any help will be greatly appreciated!!!!!
Assuming the data is in a table t with a column col and the structure is an alphabetical string followed by dash followed by a number, and both the alphabetical and the number are always not NULL, then:
select col from t
order by substr(col, 1, instr(col, '-')), to_number(substr(col, instr(col, '-')+1))
You can use an order by manipulatinng the column content and cast to number eg:
order by substr(col1, 1,2), TO_NUMBER(sustr(col1, 3,10))
I think the good way is to get constant length field
select col from t
order by substr(col, 1, 2)|| lpad(substr(col, 3),5,'0')
it will correct work only with two nondigit simbol in begining of string up to 99999 number
Related
I have column with value (200ML) and I need to separate the (ML) from the column !
I assume you mean that you want the leading numeric portion and the trailing alpha portion of the string '200ML' to be returned as separate columns. If that's correct you can use REGEXP_SUBSTR to do this:
SELECT REGEXP_SUBSTR(TEXT_STRING, '^[0-9]+', 1, 1) AS LEAD_NUMERIC,
REGEXP_SUBSTR(TEXT_STRING, '[A-Za-z]+$', 1, 1) AS TRAILING_ALPHA
FROM TABLE_A
db<>fiddle here
I'm trying to sort on a column in oracle table. The column value is 'M013,M007,M019,YYY,M018,XXX,999'. I'm trying to sort the values with in the column before comparing it to another column which already has the data sorted. I've tried multiple hash/MD5 and few other options, but didn't help.
Any help is appreciated !!
What you have is a comma separated string of random elements. As you indicated the listagg function can sort the results. I think your best bet then is to parse the string into individual elements then let listagg rebuild the string with sorted elements. (See fiddle)
with test(str) as
( select 'M013,M007,M019,YYY,M018,XXX,999' from dual)
select listagg(estr,',') within group (order by estr)
from (select regexp_substr(str,'[^,]+', 1, level) estr
from test connect by regexp_substr(str, '[^,]+', 1, level) is not null
) ;
These are the field (crane_no) values to be sorted
QC11QC10QC9
I tried the following query:
select * from table order by crane_no DESC
but query results does not give in an order because the field is mixed with staring and number (Example:QC12).
I get following results for above query:
QC9QC11QC10
I want the results to be in order (QC9, QC10, QC11). Thanks
If the data isn't huge, I'd use a regex order by clause:
select
cran_no
from your_table
order by
regexp_substr(cran_no, '^\D*') nulls first,
to_number(regexp_substr(cran_no, '\d+'))
This looks for the numbers in the string, so rows like 'QCC20', 'DCDS90' are ordered properly; it also takes care of nulls.
One approach is to extract the numeric portion of the crane_no columns using SUBSTR(), cast to an integer, and order descending by this value.
SELECT *
FROM yourTable
ORDER BY CAST(SUBSTR(crane_no, 3) AS INT) DESC
Note that I assume in my answer that every entry in crane_no is prefixed with the fixed width QC. If not, then we would have to do more work to identify the numerical component.
select ...
order by to_number( substr( crane_no,3 )) desc
Here my question:
I have a table with some records like (name, date, type). Suppose I have three type a, b and c.Now I want to count each type as type_count with some restriction and make a division with count(a)/count(b) to make a percent result, and the restriction in a and are different, how can I deal with this? thanks! my code look like this:
SELECT name, count(a), count(a)/count(b)
from table
where ...
Is it possible to make some subquery in select? look like this
select name, count(a), count(a)/ (select count(b) from table where restriction_for_b)
from table
where retriction_for_a
If I understand your question correctly, you can replace the counts with sum(if(condition, 1, 0)). Something like this:
select
name,
sum(if(condition_for_a, 1, 0)),
sum(if(condition_for_a, 1, 0)) / sum(if(condition_for_b, 1, 0))
from table
where ...
I have a table. it has a column with datatype NUMBER(38,20). it is an id column. out application generates the id. i am trying to insert record with an id value of 105.00010. but it inserts only 105.0001. May i know the reason why it is truncating one value(0). it porperly inserts records from 105.00001 to 105.00009. while inserting 105.00010 it is truncating. Please help me.
column size is **NUMBER(38,20)**
Thanks!
See the following test case:
WITH data_values
AS (SELECT 105.0001 AS test_val FROM dual
UNION ALL
SELECT 105.00010 AS test_val FROM dual)
SELECT test_val,
TO_NUMBER(test_val, '999.99999') AS NUM,
TO_CHAR(test_val, '999.99999') AS STR
FROM data_values;
Results in:
TEST_VAL NUM STR
105.0001 105.0001 105.00010
105.0001 105.0001 105.00010
The value after the final non zero digit is irrelevent to Oracle. Both your numbers are equivalent.
The rightmost zeros after the decimal are insignificant, so the value is not truncated, it is still the same value.
If you need it to stay the same you may need to treat the value as a varchar2.