sql dynamic replacement - sql

My requirement is that I am searching a bunch of columns in a table using regex_like function: Below is an example:
SELECT *
FROM dsopi_person_addr_rule ADDR
WHERE regexp_like (UPPER(addr.src_address_line1),
'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')
Now what I would like to do if I find a match I would like to take the value from src_address_line1 and put that in another column of of the same table. I know how to do this but can anyone recommend how to do this efficiently. I am open to using cursors.

You dont need cursor for this
UPDATE dsopi_person_addr_rule ADDR
SET new_col = addr.src_address_line1
WHERE regexp_like (UPPER(addr.src_address_line1),
'DEP|DPT$|ABT|DIP.|DIPART|AFDEL|AVDEL|AVD.|DIV|PGRD|PGP|PPG')

I'd take a look at the CONTAINSTABLE clause, but that would require enabling / creating FULLTEXT indexes.
http://msdn.microsoft.com/en-us/library/ms189760.aspx

Related

SQL get number from right side of string without LIKE

I'm needing a solution to this query but I can't use a LIKE statement. I'm thinking I can use INSTR and SUBSTR but I'm not very well versed with those.
Ordinarily I could just do a LIKE '%7' But I can't do that here. The goal of this is to bring back a list of any row in my database that has a string in Column1 that ends in a 7. It's also noteworthy that not all of the strings are the same length, and that I am using Oracle SQL Developer. I've copied what the LIKE statement for this could look like below.
SELECT Column1, Column2
FROM Table1
WHERE Column1 LIKE '%7'
ORDER BY Column2;
Maybe something like this?
SELECT x FROM t WHERE SUBSTR(x, -1) = '7'
Well, one simple solution is:
where regexp_like(x, '7$')
But it is unclear if you can use that function either.
Just to complement the other solutions that I find simpler and good enough for most cases.
When using a pattern that starts with a % sometimes the query becomes [very] slow. If it's for performance issues, I would recommend an [overkill] solution that has a much better performance since it uses Index Range Scan rather than Full Table Scan:
Create a stored procedure to reverse the string:
CREATE OR REPLACE PROCEDURE ReverseOf(input IN varchar2) IS
rev varchar2(50):='';
BEGIN
FOR i in reverse 1..length(input) LOOP
rev := rev||substr(input, i, 1);
END LOOP;
dbms_output.put_line(rev);
END;
Create a virtual column in the table:
alter table table1 add column reversed_column1
generated always as (ReverseOf(column1)) virtual;
Create an index on the virtual column:
create index ix_reverse_column1 on table1 (reversed_column1);
Use a "reverse LIKE" (that is very fast):
SELECT Column1, Column2
FROM Table1
WHERE reversed_column1 LIKE '7%' -- reverse pattern here!
ORDER BY Column2;
Maybe overkill, but you may need it. It helped me a lot last year.

SQL code for retrieving values of column starting using wildcard

I want to look for values in variable/column which start with 'S' and has 'gg' in between.
For instance Staggered is a word which starts with alphabet S and has gg in between the word.
so what sql query to write to get the result.
Due to the fact that you did not provide much meta information (which database?), I'll just show the following:
SELECT * FROM <table>
WHERE <columnname> LIKE 'S%gg%';
Good luck :)
As the target database is not mentioned, I will answer with Oracle syntax:
select *
from TABLE_NAME
where COL_NAME like 'S%gg%'

Compare String with "like" against a list

is it possible to compare a string against a list using "like" and wildcards, so sth like
select column
from table
where column like('%foo%', '%bar%')
The example does not work in any database format I know. How can I do this without using a verbose solution like
select column
from table
where column like'%foo%' or column like '%bar%'
I am interested in a platform-independent solution but mainly Sybase ASE.
Thanks very much!
If you are using MySQL/PostgreSQL you could use regexp, that is one way.
SELECT column FROM table WHERE (column REGEXP '^ALA[0-9]')
http://dev.mysql.com/doc/refman/5.0/en/regexp.html#operator_regexp
http://www.oreillynet.com/pub/a/databases/2006/02/02/postgresq_regexes.html
Second solution would be, mentioned by you creating many likes, joined by or.

SQL IN Statement using like syntax? [duplicate]

This question already has answers here:
Is there a combination of "LIKE" and "IN" in SQL?
(28 answers)
Closed 9 years ago.
I would like to do something like this i.e., use wild card characters in the in clause:
SELECT * FROM mytable WHERE keywords IN ('%test%', '%testing%')
This is not supported in SQL Server.... Is there some other way to achieve it...
Looking for something other than:
SELECT * FROM mytable WHERE keywords like '%test%' or keywords like '%testing%' or.....
Nope, only other way I can think of is joining in to a temp table but then you have to eliminate duplicate rows.
This query of yours is horribly inefficient as it will unconditionally table scan. Perhaps you should look at separating the keywords or introducing a full text index.
If you would like to look into doing full text searches, then I would recommend looking into some of the heavy weights out there like sphinx or lucene when you evaluate the Sql Server full-text solution.
SELECT DISTINCT *
FROM
mytable M
JOIN
(
SELECT '%test%' AS pattern
UNION ALL SELECT '%testing%'
...
) foo ON M.keywords LIKE foo.Pattern
Could be a CTE or temp table too
This is not supported in MS SQL.... Is
there some other way to achieve it?
Yes, Full Text Search. At least for prefix wildcards.
You can use Regular Expression to do this.
In one Regular Expression you can define as many pattern as you want in one expression.
There are lot's of article about Regular Expression Matching in MS SQL SERVER.
I used RLIKE in MySQL to do Regular Expression Matching.
I also attached link1 & link2 for MS SQL SERVER Regular Expression Matching.

Oracle DB: How can I write query ignoring case?

As I had written in title, I have SQL query, run on Oracle DB, lets say:
SELECT * FROM TABLE WHERE TABLE.NAME Like 'IgNoReCaSe'
If I would like, that the query would return either "IGNORECASE", "ignorecase" or combinations of them, how can this be done?
Select * from table where upper(table.name) like upper('IgNoreCaSe');
Alternatively, substitute lower for upper.
Use ALTER SESSION statements to set comparison to case-insensitive:
alter session set NLS_COMP=LINGUISTIC;
alter session set NLS_SORT=BINARY_CI;
If you're still using version 10gR2, use the below statements. See this FAQ for details.
alter session set NLS_COMP=ANSI;
alter session set NLS_SORT=BINARY_CI;
You can use either lower or upper function on both sides of the where condition
You could also use Regular Expressions:
SELECT * FROM TABLE WHERE REGEXP_LIKE (TABLE.NAME,'IgNoReCaSe','i');
You can use the upper() function in your query, and to increase performance you can use a function-base index
CREATE INDEX upper_index_name ON table(upper(name))
You can convert both values to upper or lowercase using the upper or lower functions:
Select * from table where upper(table.name) like upper('IgNoreCaSe')
or
Select * from table where lower(table.name) like lower('IgNoreCaSe');
In version 12.2 and above, the simplest way to make the query case insensitive is this:
SELECT * FROM TABLE WHERE TABLE.NAME COLLATE BINARY_CI Like 'IgNoReCaSe'
...also do the conversion to upper or lower outside of the query:
tableName:= UPPER(someValue || '%');
...
Select * from table where upper(table.name) like tableName
Also don't forget the obvious, does the data in the tables need to have case? You could only insert rows already in lower case (or convert the existing DB rows to lower case) and be done with it right from the start.