Table in SQLITE, want to simple way to delete everything to a right of a set phrase/character in the Company_name_ column, in this case everything after "LLC":
Company_name_
Example LLC $42
Example llc,klp
Example LLc jim
becomes
Company_name_
Example LLC
Example llc
Example LLc
Tried Set Charindex and Substr but getting syntax errors.
Thank you
You can do it with string functions SUBSTR() and INSTR().
If you want a SELECT query use a CASE expression with the operator LIKE to check if the column value contains 'LLC' or not:
SELECT CASE
WHEN Company_name_ LIKE '%LLC%'
THEN SUBSTR(
Company_name_,
1,
INSTR(UPPER(Company_name_), 'LLC') + LENGTH('LLC') - 1
)
ELSE Company_name_
END Company_name_
FROM tablename;
If you want to update the table:
UPDATE tablename
SET Company_name_ = SUBSTR(
Company_name_,
1,
INSTR(UPPER(Company_name_), 'LLC') + LENGTH('LLC') - 1
)
WHERE Company_name_ LIKE '%LLC%';
See the demo.
Related
I am creating a Product Decoder for a project.
Lets say, Our product can have a code such as "ABCDE" OR a code like "BCDEF".
ABCDE has a table of data that I use to decode using a lookup. For example AB can decode into "Potato" and CDE can decode into "Chip". So any combination with AB can be Potato "Anything else".
BCDER, BC can decode into "Veggie" so DER can code into "Chip".
I also use the 1/search method to take placements for the decode. Example =IFERROR(LOOKUP(2,1/SEARCH($E$19:$E$23,N18),$E$19:$E$23), "")
I concatenate all the decodes using =S2&" "&T2&" "&U2&" "&V2
Question is...if we are getting a huge amount of product code coming that I want to decode into one single column... How do I tell excel to use this table of data for ABCDE if product starts with "A", if not, use table of data that correlates to BCDER when product starts with "B".
Edit 1:
Here is my table, right side is where i look up the Part Number column N"
As you can see on column "W" I concatenate the date is Look up from columns O~V.
Column O Function: =IFERROR(LOOKUP(2,1/SEARCH($C$1:$C$7,N2),$C$1:$C$7), "")
On column N, I have two parts. One that starts with M and one that starts with K which is pretty standard.
Image two is me trying to use the IF Left but, it doesn't really work
=IF(LEFT(AA4,10) = "M ", W2, W18)
So How can I tell my excel page to use Table A1:A12 if part starts with "M*" and vice versa?
Let me know if this is confusing, I will do my best to clear things up.
First, a possible correction
I think this function does not give you what you say it does:
= IFERROR(LOOKUP(2,1/SEARCH($E$19:$E$23,N18),$E$19:$E$23), "")
You might mean:
= IFERROR( LOOKUP( 2, 1/SEARCH( $E$19:$E$23, N18 ), $F$19:$F$23 ), "" )
Because you want to look up the value in column E and return the value in column F. If that's not true, then skip the rest of this answer.
Now the solution
What you're trying to do is change the lookup array if the part number starts with a different letter. So, the IF( LEFT( combo mentioned by #BigBen should be used to modify the lookup array. I think it would look like this:
= IFERROR( LOOKUP( 2
,1/SEARCH( IF( LEFT( AA4, 1 ) = "M"
,$C$2:$C$12
,$C$19:$C$23 )
,N2 )
,IF( LEFT( AA4, 1 ) = "M"
,$D$2:$D$12
,$D$19:$D$23 )
)
,"")
I am looking for a way to retrieve always the 2 numbers after the last "/"character in a string. If there is something after the 2 numbers that I want, I don't care.
the code that I came up with is this:
CASE when INSTRUMENT like '%/%' then SUBSTR(INSTRUMENT,INSTR(INSTRUMENT,'/',-3,1)+1,2) else '0' end
This seems to work fine.
The problem is that when it does not find any "/"character, then it does not fill them with a 0 as I would like.
To give you an example of what I would like to perform:
XXX/YYY/ZZZ/92 ---> Returns 92
XXX/YYY/ZZZ/42 (test) ---> Returns 42
XXX YYY ZZZ 10 ---> Returns 0
All of this must be "plugged" in a select case statement, but this should not change the solution.
Thanks in advance
Perhaps regexp_substr will work for you. If it returns NULL, meaning the pattern of a slash followed by 2 digits and 0 or more characters to the end of the string was not found, replace with a '0' by using NVL().
SQL> with tbl(str) as (
select 'XXX/YYY/ZZZ/92' from dual union
select 'XXX/YYY/ZZZ/42 (test)' from dual union
select 'XXX YYY ZZZ 10' from dual
)
select nvl(regexp_substr(str, '/(\d{2}).*$', 1, 1, NULL, 1), '0') digits
from tbl;
DIGITS
---------------------
0
42
92
SQL>
I'm trying to pull data from a column in a table I have called Vendors and the column name is VendorContactLName. I also have a column VendorContactFName. I'm trying to make it so as I pull the data, it conditionally changes the output of the string.
For example, I want to add an apostrophe s <'s> to the end of the name which I can do with:
SELECT VendorContactFName + ' ' + VendorContactLName + '''s' AS 'Vendor Full Name' FROM Vendors;
However, I'd like to make it so that if it happens to be that the last name in VendorContactLName already ends with an "s" that I only concatenate an apostrophe to it, not apostrophe and an 's'.
So, I'd like: Karl Allen, Michael Dunlap, and Michelle Higgins to come out as Karl Allen's, Michael Dunlap's, and Michelle Higgins'.
Thanks for the help.
Check this:
SELECT VendorContactFName + ' ' +
(case right(VendorContactLName,1)
when 's' then VendorContactLName + ''''
else VendorContactLName + '''s'
end) as 'Vendor Full Name'
FROM Vendors
demo here
Hi this is my first post and I'm also new to sql. I am trying to extract text from within a string
I have a table column that looks like this
site - abc - left
site - def - left
site - ghi - right - inner
site - jkl - right - inner
site - mno
site - pqr
I need a query that would return text inbetween the first two '-' but as per example some only have one '-'.
For example.
abc
def
ghi
jkl
mno
pqr
Any help greatfully accepted
The code I have been working with only gives me the first column 'site'.
SELECT SUBSTR(site.description,1,instr(site.description,'-',1,1)-1) AS loc
FROM table
Suppose your data resides in a tabled named test_n with only column val with above values as mentioned in your question, the query is:
select val
, instr(val, '-', 1,1) + 1 START_POS
, instr(val, '-',1,2) END_POS
, substr(val, instr(val, '-', 1,1) + 1, decode(instr(val, '-',1,2),0,length(val)+1,instr(val, '-',1,2) ) - instr(val, '-', 1,1)-1 ) result
FROM test_n;
I have not checked yet, but i think this would work for you
substr(var1,1,instr(site.description,'-',1,1)+1-instr(site.description,'-',1,2)-1)
it should work,if it did not just need a little change.let me know the result
I have just a hair over 30,000 tweets. I have one column that has the actual tweet. There are two things that I would like to accomplish with this column.
First here is a snippet of sample data:
RT #Just_Sports: Cool page for fans of early pro #baseball. https://t.co/QCMYFQNSq8 #mlb #vintage #Chicago #Detroit #Boston #Brooklyn #Phil…
#brettjuliano you already know #unity #newengland #hiphop #boston #watertown #network
I have a column that uses the following formula to see if the message starts out with RT meaning a re-tweet. It returns 1 for yes and 0 for no.
What I would like to accomplish is to create a formula in two columns. One that will get the username if the RT column has a value of 1 and in the second column the username if the RT column has a value of 0. Since usernames are of arbitrary length I am unsure of how to go about this.
Example
RT #Just_Sports: | 1 | #Just_Sports | 0
#brettjuliano | 0 | | #brettjuliano
Take a look at Excel's FIND function. You can use this to identify the position of the #, then using a specified delimiter, match the end of the user name:
=MID(A1, FIND("#",A1), FIND(":",A1,FIND("#",A1)) - FIND("#",A1))
Where A1 is the cell containing the tweet, and ":" is your delimiter.
You can use the same feature to check for the existence of the "RT" identifier.
=FIND("RT",A1)>0
Which returns TRUE if "RT" is found. You may want to consider a search for " RT " (spaces), or some other variation, since there is no standard for using this in a tweet:
=OR(FIND("RT",A1)>0,FIND(" RT",A1)>0,FIND("RT ",A1)>0, FIND(" RT ",A1)>0)
But beware of false positives: ART, START, ARTOO, etc...
Additionally, your "RT" may be lower/upper/mixed case, in which case you'll want to normalize that search:
=OR(FIND("RT",UPPER(A1))>0,FIND(" RT",UPPER(A1))>0,FIND("RT ",UPPER(A1))>0, FIND(" RT ",UPPER(A1))>0)
My OR check is different than the 0/1 check you say you already have, so you can jsut add IF to that to convert to the 0/1 as needed:
=IF(OR(FIND("RT",A1)>0,FIND(" RT",A1)>0,FIND("RT ",A1)>0, FIND(" RT ",A1)>0),1,0)
Once you know you have the RT check correct, and your second column is filled properly, you can add to my original formula:
Case for 1 in 2nd column:
=IF(B1=1,MID(A1, FIND("#",A1), FIND(":",A1,FIND("#",A1)) - FIND("#",A1)),"")
Case for 0 in 2nd column:
=IF(B1=0,MID(A1, FIND("#",A1), FIND(":",A1,FIND("#",A1)) - FIND("#",A1)),"")