simple SQL not working on table - sql

Have this odd behavior, was wondering if anyone can help.
Table:
STATUS,TYPE,ID
passed,requirement,J1X11986
passed,requirement,J1X11986-ABC
SQL> select id from MY_TABLE;
ID
--------------------------------------------------------------------------------
J1X11986
SQL> select status from MY_TABLE where id='J1X11986';
no rows selected
SQL> select status from MY_TABLE where id='J1X11986-ABC';
RESULT
------
passed

Try with a TRIM. There may be white spaces.
If you are using SQL server use below query.
Select status
From MyTable
Where LTRIM (RTRIM (id))='J1X11986'

Related

Oracle SQL Regexp capture only the number from the string

I am trying to extract the number from the row.
i am trying this in Oracle 11G
CREATE TABLE test1
(
COL_NAME VARCHAR2(100)
);
insert into test1 values ('DWH_SCHEMA_BI.AB_25_BC_ORDER_STATUS');
select COL_NAME, REGEXP_REPLACE(COL_NAME, '^([A-Z0-9$]{17,})_.*', '\17') as BEGINNING from TEST1
and COL_NAME='DWH_SCHEMA_BI.AB_25_BC_ORDER_STATUS'
EXISTING RESULT
COL_NAME BEGINNING
DWH_SCHEMA_BI.AB_25_BC_ORDER_STATUS DWH_SCHEMA_BI.AB_25_BC_ORDER_STATUS
EXPECTED RESULT
COL_NAME BEGINNING
DWH_SCHEMA_BI.AB_25_BC_ORDER_STATUS 25
Total Patterns
DWH_SCHEMA_BI.AB_25_BC_ORDER_STATUS
DWH_SCHEMA.ABC_02_BC_ORDER_STATUS
DWH_BID.ABC_11_BC_ORDER_STATUS
Please note i have total 3 patterns, how to achieve the desired result , seems my REGEXP logic is not working as per the EXPECTED RESULTS
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=8aafffde010ae586f8a79974ee1ff140
Use regexp_substr():
select t1.*,
regexp_substr(col_name, '[0-9]+')
from test1 t1;
Here is a db<>fiddle.

How to make WHERE clause case insensitive in oracle sql database?

select * from table_Name where name ="red"
I need to fetch both "red" and "RED".
For example: I need to use both upper and lower in same statement.
How can I do this?
You could use case insensitive parameter at session 'NLS_SORT=BINARY_CI'. There are two parameters at session level:
NLS_COMP
NLS_SORT
Let's see a demo:
Normal scenario:
SQL> with names as
(
select 'Vishnu' name from dual
)
-- Your query starts here
select * from names where name='vIsHnU';
no rows selected
Case insensitive approach:
SQL> alter session set nls_comp='LINGUISTIC';
Session altered
SQL> alter session set nls_sort='BINARY_CI';
Session altered
SQL> with names as
(
select 'Vishnu' name from dual
)
-- Your query starts here
select * from names where name='vIsHnU';
NAME
------
Vishnu
One more example:
SQL> with names as
(
select 'red' name from dual union all
select 'RED' from dual
)
-- Your query starts here
select * from names where name='rEd';
NAME
----
red
RED
To improve the performance, you could also create a case insensitive INDEX.
For example:
create index names_ci_indx on names(NLSSORT(name,'NLS_SORT=BINARY_CI'));
Now, there are ways to improve performance of above transaction. Please read Oracle – Case Insensitive Sorts & Compares
select * from table_Name where lower(name) ="red"
or
select * from table_Name where upper(name) ="RED"
This cannot use an index on name. It might be appropriate to create a function based index on the expression used.

How do I set a substitution variable to the result of a query in SQL Developer?

I want to do something like this in Oracle's SQL Developer (v17.2) against an Oracle 12c database:
define thisenv = '(select name from v$database)'
select &thisenv env, count(1) from phone;
I want the select to return something like this:
ENV COUNT(1)
------ ----------
Dev1 7
If I use my sample code, I get told, effectively, that I need a group by clause because it is seeing the query instead of a string literal that is the result of the query. But adding a group by is not going to work for me.
Per the answer to this question, I tried replacing the first line with
column dummyenv new_value thisenv
select name dummyenv from v$database;
I have also tried using a bind variable instead, but I still get prompted for a value for thisenv. So, those options don't work.
What else should I try?
The PHONE table looks like this:
PHONEID PERSONID PHONENUM TYPE
------- -------- ------------ ----
899250 ABC12345 123-456-7890 WORK
Never mind the substitution variable - you are starting with the following SQL statement, which is syntactically incorrect:
select ( select name from ... ), count(1) from ...
- this does not fail due to the use of a substitution variable, it fails as a simple SQL statement.
If you want that output (as you show it), rewrite the query as
select name, ct
from (select name from v$database)
cross join
(select count(1) as ct from phone);
Now you can use a substitution variable if need be:
SQL> define thisenv = '(select name from v$database)'
SQL> select name, ct
2 from &thisenv
3 cross join
4 (select count(1) as ct from phone);
Of course, when I run this on my machine I get an error (since I don't have a table PHONE), but it should work for you. It does work for me when I use an existing table name.
You could count the phones in a scalar subquery:
SELECT name,
(SELECT count(*) FROM phone) as phones
FROM v$database;
Alternatively, as the select privileges to the view v$database are often not granted, you can use the function sys_context. Because it is a scalar, you can just put it in the query:
SELECT sys_context('userenv', 'db_name') as db_name, count(*)
FROM phone;

How to split a column using only Sql?

Lets say my table is like,
data
----
smith700
then my output should be
name sal
----- ---
smith 700
I only can use SQL statement no PL/SQL statement is allowed...How to do it?
Use SubStr
SELECT SUBSTR('smith700',1,5),SUBSTR('smith700',6,3)
UPDATE
For varying lengths of numbers and characters you could try
SELECT REGEXP_REPLACE('smith700', '[A-Za-z]','') FROM DUAL;
SELECT REGEXP_REPLACE('smith700', '[0-9]') FROM DUAL;
select substr(data,1,5) name, substr(data,6,3) sal from your_tab;

Can the "IN" operator use LIKE-wildcards (%) in Oracle?

I have searched this question, and found an answer in MySQL but this is one of those incidents where the statement fails to cross over into Oracle.
Can I use wildcards in "IN" MySQL statement?
pretty much sums up my question and what I would like to do, but in Oracle
I would like to find the legal equivalent of
Select * from myTable m
where m.status not in ('Done%', 'Finished except%', 'In Progress%')
Thanks for any help
Select * from myTable m
where m.status not like 'Done%'
and m.status not like 'Finished except%'
and m.status not like 'In Progress%'
It seems that you can use regexp too
WHERE NOT REGEXP_LIKE(field, '^Done|^Finished')
I'm not sure how well this will perform though ... see here
This appears to fit what you are looking for: https://forums.oracle.com/forums/thread.jspa?threadID=2140801
Basically, you will need to use regular expressions as there appears to be nothing built into oracle for this.
I pulled out the example from the thread and converted it for your purposes. I suck at regex's, though, so that might need tweaked :)
SELECT *
FROM myTable m
WHERE NOT regexp_like(m.status,'((Done^|Finished except^|In Progress^)')
Not 100% what you were looking for, but kind of an inside-out way of doing it:
SQL> CREATE TABLE mytable (id NUMBER, status VARCHAR2(50));
Table created.
SQL> INSERT INTO mytable VALUES (1,'Finished except pouring water on witch');
1 row created.
SQL> INSERT INTO mytable VALUES (2,'Finished except clicking ruby-slipper heels');
1 row created.
SQL> INSERT INTO mytable VALUES (3,'You shall (not?) pass');
1 row created.
SQL> INSERT INTO mytable VALUES (4,'Done');
1 row created.
SQL> INSERT INTO mytable VALUES (5,'Done with it.');
1 row created.
SQL> INSERT INTO mytable VALUES (6,'In Progress');
1 row created.
SQL> INSERT INTO mytable VALUES (7,'In progress, OK?');
1 row created.
SQL> INSERT INTO mytable VALUES (8,'In Progress Check Back In Three Days'' Time');
1 row created.
SQL> SELECT *
2 FROM mytable m
3 WHERE +1 NOT IN (INSTR(m.status,'Done')
4 , INSTR(m.status,'Finished except')
5 , INSTR(m.status,'In Progress'));
ID STATUS
---------- --------------------------------------------------
3 You shall (not?) pass
7 In progress, OK?
SQL>
Somewhat convoluted, but:
Select * from myTable m
join (SELECT a.COLUMN_VALUE || b.COLUMN_VALUE status
FROM (TABLE(Sys.Dbms_Debug_Vc2coll('Done', 'Finished except', 'In Progress'))) a
JOIN (Select '%' COLUMN_VALUE from dual) b on 1=1) params
on params.status like m.status;
This was a solution for a very unique problem, but it might help someone. Essentially there is no "in like" statement and there was no way to get an index for the first variable_n characters of the column, so I made this to make a fast dynamic "in like" for use in SSRS.
The list content ('Done', 'Finished except', 'In Progress') can be variable.
The closest legal equivalent to illegal syntax mentioned in question is:
select * from myTable m
where not exists (
select 1
from table(sys.ku$_vcnt('Done', 'Finished except', 'In Progress')) patterns
where m.status like patterns.column_value || '%'
)
Both mine and #Sethionic's answer make possible to list patterns dynamically (just by choosing other source than auxiliar sys.whatever table).
Note, if we had to search pattern inside string (rather than from the beginning) and database contained for example status = 'Done In Progress', then
my solution (modified to like '%' || patterns.column_value || '%') would still generate one row for given record, whileas
the #Sethionic's solution (modified to another auxiliar join before a) would produce multiple rows for each pattern occurence.
Not judging which is better, just be aware of differences and choose which better fits your need.