A better way of performing replace on a string in SQL? - sql

Let's say I have a string in one cell that is like this:
{IR1} {IR2} some text {IR3} some more text {IR6} {IR7}
I want to replace all {IR1} to {IR7} with known values from another table.
What is the most efficient way of replacing all IRX substrings with knows replacements values?
At the moment I can replace replace the number of replace values in that table: like Replace(Replace(Replace.......)...)...)..), however I want to know if there is anything better than this.

Related

How do I use the contain function in AWS Athena to find certain text

I have a simple table let's say Names. In the column First I want to find all rows that contain the letters Jo. It would then result in Joe and John.
However when I try and use contains function it talks about an array and I have no idea how to get it to work.
Thanks.
CONTAINS() is a function that checks if an elements is in an array.
You can use the LIKE operator (which is Standard SQL). If you want names that start with "Jo":
where name like 'Jo%'
If you want to match names with "Jo" anywhere:
where name like '%Jo%'

Is there a way in Redshift to extract all URLs from a block of text each as a separate row?

I'm trying to extract all URLs of a certain format from a block of text that may contain 0, 1, or lots of URLs.
For example, one row may have the value:
'some text
blahblahblah.com
more text
secondwebsite.com
more test'
And I would want that represented in my output as two rows:
'blahblahblah.com'
'secondwebsite.com'
I can use REGEXP_SUBSTR and SPLIT_PART to get just the first .com occurrence, but ideally I'd be able to extract all occurrences separately. Is there a way to do this?
Returning multiple output rows from a single input row is not easy in SQL.
You would need to cross-join to a numeric table (eg a table that contains rows containing 1, 2, 3..., and then use that number to reference a separate part of the input row.
Bottom line: It is possible, but it is messy. Try to find an alternate way of achieving this.
If you like to create separate rows from a field with multiple URLs, it is usually not feasible in SQL, I suggest you use an ETL tool or some scripting language like python to do this.

Replace multiple characters with a single line in ORACLE SQL

I have a phone number and zip code field in a table. I am trying to get this information into a common format, and I want to get rid of all the extra junk like dashes, parenthesis, spaces, and letters.
I was wondering if there was a way to do this with the replace function, I tried doing it similarly to how one would in REGEXP_LIKE() and had no luck, this is what I have.
select (REPLACE(numbers.PHONE,'[a-zA-Z._-%() ]',''))
from table numbers;
If there isn't a way to do this that's fine, I just wanted to avoid having to make a whole bunch of replace statements for everything I want to replace.
It would depend on how much junk you have in your zip codes and phones. For example, you could remove all non-digital characters in those fields with a replace like this one:
SELECT REGEXP_REPLACE('234N2&.-#3NDJ23842','[^[:digit:]]+') FROM DUAL
And afterwards you could format the resulting digits with a replace like this:
SELECT REGEXP_REPLACE('2342323842','([[:digit:]]{3})([[:digit:]]{3})([[:digit:]]{4})','\1 \2 \3') FROM DUAL
I know the examples are not valid as zip codes nor phone numbers but I think they might help you.

VB.NET BindingSource filter for Datagridview

In MSSQL I can filter a query on a phone number like this:
where replace(phone,'-','') Like '%480555%'
I am trying to figure out how to do this on a Datasource. A normal query looks like this:
Dim stringFilter As String = String.Empty
String.Format("phone Like '%480555%'")
ViewCustomersBindingSource.Filter = stringFilter
However, this will not find any results because the datasource has the values with hyphens in it. REPLACE is not a valid argument for filtering.
My initial thought was to update the MSSQL View to strip the hyphens. However, for display, I would want to display the hyphens. I can not assume they will all look the same as some phone numbers might be a different country than the US.
Is there another way to filter on a telephone number and ignore the hyphens?
I dont think you can directly set a filter that ignores the hyphens (as in your first query) on your binding source. There is no String operations possible in the filter expression column. From this http://msdn.microsoft.com/fr-fr/library/system.windows.forms.bindingsource.filter.aspx the only string operation allowed is +.
But you can keep and display the original column which contains the hyphens, and add another column (without the hyphens) that you do not display, but that you use just for filtering.
With MSSQL, you can for example add a computed column that uses the REPLACE function.
Here is somes links that could help you to create a computed column:
How to replace double quotes in derived column transformation?.
How to create a calculated column in a SQL Server 2008 table
Hope this helps.

Replace all occurrences of a substring in a database text field

I have a database that has around 10k records and some of them contain HTML characters which I would like to replace.
For example I can find all occurrences:
SELECT * FROM TABLE
WHERE TEXTFIELD LIKE '%&#47%'
the original string example:
this is the cool mega string that contains &#47
how to replace all &#47 with / ?
The end result should be:
this is the cool mega string that contains /
If you want to replace a specific string with another string or transformation of that string, you could use the "replace" function in postgresql. For instance, to replace all occurances of "cat" with "dog" in the column "myfield", you would do:
UPDATE tablename
SET myfield = replace(myfield,"cat", "dog")
You could add a WHERE clause or any other logic as you see fit.
Alternatively, if you are trying to convert HTML entities, ASCII characters, or between various encoding schemes, postgre has functions for that as well. Postgresql String Functions.
The answer given by #davesnitty will work, but you need to think very carefully about whether the text pattern you're replacing could appear embedded in a longer pattern you don't want to modify. Otherwise you'll find someone's nooking a fire, and that's just weird.
If possible, use a suitable dedicated tool for what you're un-escaping. Got URLEncoded text? use a url decoder. Got XML entities? Process them though an XSLT stylesheet in text mode output. etc. These are usually safer for your data than hacking it with find-and-replace, in that find and replace often has unfortunate side effects if not applied very carefully, as noted above.
It's possible you may want to use a regular expression. They are not a universal solution to all problems but are really handy for some jobs.
If you want to unconditionally replace all instances of "&#47" with "/", you don't need a regexp.
If you want to replace "&#47" but not "&#471", you might need a regexp, because you can do things like match only whole words, match various patterns, specify min/max runs of digits, etc.
In the PostgreSQL string functions and operators documentation you'll find the regexp_replace function, which will let you apply a regexp during an UPDATE statement.
To be able to say much more I'd need to know what your real data is and what you're really trying to do.
If you don't have postgres, you can export all database to a sql file, replace your string with a text editor and delete your db on your host, and re-import your new db
PS: be careful