Oracle one column contains the other column - oracle

ID - FILE_NAME
100 SOMETHING_100.TXT
The question is simple actually. I want to select rows that contains its own ID in another columns string
Any ideas?

INSTR might help; for example,
select *
from your_table
where instr(file_name, id) > 0;
If ID is a number (looks like it), then add TO_CHAR:
where instr(file_name, to_char(id)) > 0

Related

Trying to display top 3 amount from a table using sql query in oracle 11g..column is of varchar type

Am trying to list top 3 records from atable based on some amount stored in a column FTE_TMUSD which is of varchar datatype
below is the query i tried
SELECT *FROM
(
SELECT * FROM FSE_TM_ENTRY
ORDER BY FTE_TMUSD desc
)
WHERE rownum <= 3
ORDER BY FTE_TMUSD DESC ;
o/p i got
972,9680,963 -->FTE_TMUSD values which are not displayed in desc
I am expecting an o/p which will display the top 3 records of values
That should work; inline view is ordered by FTE_TMUSD in descending order, and you're selecting values from it.
What looks suspicious are values you specified as the result. It appears that FTE_TMUSD's datatype is VARCHAR2 (ah, yes - it is, you said so). It means that values are sorted as strings, not numbers - and it seems that you expect numbers. So, apply TO_NUMBER to that column. Note that it'll fail if column contains anything but numbers (for example, if there's a value 972C).
Also, an alternative to your query might be use of analytic functions, such as row_number:
with temp as
(select f.*,
row_number() over (order by to_number(f.fte_tmusd) desc) rn
from fse_tm_entry f
)
select *
from temp
where rn <= 3;

How can i limit the output of select statement for a varchar column in Oracle?

i have a table in which i store the information (product id and description ) of all my products, description column is of type VarChar2(200). i want to format the output of this column in select statement to only result me specific part of output string. E.G Here is my simple select statement:
Select PRODUCTId, PRODUCT_DESC From ProductTable Order By PRODUCTId Desc;
this statement result me the output as:
ProductId Product_Desc
1 Oxford English-Oxford-Oxford Press-Textbook
now i want only the specific part of the output result from product_description column. i have already checked Trim() function but that did not helped me. can someone help me?
A substring function may help.
SELECT SUBSTR('ABCDEFG',3,4) "Substring"
FROM DUAL;
You can use SUBSTR() function. You can provide start and end position for the product_desc column.
The query should be like:
Select product_id,substr(product_desc,2,4) from producttable;
Here you'll get 4 chars from the second one.

Oracle sequence in a queue table

So I have a table called rentalqueue(queue_id,customer_id,movie_title,etc.) I also have a sequence on the queue_id as such:
CREATE SEQUENCE rentalqueue_seq
MINVALUE 100
MAXVALUE 300
START WITH 100
INCREMENT BY 1
NOCACHE;
Lets say I insert several movie titles into rentalqueue for a given customer from a movie table. How can I implement a trigger(assumption), so that I can designate the very last movie added in the queue? Assume I add 5 movies over lets say a couple days, each with a different timestamp.
I suggest you modify your sequence so that the maxvalue is something like 99999999. You can select the last movie added for a customer like this:
select *
from (
select *
from rentalqueue
where customer_id = :p_customer_id
order by queue_id desc)
where rownum = 1
Or, if you have a date_added field (or something similar), like this:
select *
from (
select *
from rentalqueue
where customer_id = :p_customer_id
order by date_added desc)
where rownum = 1

How to split FoxPro records?

I have 60,000 records in the dbf file in FoxPro. I want to split it into each 20,000 records (20000 * 3 = 60,000).
How can I achieve this?
I am new to FoxPro. I am using Visual FoxPro 5.0.
Thanks in advance.
You must issue a SKIP command when using the COPY command to make sure you are starting on the next record.
USE MyTable
GO TOP
COPY NEXT 20000 TO NewTable1
SKIP 1
COPY NEXT 20000 TO NewTable2
SKIP 1
COPY NEXT 20000 TO NewTable3
Todd's suggestion will work if you don't care how the records are split. If you want to divide them up based on their content, you'll want to do something like Stuart's first suggestion, though his exact answer will only work if the IDs for the records run from 1 to 60,000 in order.
What's the ultimate goal here? Why divide the table up?
Tamar
You can directly select from the first table:
SELECT * from MyBigTable INTO TABLE SmallTable1 WHERE ID < 20000
SELECT * from MyBigTable INTO TABLE SmallTable2 WHERE ID BETWEEN (20000, 39999)
SELECT * from MyBigTable INTO TABLE SmallTable3 WHERE ID > 39999
if you want more control, though, or you need to manipulate the data, you can use xbase code, something like this:
SELECT MyBigTable
scan
scatter name oRecord memo
if oRecord.Id < 20000
select SmallTable1
append blank
gather name oRecord memo
else if oRecord.Id < 40000
select SmallTable2
append blank
gather name oRecord memo
else
select SmallTable3
append blank
gather name oRecord memo
endscan
It's been a while since I used VFP and I don't have it here, so apologies for any syntax errors.
use in 0 YourTable
select YourTable
go top
copy to NewTable1 next 20000
copy to NewTable2 next 20000
copy to NewTable3 next 20000
If you wanted to split based on record numbers, try this:
SELECT * FROM table INTO TABLE tbl1 WHERE RECNO() <= 20000
SELECT * FROM table INTO TABLE tbl2 WHERE BETWEEN(RECNO(), 20001, 40000)
SELECT * FROM table INTO TABLE tbl3 WHERE RECNO() > 40000

How to work around ORA-02014: cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc

I want to lock one record in a table.
The record is specified as "the next that has ID greater than..."
CREATE TABLE test (id number);
SELECT id
FROM (SELECT id
FROM test
WHERE id > 10
ORDER BY id)
WHERE ROWNUM = 1
FOR UPDATE;
This seems intuitive and easy. But it is not. Any ideas?
P.S.
I do need the existing query to remain the same because it is a cursor and there are several places that use this cursor's %rowtype.
I think you're going to need something like:
SELECT id
FROM test
WHERE id =
(SELECT MIN(id)
FROM test
WHERE id > 10)
FOR UPDATE;

Resources