Pl/Sql using instr to find exact match - string
I am trying to find if a string exist in a word and extract it. I have uses the instr() function but this works as the LIKE function: if part or the whole word exists it returns it.
Here I want to get the string 'Services' out, it works but if I change 'Services' to 'Service' it still works. I don't want that. If 'Service' is entered it should return null and not 'Services'
Modified:
What I am trying to do here is abbreviate certain parts of the company name.
This is what my database table looks like :
Word | Abb
---------+-----
Company | com
Limited | ltd
Service | serv
Services | servs
Here is the code:
Declare
Cursor Words Is
SELECT word,abb
FROM abbWords
processingWord VARCHAR2(50);
abbreviatedName VARCHAR(120);
fullName = 'A.D Company Services Limited';
BEGIN
FOR eachWord IN Words LOOP
--find the position of the word in name
wordPosition := INSTR(fullName, eachWord.word);
--extracts the word form the full name that matches the database
processingWord := Substr(fullName,instr(fullName,eachWord.word), length(eachWord.word));
--only process words that exist in name
if wordPosition > 0 then
abbreviatedName = replace(fullName, eachWord.word,eachWord.abb);
end if;
END lOOP;
END;
So if the user enters 'Service' I don't want 'Services' to be returned. By this I mean word position should be 0 if the word 'Service' in not found instead of returning the position for the word 'Services'
One way of doing it:
DECODE(INSTR('A.D Company Seervices Limited','Services'),
0,
NULL,
SUBSTR('A.D Company Services Limited',
INSTR('A.D Company Services Limited','Services'),
length('Services')))
INSTR() will return 0 if text is not found. DECODE() will evaluate the first argument, compare to the second, if match, return third argument, if not, return fourth argument. (sqlfiddle link)
Arguably not the most elegant way, but matches your requirement.
I think you're over-complicating this. You can do everything with regular expressions. For instance; given the following table:
create table names ( name varchar2(100));
insert into names values ('A.D Company Services Limited');
insert into names values ('A.D Company Service Limited');
This query will only return the name 'A.D Company Services Limited'.
select *
from names
where regexp_like( name
, '(^|[[:space:]])services($|[[:space:]])'
, 'i' )
This means match the beginning of the string, ^, or a space followed by services followed the end of the string, $, or a space. This is what differentiates regular expressions from using instr etc. You can make your matches easily conditional on other factors.
However, though this seems to be your question I don't think this is what you're trying to do. You're trying to replace the string 'serv' in your wider string without replacing 'services' or 'service'. For this you need to use regexp_replace().
If I add the following row to the table:
insert into names values ('A.D Company Serv Limited');
and run this query:
select regexp_replace( name
, '(^|[[:space:]])serv($|[[:space:]])'
, ' Services '
, 1, 0, 'i' )
from names
The only thing that will change is ' Serv ', which in this newest line, will be replaced with ' Services '. Note the spaces; as you don't want to replace 'Services' with 'ServServices' these are very important.
Here's a little SQL Fiddle to demonstrate.
Another alternative is to use something like:
select replace(name,' serv ', ' Services ')
from names;
This will replace only the word 'Serv' situated between 2 spaces.
Thank you,
Alex.
INSTR returns a number: the index of the first occurrence of the matching string. You should use regexp_substr instead (10g+):
SQL> select regexp_substr('A.D Company Services Limited', 'Services') match,
2 regexp_substr('A.D Company Service Limited', 'Services') unmatch
3 from dual;
MATCH UNMATCH
-------- -------
Services
Related
How to remove all characters before a specific character in Cognos Report Studio 10.2
I have columns with different company names. In front of each company name there is a Company_ID. After the Company_ID a specific character = _ divides the ID from the Name. For example i have 111_Mercedes 11B4324_Apple 38A_Google A1ZH8_Airline I would like to remove all characters including the specific character. Result should be Mercedes Apple Google Airline Thanks in advance
If this is all in one data item and you need a pattern removed, try this: As an example, 111_Mercedes 11B4324_Apple 38A_Google The name starts with _ and ends with a space Because of this, we can use the replace function to set up the process in two steps 1) Wrap the undesired portion in brackets Sql would look like this select concat('<',replace( replace('111_Mercedes 11B4324_Apple 38A_Google',' ','<') ,'_','>')) FROM sysibm.sysdummy1 The result would look like <111>Mercedes<11B4324>Apple<38A>Google 2) Then remove the content in the brackets Sql would look like this: Select trim(REGEXP_REPLACE( '<111>Mercedes<11B4324>Apple<38A>Google' , '<(.*?)>',' ',1,0,'c')) FROM sysibm.sysdummy1 The result would look like this Mercedes Apple Google For Cognos try to use the functions in the data item definitions BracketCompany = concat('<',replace(replace([Company ID],' ','<'),'_','>')) Then another data item over this, to remove the content within the brackets FinalCompany = trim(REGEXP_REPLACE([BracketCompany], '<(.*?)>',' ',1,0,'c'))
String contains substring and substring not part of longer word (exact match)
I have captured the full text of a PDF-file in a string called pdfText. Next I am looping through an array containing substrings to be found/searched for in the pdfText-string. One of the substrings is Invoice. Both pdfText and the substrings I am searching for are converted to lower case. If at least one of the substrings are found in the pdfText, a boolean is set to true. Now, I have an example where the pdtText contains '...Net amount to be invoiced...'. This is the only variant of 'invoice' in the text. This of course returns true if I use substring = "Invoice" ... pdfText.contains(substring.ToLower). But in this case I need it to return false. I need to find only exact matches. Another example, if the pdfText contains '...This is an invoice. Please pay....Net amount to be invoiced...' the boolean should be set to true because of the first invoice-match, but not the second invoiced-(non)match. So what I am looking for is to find a substring Invoice in a string pdfText and make sure, that the substring is not part of a longer word invoiced, invoice-process etc.. Note, that invoice. should return True. I believe this should be possible, but cannot wrap my head around it currently. I might need to use regex?
This one uses the RegEx, with a slight change, proposed by #Mederic at https://stackoverflow.com/a/45587916/2326360 Use the build in UiPath activity Is Match, found under Programming->String. Use it inside your loop, with the current settings. The RegEx is: substring+"[^a-zA-Z]" I have declared the following variables:
RegEx would be a good approach. I only started RegEx not long ago but I think this would work fine. RegEx: (invoice)[^a-zA-Z] Explanation: () Creates a Capture Group invoice looks for the match for invoice [^a-zA-Z] Checks there are no characters from a-z or A-Z after Example: Sample: This was invoiced Result: No Result Sample: This is an invoice. Result: Match on invoice. Capture group 1 = invoice Implementation: Dim m As Match = Regex.Match(pdfText.ToLower,"(invoice)[^a-zA-Z]") ' If successful, write the group. If (m.Success) Then Dim key As String = m.Groups(1).Value Console.WriteLine(key) End If
how to use like and substring in where clause in sql
Hope one can help me and explain this query for me, why the first query return result but the second does not: EDIT: first query: select name from Items where name like '%abc%' second Query: select name from Items where name like substring('''%abc%''',1,10) why the first return result but the second return nothing while substring('''%abc%''',1,10)='%abc%' If there are a logic behind that, Is there another approach to do something like the second query, my porpuse is to transform a string like '''abc''' to 'abc' in order to use like statement,
You can concatenate strings to form your LIKE string. To trim the first 3 and last 3 characters from a string use the SUBSTRING and LEN functions. The following example assumes your match string is called #input and starts and ends with 3 quote marks that need to be removed to find a match: select name from Items where name like '%' + SUBSTRING(#input, 3, LEN(#input) - 4) + '%'
Display the specific part of the string in PostgreSQL 9.3
I have a string to modify as per the requirements. For example: The given string is: str1 varchar = '123,456,789'; I want to show the string as: '456,789' Note: The first part (delimited) with comma, I want to remove from string and show the rest of string. In SQL Server I used STUFF() function. SELECT STUFF('123,456,789',1,4,''); Result: 456,789 Question: Is there any string function in PostgreSQL 9.3 version to do the same job?
you can use regular expressions: select substring('123,456,789' from ',(.*)$'); The comma matches the first comma found in the string. The part inside the brackets (.*) is returned from the function. The symbol $ means the end of the string. A alternative solution without regular expressions: select str, substring(str from position(',' in str)+1 for length(str)) from (select '123,456,789'::text as str) as foo;
You could first turn the string to array and return second and third cell: select array_to_string((regexp_split_to_array('123,456,789', ','))[2:3], ',') Or you could use substring-function with regular expressions (pattern matching): SELECT substring('123,456,789' from '[0-9]+,([0-9]+,[0-9]+)') [0-9]+ means one or more digits parentheses tell to return that part from the string Both solutions work on your specific string.
Your The SQL Server example indicates you just want to remove the first 4 characters, which makes the rest of your question seem misleading because it completely ignores what's in the string. Only the positions matters. Be that as it may, the simple and cheap way to cut off leading characters is with right(): SELECT right('123,456,789', -4); SQL Fiddle.
Lua: Search a specific string
Hi all tried all the string pattrens and library arguments but still stuck. i want to get the name of the director from the following string i have tried the string.matcH but it matches the from the first character it finD from the string the string is... fixstrdirector = {id:39254,cast:[{id:15250,name:Hope Davis,character:Aunt Debra,order:5,cast_id:10,profile_path:/aIHF11Ss8P0A8JUfiWf8OHPVhOs.jpg},{id:53650,name:Anthony Mackie,character:Finn,order:3,cast_id:11,profile_path:/5VGGJ0Co8SC94iiedWb2o3C36T.jpg},{id:19034,name:Evangeline Lilly,character:Bailey Tallet,order:2,cast_id:12,profile_path:/oAOpJKgKEdW49jXrjvUcPcEQJb3.jpg},{id:6968,name:Hugh Jackman,character:Charlie Kenton,order:0,cast_id:13,profile_path:/wnl7esRbP3paALKn4bCr0k8qaFu.jpg},{id:79072,name:Kevin Durand,character:Ricky,order:4,cast_id:14,profile_path:/c95tTUjx5T0D0ROqTcINojpH6nB.jpg},{id:234479,name:Dakota Goyo,character:Max Kenton,order:1,cast_id:15,profile_path:/7PU6n4fhDuFwuwcYVyRNVEZE7ct.jpg},{id:8986,name:James Rebhorn,character:Marvin,order:6,cast_id:16,profile_path:/ezETMv0YM0Rg6YhKpu4vHuIY37D.jpg},{id:930729,name:Marco Ruggeri,character:Cliff,order:7,cast_id:17,profile_path:/1Ox63ukTd2yfOf1LVJOMXwmeQjO.jpg},{id:19860,name:Karl Yune,character:Tak Mashido,order:8,cast_id:18,profile_path:/qK315vPObCNdywdRN66971FtFez.jpg},{id:111206,name:Olga Fonda,character:Farra Lemkova,order:9,cast_id:19,profile_path:/j1qabOHf3Pf82f1lFpUmdF5XvSp.jpg},{id:53176,name:John Gatins,character:Kingpin,order:10,cast_id:41,profile_path:/A2MqnSKVzOuBf8MVfNyve2h2LxJ.jpg},{id:1126350,name:Sophie Levy,character:Big Sister,order:11,cast_id:42,profile_path:null},{id:1126351,name:Tess Levy,character:Little Sister,order:12,cast_id:43,profile_path:null},{id:1126352,name:Charlie Levy,character:Littlest Sister,order:13,cast_id:44,profile_path:null},{id:187983,name:Gregory Sims,character:Bill Panner,order:14,cast_id:45,profile_path:null}],crew:[{id:58726,name:Leslie Bohem,department:Writing,job:Screenplay,profile_path:null},{id:53176,name:John Gatins,department:Writing,job:Screenplay,profile_path:/A2MqnSKVzOuBf8MVfNyve2h2LxJ.jpg},{id:17825,name:Shawn Levy,department:Directing,job:Director,profile_path:/7f2f8EXdlWsPYN0HPGcIlG21xU.jpg},{id:12415,name:Richard Matheson,department:Writing,job:Story,profile_path:null},{id:57113,name:Dan Gilroy,department:Writing,job:Story,profile_path:null},{id:25210,name:Jeremy Leven,department:Writing,job:Story,profile_path:null},{id:17825,name:Shawn Levy,department:Production,job:Producer,profile_path:/7f2f8EXdlWsPYN0HPGcIlG21xU.jpg},{id:34970,name:Susan Montford,department:Production,job:Producer,profile_path:/1XJt51Y9ciPhkHrAYE0j6Jsmgji.jpg},{id:3183,name:Don Murphy,department:Production,job:Producer,profile_path:null},{id:34967,name:Rick Benattar,department:Production,job:Producer,profile_path:null},{id:1126348,name:Eric Hedayat,department:Production,job:Producer,profile_path:null},{id:186721,name:Ron Ames,department:Production,job:Producer,profile_path:null},{id:10956,name:Josh McLaglen,department:Production,job:Executive Producer,profile_path:null},{id:57634,name:Mary McLaglen,department:Production,job:Executive Producer,profile_path:null},{id:23779,name:Jack Rapke,department:Production,job:Executive Producer,profile_path:null},{id:488,name:Steven Spielberg,department:Production,job:Executive Producer,profile_path:/cuIYdFbEe89PHpoiOS9tmo84ED2.jpg},{id:30,name:Steve Starkey,department:Production,job:Executive Producer,profile_path:null},{id:24,name:Robert Zemeckis,department:Production,job:Executive Producer,profile_path:/isCuZ9PWIOyXzdf3ihodXzjIumL.jpg},{id:531,name:Danny Elfman,department:Sound,job:Original Music Composer,profile_path:/pWacZpYPos8io22nEiim7d3wp2j.jpg},{id:18265,name:Mauro Fiore,department:Crew,job:Cinematography,profile_path:null},{id:54271,name:Dean Zimmerman,department:Editing,job:Editor,profile_path:null},{id:25365,name:Richard Hicks,department:Production,job:Casting,profile_path:null},{id:5490,name:David Rubin,department:Production,job:Casting,profile_path:null},{id:52088,name:Tom Meyer,department:Art,job:Production Design,profile_path:null}]} i have tried string.match(fixstrdirector,"name:(.+),department:Directing") but it gives me the from the first occurace it find the name to the end of thr string output: Hope Davis,character:Aunt Debra,order:5,cast_id:10,profile_path:/aIHF11Ss8P0A8JUfiWf8OHPVhOs.jpg},{id:53650,name:Anthony Mackie,character:Finn,order:3,cast_id:11,profile_path:/5VGGJ0Co8SC94iiedWb2o3C36T.jpg},{id:19034,name:Evangeline Lilly,character:Bailey Tallet,order:2,cast_id:12,profile_path:/oAOpJKgKEdW49jXrjvUcPcEQJb3.jpg},{id:6968,name:Hugh Jackman,character:Charlie Kenton,order:0,cast_id:13,profile_path:/wnl7esRbP3paALKn4bCr0k8qaFu.jpg},{id:79072,name:Kevin Durand,character:Ricky,order:4,cast_id:14,profile_path:/c95tTUjx5T0D0ROqTcINojpH6nB.jpg},{id:234479,name:Dakota Goyo,character:Max Kenton,order:1,cast_id:15,profile_path:/7PU6n4fhDuFwuwcYVyRNVEZE7ct.jpg},{id:8986,name:James Rebhorn,character:Marvin,order:6,cast_id:16,profile_path:/ezETMv0YM0Rg6YhKpu4vHuIY37D.jpg},{id:930729,name:Marco Ruggeri,character:Cliff,order:7,cast_id:17,profile_path:/1Ox63ukTd2yfOf1LVJOMXwmeQjO.jpg},{id:19860,name:Karl Yune,character:Tak Mashido,order:8,cast_id:18,profile_path:/qK315vPObCNdywdRN66971FtFez.jpg},{id:111206,name:Olga Fonda,character:Farra Lemkova,order:9,cast_id:19,profile_path:/j1qabOHf3Pf82f1lFpUmdF5XvSp.jpg},{id:53176,name:John Gatins,character:Kingpin,order:10,cast_id:41,profile_path:/A2MqnSKVzOuBf8MVfNyve2h2LxJ.jpg},{id:1126350,name:Sophie Levy,character:Big Sister,order:11,cast_id:42,profile_path:null},{id:1126351,name:Tess Levy,character:Little Sister,order:12,cast_id:43,profile_path:null},{id:1126352,name:Charlie Levy,character:Littlest Sister,order:13,cast_id:44,profile_path:null},{id:187983,name:Gregory Sims,character:Bill Panner,order:14,cast_id:45,profile_path:null}],crew:[{id:58726,name:Leslie Bohem,department:Writing,job:Screenplay,profile_path:null},{id:53176,name:John Gatins,department:Writing,job:Screenplay,profile_path:/A2MqnSKVzOuBf8MVfNyve2h2LxJ.jpg},{id:17825,name:Shawn Levy
You're searching from the first occurrence of "name:" until the "department:Directing" with everything in between. Instead, you need to restrict what can be between the two strings. Here for example I'm saying that the characters that make up the name can only be alphanumeric or a space: string.match(fixstrdirector,"name:([%w ]+),department:Directing") Alternatively, given that there's a comma separating the parameters, a better approach would be to search for "name:" followed by any characters other than a comma, followed by "department:Directing": string.match(fixstrdirector,"name:([^,]+),department:Directing") Of course that wouldn't work if the name had a comma it in!
Lua patterns provides - modifier for tasks as you have above. As stated on PiL - Section 20.2: The + modifier matches one or more characters of the original class. It will always get the longest sequence that matches the pattern. Like *, the modifier - also matches zero or more occurrences of characters of the original class. However, instead of matching the longest sequence, it matches the shortest one. Next, when you are using . to match, it'll find any and all characters satisfying the pattern. Therefore, you'll get the result from first occurence of name until the ,department:Directing is found. Since you know that it is a JSON data, you can try to match for [^,]; that is, non-comma characters. So, for your case try: local tAllNames = {} for sName in fixstrdirector:gmatch( "name:([^,]-),department:Directing" ) do tAllNames[ #tAllNames + 1 ] = sName end and all your required names will be stored in the table tAllNames. An example of the above can be seen at codepad.