Multiple Replacements with Oracle REGEXP_REPLACE - sql

Ok so an Imperial Agent has gained access to all the Galactic mail servers and has created a mail account for Darth Vadar on each one...
There is a master distribution list maintained in an Oracle Column by the Republic that looks like this:
~To,Chewie,ChewBacca#wookie.net~;~Cc,Han Solo,Millenium#Falcon.com~;~Cc,Luke Skywalker,Luke#IamYourFather.co.uk~
Our Imperial Agent needs help using Oracle REGEXP_REPLACE to replace all the email account names portions with the Darth Vadar account, Vadar#... so the end result would be:
~To,Chewie,Vadar#wookie.net~;~Cc,Han Solo,Vadar#Falcon.com~;~Cc,Luke Skywalker,Vadar#IamYourFather.co.uk~
Can this be done as a single statement?
You would think that using the Dark Force would be easier then this.

Might be too simple for some cases, but for your example this works:
regexp_replace(value, '[[:alnum:]\.]*#', 'Vadar#')
e.g:
select regexp_replace('~To,Chewie,ChewBacca#wookie.net~;~Cc,Han Solo,Millenium#Falcon.com~;~Cc,Luke Skywalker,Luke#IamYourFather.co.uk~',
'[[:alnum:].%_+-]*#', 'Vadar#')
from dual;
~To,Chewie,Vadar#wookie.net~;~Cc,Han Solo,Vadar#Falcon.com~;~Cc,Luke Skywalker,Vadar#IamYourFather.co.uk~
SQL Fiddle with dash and period examples.

Related

Show list of SQL Server Agent operators in a query

How can I see (with a query) the list of operators of SQL Server Agent with their e-mail address?
I am asking it because i want to put more than one e-mail address and I used the msdb.dbo.sp_update_operator construct as described here, but unfortunally I can use only 100 characters so I want to check if the were a truncation.
You can get, through the following query, all of the operators email addresses, along with a lot of other potentially useful information.
SELECT *
FROM [msdb].[dbo].[sysoperators]

How to find non english language and its data in a table of sql server

I have a Sql Server table loaded with data from multiple countries. Say Japanese, Thai, Urdu, Portuguese, Spanish and many more which i didn't identify.
How to identify the language and its relevant data from that table ?
sample:
colid | colname
1 | stackoverflow
2 | 龍梅子, 老貓
i need a query to produce :
stackoverflow, english
龍梅子, 老貓 , chinese
is this possible to get ?
You might try this:
DECLARE #str NVARCHAR(100)=N'龍梅子, 老貓';
SELECT UNICODE(Left(#str,1))
The result is 40845
A google search on unicode 40845 brought me here.
It seems to be the dragon or emperor from chinese character set
Some more information upon unicode character ranges
The given code points to CJK Unified Ideographs
UPDATE
In order to use this:
You will either have to
get a local copy of a full list into your database and do a look-up. Something like from here: http://www.tamasoft.co.jp/en/general-info/unicode.html
or create a range table (much smaller!) like in the link given (http://jrgraphix.net/research/unicode_blocks.php)
or do some kind of web lookup...
You can't, what if the languages use the same alphabet, or even worse share the same words? You obviously wouldn't want to ask an internet ai-based program to decide.
You should add a column language_id, have a table for the languages and hope that it's not too many entries to update.

DB2 complex like

I have to write a select statement following the following pattern:
[A-Z][0-9][0-9][0-9][0-9][A-Z][0-9][0-9][0-9][0-9][0-9]
The only thing I'm sure of is that the first A-Z WILL be there. All the rest is optional and the optional part is the problem. I don't really know how I could do that.
Some example data:
B/0765/E 3
B/0765/E3
B/0764/A /02
B/0749/K
B/0768/
B/0784//02
B/0807/
My guess is that I best remove al the white spaces and the / in the data and then execute the select statement. But I'm having some problems writing the like pattern actually.. Anyone that could help me out?
The underlying reason for this is that I'm migrating a database. In the old database the values are just in 1 field but in the new one they are splitted into several fields but I first have to write a "control script" to know what records in the old database are not correct.
Even the following isn't working:
where someColumn LIKE '[a-zA-Z]%';
You can use Regular Expression via xQuery to define this pattern. There are many question in StackOverFlow that talk about patterns in DB2, and they have been solved with Regular Expressions.
DB2: find field value where first character is a lower case letter
Emulate REGEXP like behaviour in SQL

oracle - query data having non-latin characters

I'm not familiar oracle but have to use PHP/PLSQL to query an oracle database. These data are old and kept in a very bad way.
For example, a person's name let say ÇİĞDEM may be kept in several different ways like CIGDEM or ÇÝÐDEM or ÇIĞDEM or CİĞDEM etc :(
What've did so far is replacing characters that I've found using replace but I don't like it. It works for most of the cases but I can't just accept this. It should work for all possible combinations.
SELECT ... FROM ... WHERE replace(replace(CONVERT(ADI, 'US7ASCII', 'US7ASCII'), chr(221), 'I'), 'Ü', 'U') LIKE :myvariablehere ...
Is there an elegant way to search this kind of data
EDIT
Database version is 10g
I have good experiences with Knuth's soundex function which is available in oracle SQL. Or use Oracle Text as described at http://docs.oracle.com/cd/E18283_01/text.112/e16594/search.htm
Good luck!

What's the best approach to embed RegEx in Oracle or SQL Server 2005 SQL?

This is a 3 part question regarding embedded RegEx into SQL statements.
How do you embed a RegEx expression into an Oracle PL/SQL
select statement that will parse out
the “DELINQUENT” string in the text
string shown below?
What is the performance impact if used within a
mission critical business
transaction?
Since embedding regex
into SQL was introduced in Oracle
10g and SQL Server 2005, is it
considered a recommended practice?
Dear Larry :
Thank you for using ABC's alert service.
ABC has detected a change in the status of one of your products in the state of KS. Please review the
information below to determine if this status change was intended.
ENTITY NAME: Oracle Systems, LLC
PREVIOUS STATUS: --
CURRENT STATUS: DELINQUENT
As a reminder, you may contact your the ABC Team for assistance in correcting any delinquencies or, if needed, reinstating
the service. Alternatively, if the system does not intend to continue to engage this state, please notify ABC
so that we can discontinue our services.
Kind regards,
Service Team 1
ABC
--PLEASE DO NOT REPLY TO THIS EMAIL. IT IS NOT A MONITORED EMAIL ACCOUNT.--
Notice: ABC Corporation cannot independently verify the timeliness, accuracy, or completeness of the public information
maintained by the responsible government agency or other sources of data upon which these alerts are based.
Why would you need regular expressions here?
INSTR and SUBSTR will do the job perfectly.
But if you convinced you need Regex'es you can use:
REGEXP_INSTR
REGEXP_REPLACE
REGEXP_SUBSTR
(only available in Oracle 10g and up)
SELECT emp_id, text
FROM employee_comment
WHERE REGEXP_LIKE(text,'...-....');
If I recall correctly, it is possible to write a UDF in c#/vb for SQL Server.
Here's a link, though possibly not the best: http://www.novicksoftware.com/coding-in-sql/Vol3/cis-v3-N13-dot-net-clr-in-sql-server.htm
Why not just use INSTR (for Oracle) or CHARINDEX (for SQL Server) combined with SUBSTRING? Seems a bit more straightforward (and portable, since it's supported in older versions).
http://www.techonthenet.com/oracle/functions/instr.php and http://www.adp-gmbh.ch/ora/sql/substr.html
http://www.databasejournal.com/features/mssql/article.php/3071531 and http://msdn.microsoft.com/en-us/library/ms187748.aspx
INSTR and CHARINDEX are great alternative approaches but I'd like to explore the benefits of embedding Regex.
In MS SQL you can use LIKE which has some "pattern matching" in it. I would guess Oracle has something similar. Its not Regex, but has some of the matching capabilities. (Note: its not particularly fast).. Fulltext searching could also be an option (again MS SQL) (probably a much faster way in the context of a good sized database)