Remove content inside parentheses - openrefine

Have several cells in a column that at the end of the sentence have a sentence inside of ( )
Ex. Hello World (wwfx fgty jkilo)
The output desire is Hello World ()

You can use 'match' to do this. The awkward bit is making sure you deal with both the situation where there are brackets and where there are not brackets successfully.
Using match:
with(value.match(/(.*)\(.*\)/)[0],w,if(isNonBlank(w),w+"()",value))
This extracts everything before the last open bracket in the string. If there is no open bracket then it just uses the original value

The answer without using regex:
create a text filter searching for (
create a text filter searching for )
use the following GREL value.split('(')[0]+'()'+value.split(')')[-1]
where
value.split('(')[0] selects everything before the first (
value.split(')')[-1] selects everything after the last ) +'()'+
concatenate the two results and add the ()

Related

Regex match first number if it does not appear at the end

I am currently facing a Regex problem which apparently I cannot find an answer to.
My Regex is embedded in a teradata SQL of the form:
REGEXP_SUBSTR(column, 'regex_pattern')
I want to find the first appearance of any number except if it appears at the end of the string.
For Example:
"YEL2X30" -> "2"
"YEL19XYZ05" -> "19"
"YELLOW05" -> ""
I tried it with '[0-9]+(?!$)/' but this returns me a blank String always.
Thanks in Advance!
Shot in the dark here since I'm unfamiliar with teradata and the supported SQL-functionality. However, reading the docs on the REGEXP_SUBSTR() function it seems like you may want to use the 3rd and 4th possible argument along with a slightly different regular expression:
[0-9]+(?![0-9]|$)
Meaning: 1+ Digits that are not followed by either the end of the string or another digit.
I'd believe the following syntax may work now to retrieve the 1st appearance of any number from the matching results:
REGEXP_SUBSTR(column, '[0-9]+(?![0-9]|$)', 1, 1)
The 3rd parameter states from which position in the source-string we need to start searching whereas the 4th will return the 1st match from any possible multiple matches (is how I read the docs). For example: abc123def456ghi789 whould return 123.
Fiddling around in online IDE's gave me that:
CREATE TABLE TBL (TST varchar(100));
INSERT INTO TBL values ('YEL2X30'), ('YEL19XYZ05'), ('YELLOW05'), ('abc123def456ghi789');
SELECT REGEXP_SUBSTR(TST, '[0-9]+(?![0-9]|$)', 1, 1) as 'RESULTS' FROM TBL;
Resulted in:
RESULTS
2
19
NULL
123
NOTE: I also noticed that leaving out the 3rd and 4th parameter made no difference since they will default back to 1 without explicitly mentioning them. I tested this over here.
Possibly the simplest way is to look for digits followed by a non-digit. Then keep all the digits:
regexp_substr(regexp_substr(column, '[0-9]+[^0-9]'), '[0-9]+')

SQL request to find item in table by value in column

Its pretty simple question, I know, but I really stacked with a problem with it...
I have a table customer_customer and a column code in it. So I need to find all items with a specific code value. So I wrote that:
SELECT * FROM customer_customer WHERE code LIKE "КL-12345"
and got an error:
column "КL-12345" does not exist
Why КL-12345 became a column if I specify it as value of code column? What am I doing wrong?
String literals must be enclosed in single quotes.
By enclosing it in double quotes, you specified a variable name.
Also, note that your where condition is the same as writing
where code = 'КL-12345'
LIKE is used for pattern matching. For instance you would match all codes that contain 'KL-12345' like this
where code like '%KL-12345%'
Change it to single quotes
SELECT * FROM customer_customer WHERE code LIKE 'КL-12345'
or
SELECT * FROM customer_customer WHERE code = 'КL-12345'

How to return first letter of a word in Access

I have been trying to return the first letter of a last name in Access.
I tried this in a query but it shows an error message.
In my table I have a field called lastName.
When I make a new query and open the expression builder I use the following expression:
Example: Left([lastName],1)
It shows the error that it is invalid.
How do I make this work?
Microsoft's Help on String Functions shows that you should be using the following format:
=Left([SerialNumber],2)
If [SerialNumber] is “CD234”, the result is “CD”.
I took a quick look at a few different languages, and they all showed the same format. None of them used a semicolon (;) in place of a comma (,).

string replace sql select

Hi Ive tried to find an answer to this but cant find one.
Id like to remove some characters and prepend a pound sign to the result of an SQL query which looks as follows (its already using a replace command can I stack these)?:
select fundraiser.Company_Name,
replace(Just_Giving_Campaign,'"label":',''),
sum(fundraising_campaigns.Total_Collected) as donations
from fundraising_campaigns,
fundraiser
where Charity_Name = 'WaterAid'
and fundraising_campaigns.Campaigners_ID = fundraiser.id
group by fundraiser.Company_Name
Can anyone confirm how I would go about adding (£ sign) and remove several sets of characters from a select statement.Certainly dont appear to be able to stack replace statements (e.g.
replace(replace (string, what to match, what to replace it with), what to match, what to replace it with)
Appreciate any thoughts
I am not sure about your question. If I am correct you want to prepend £ and do some nested replace. Hope the below example helps.
select '£'+replace(replace('YourText','x','s'),'You','U')

Search for string with conditions

I have table with a name filed which is string. I need to create a sql statement that searches for children to a node without finding the children to the children. Is it possible to use LIKE and some wildcards to accomplish this? You can see some examples below of the results I need to get based on my search string.
Search string is /home
Then the follwing entries should be returned
/home/something
/home/somethingElse
but not
/home/something/foo
/home/something/bar
/home/somethingElse/foo
but if the search string is /home/something
These should be returned
/home/something/foo
/home/something/bar
SELECT name FROM table
WHERE name LIKE '/home/%' AND name NOT LIKE '/home/%/%'
should filter out anything with second level node under it.
I would probably search on the number of slashes in addition to the actual keywords. So the first one would be searching for /home with 1-2 /'s
The second one would be /home/something wtih 2-3 slashes.
I don't have sql up infront of me, but I'll work on some sample code for you.
Edit:
CREATE FUNCTION [dbo].[ufn_CountChar] ( #pInput VARCHAR(1000), #pSearchChar CHAR(1) )
RETURNS INT
BEGIN
RETURN (LEN(#pInput) - LEN(REPLACE(#pInput, #pSearchChar, '')))
END
GO
This little function will act nicely to count the number of slashes in your strings.
Enjoy
Hope this helps,
Cheers,