Ignore numeric values in string, Presto - presto

I have a column in a database which is all postcodes. I want to use that column to get statistical data about specific regions. To do this, I want to extract just the first non numeric characters of the postcode (B for Birmingham, BT for Belfast).
I can see solutions in other SQL formats using a CASE WHEN with ISNUMERIC but that function doesn't work in Presto. Are there any solutions to this?
As always, any advice would be greatly appreciated.
Many thanks,
Barry

I think you'll want to look at using regular expression functions to either extract the non-numeric characters using regexp_extract or replace all non-numeric characters using regex_replace.
See https://prestodb.io/docs/current/functions/regexp.html

I managed to get around this using
select concat(substr(postcode,1,1), case when substr(postcode,2,1) in ('1','2','3','4','5','6','7','8','9','0') then '' else substr(postcode,2,1) end)

Related

excel, check if string contains a character a-z or A-Z

I have a column with unstructured data. I need to detect if the string has an alphabetical character meaning a-z or A-Z. I am not sure how to do this in excel with a formula or other. I am thinking this could be a long countif and sumproduct. Or maybe regex in excel. I will post an attempt try once I try this out more. But I am looking for some advice. While searching online, I could not find a formula to do this. Thanks for your help. Please see below screenshot for sample data.
Note that SEARCH is case-insensitive, so you can use:
=IF(OR(ISNUMBER(SEARCH({"A";"B";"C";"D";"E";"F";"G";"H";"I";"J";"K";"L";"M";"N";"O";"P";"Q";"R";"S";"T";"U";"V";"W";"X";"Y";"Z"},A8))),"Yes","No")
If you have a later version of O365 with the SEQUENCE function, you can use:
=IF(OR(ISNUMBER(SEARCH(CHAR(SEQUENCE(26,,65,1)),A1))),"Yes","No")
Using the fact that a to z is 99 to 122 in ASCII, you can use SEARCH, which is case-insensitive:
=IF(SUMPRODUCT(--ISNUMBER(SEARCH(CHAR(ROW($99:$122)),A2)))>0,"Yes","No")
Note that inserting or deleting rows will change the $99:$122 so only use this approach if you can ensure that it remains unchanged.
This is not case sensitive so it will return Yes for a|A
=IF(SUMPRODUCT(--ISNUMBER(SEARCH(MID(A1,ROW(INDIRECT("A$1:A"&LEN(A1))),1),"abcdefghijklmnopqrstuvwxyz"))),"Yes","No")

Attempting to extract one of two possible words from a text string if matches a criteria, or input the string in it's entirety

I'm trying to bring back one of two possible words (Local or National) from a text string, and if neither of these words are in the text string, then bring back the string in the whole cell
The issue I have is that I can bring back either word when they appear, but I get an error when they do not
I'm currently using
=IFERROR(IF(SEARCH("*local*",B2,1),"Local"),IF(SEARCH("*national*",B2,1),"National"))
However this obviously this doesn't bring back if the words not exist
I'm sure it's easy and I'm missing something, but I just cannot figure it out. any help would be great
Cheers all
You can use:
Formula in B1:
=IF(ISNUMBER(SEARCH("*local*",A1)),"Local",IF(ISNUMBER(SEARCH("*national*",A1)),"national",A1))
Drag down
Note:
Notice that your wildcards make it that even a string with 'international' in it will return 'national'. If this is not what you want, you should remove the wildcards.
You can also use INDEX/AGGREGATE:
=IFERROR(INDEX({"local","national"},AGGREGATE(15,7,ROW($1:$2)/(ISNUMBER(SEARCH({"local";"national"},A1))),1)),A1)
This will allow one to replace the both hard coded arrays with a range of cells that contain the outputs. If Local and National were in D1:D2 then you can use:
=IFERROR(INDEX($D:$D,AGGREGATE(15,7,ROW($D$1:$D$2)/(ISNUMBER(SEARCH($D$1:$D$2,A1))),1)),A1)
That way if the list gets bigger the formula does not.
I'd suggest regular expressions.
=IF(REGEXMATCH(A2,"(Local|National)"),REGEXEXTRACT(A2,"(Local|National)"),A2)

Finding question marks in cell string

I would like to find out if a string within a cell in Excel 2010 contains any of the following, and then return a '1'.
?dementia
? dementia
dementia?
dementia ?
I've tried some formulas but they don't seem able to get past the use of the wildcard and the string when combined.
Would anyone have any pointers or advice?
Here is a combination of the suggested answers, and my own work around:
This is the formula you need:
=IF(IFERROR(FIND(A2,$C$3),0)>0,1,0)
where A2 is the string that you are trying to check.
Assuming you don't want to include word ? word and return 1, you can use something like this:
=IF(OR(LEFT(A1,1)="?",RIGHT(A1,1)="?"),1,"")
You will want to use the FIND function (Documentation).
Make sure your search term in in quotation marks.
If you are still having trouble with wildcards, you can try the CHAR function (Documentation). It will return the character as a string. In your case, to get a ?, you would use CHAR(63). I use this chart to keep track of CHAR codes (just use the number and ignore the "Alt")
REVISED:
If I understand the question correctly, you are trying to search long string in a short string list.
The formula you can use is:
=ISTEXT(LOOKUP(99^99,SEARCH(SUBSTITUTE($A$2:$A$5,"?","|"),SUBSTITUTE(C2,"?","|")),$A$2:$A$5))*1
The problem with my prior formula is due to that ? mark because it is being treated as a wild card. Here I have replace it with | (you can change this to a character that you will never use but avoid ? and *). This should work for you now.

How to remove 1 character from a column in excel

I have a column in my Excel file which contains different values. All these values begin with a letter and continues with a number.
I would like to know how to remove that letter and obtain only the number. Keep in mind that each value can have a different length.
Thanks
You can use something like this
=MID(A1,2,LEN(A1)-1)
you could do it with
=SUBSTITUTE(A1,LEFT(A1),"")
or
=RIGHT(A1,LEN(A1)-1)
There are countless ways to do that...
use simple function in excel itself
=left(cellname,no. of charcters)
=left(A1,8) will give only first left characters and same can be used right/mid

Sharepoint: Calculated Column replace all spaces

Seems like it would be a simple thing really (and it may be), but I'm trying to take the string data of a column and then through a calculated column, replace all the spaces with %20's so that the HTML link in the workflow produced email will actually not break off at the first space.
For example, we have this in our source column:
file:///Z:/data/This is our report.rpt
And would like to end up with this in the calculated column:
file:///Z:/data/This%20is%20our%20report.rpt
Already used the REPLACE, and made up a ghastly super nested REPLACE/SEARCH version, but the problem there is that you have to nest for EACH potential space, and if you don't know how many up front, it doesn't work, or will miss some.
Have any of you come across this scenario and how did you handle it?
Thanks in advance!
As far as I know there is no generic solution using the calculated-column syntax. The standard solution for this situation is using an ItemAdded (/ItemUpdated) event and initializing the field value from code.
I was able to solve this issue for my circumstances by using a series of calculated columns.
In the first calculated column (C1) I entered a formula to remove the first space, something like this:
=IF(ISNUMBER(FIND(" ",[Title])),REPLACE([Title],FIND(" ",[Title]),1,"%20"),[Title])
In the second Calculated column (C2) I used:
=IF(ISNUMBER(FIND(" ",[C1])),REPLACE([C1],FIND(" ",[C1]),1,"%20"),[C1]).
In my case, I wanted to encode upto four spaces, so I used 3 calculated columns (C1, C2, C3) in the same fashion and got the desired result.
This is not as efficient as using a single calculated column, but if SUBSTITUTE will not work in your SharePoint environment, and you cannot use an event handler or workflow, it may offer a workable alternative.
I actually used a slightly different formula, but it was on a work machine to which I don't have access at the moment, so I just grabbed this formula from a similar S.O. question. Any formula that will replace the first occurrence of a space with "%20" will work, the trick is to a) make sure the formula returns the original string unchanged if it does not have more spaces in it, and b) test, test, test. Create a view of your list that has the field you are trying to encode, plus the calculated fields, and see if you are getting the results you want.
so that the HTML link in the workflow produced email will actually not break off at the first space.
The browser only does this if you have not enclosed your link in quotes
If you wrap the link in quotes, it does not cut off at the first space
In a SharePoint Formula it would be:
="""file:///Z:/data/This is our report.rpt"""
becuase two quotes are the SP escape notation to output a quote
You can use this formula (Start trim for 1, in my case was 4):
=IF(ISBLANK([EUR Amount]),"",(TRIM(MID([EUR Amount],4,2))&TRIM(MID([EUR Amount],6,2))&TRIM(MID([EUR Amount],8,2))&TRIM(MID([EUR Amount],10,2))&TRIM(MID([EUR Amount],12,2))&TRIM(MID([EUR Amount],14,2)))*1)

Resources