What is the Substitute function for CONTAINS function in QUICK SIGHT. Or any function that can be used in the backend (in Redshift).
I think you are looking for locate. If a substring is not contained in a string, locate returns 0
locate(expression, substring, start)
Documentation
Related
I have, in a database, records that are serialized PHP strings that I must obfuscate emails if there are any. The simplest record is like {s:20:"pika.chu#pokemon.com"}. It is basically saying: this is a string of length 20 which is pika.chu#pokemon.com. This field can be kilobytes long with lot of emails (or none) and sometimes it is empty.
I wish I could use a SQL regular expression function to obfuscate the user part of the email while preserving the length of the string in order not to break the PHP serialization. The example email above shall be turned into {s:20:"xxxxxxxx#pokemon.com"} where the number of x matches the length of pika.chu.
Any thoughts?
Here is a more complete example of what can be found as serialized PHP:
a:4:{s:7:"locales";a:3:{i:0;s:5:"fr_FR";i:1;s:5:"de_DE";i:2;s:5:"en_US";}s:9:"publisher";s:18:"john#something.com";s:7:"authors";a:2:{i:0;s:21:"william#something.com";i:1;s:19:"debbie#software.org";}s:12:"published_at";O:8:"DateTime":3:{s:4:"date";s:26:"2022-01-26 13:05:26.531289";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";}}
I tried to do it using native functions but it not worked because functions like REGEXP_REPLACE don't let you manipulate the match to get the size of it, for example.
Instead, I've created a UDF to do that:
CREATE TEMP FUNCTION hideEmail(str STRING)
RETURNS STRING
LANGUAGE js AS """
return str
.replace(/([a-zA-Z.0-9_\\+-:]*)#/g, function(txt){return '*'.repeat(txt.length-1)+"#";})
""";
select hideEmail('a:4:{s:7:"locales";a:3:{i:0;s:5:"fr_FR";i:1;s:5:"de_DE";i:2;s:5:"en_US";}s:9:"publisher";s:18:"john#something.com";s:7:"authors";a:2:{i:0;s:21:"william#something.com";i:1;s:19:"debbie#software.org";}s:12:"published_at";O:8:"DateTime":3:{s:4:"date";s:26:"2022-01-26 13:05:26.531289";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";}}')
Result:
a:4:{s:7:"locales";a:3:{i:0;s:5:"fr_FR";i:1;s:5:"de_DE";i:2;s:5:"en_US";}s:9:"publisher";s:18:"****#something.com";s:7:"authors";a:2:{i:0;s:21:"*******#something.com";i:1;s:19:"******#software.org";}s:12:"published_at";O:8:"DateTime":3:{s:4:"date";s:26:"2022-01-26 13:05:26.531289";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";}}
I have the following function to convert any string to title case:
CREATE OR REPLACE FUNCTION udf.title_case(str STRING)
RETURNS STRING
LANGUAGE js AS """
return str
.replace(/([^\\W_]+[^\\s-]*) */g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();})
""";
UPDATE:
I fixed chartAt to charAt and still get the same error
and it produces the following error:
"project.dataset.charAt" is not a function at UDF$1(STRING) line 3, columns 110-111
I can bypass this error by using [] notation which is not ideal however I hit the same error with substr.
I normally test my functions in JSBin or similar and works fine but when translate it to Bigquery I need to escape \ in regex and then deal with these out of the blue errors.
Makes life harder for those who are not experienced in the arts of JS programming.
Thanks in advance for your help.
Consider use of INITCAP function instead of JS UDF
It takes a STRING and returns it with the first character in each word in uppercase and all other characters in lowercase
for example
SELECT INITCAP('I have the following function to convert any string to title case:')
produces below output
You write the wrong function name, "char[t]At" instead of "charAt".
I use temp function to test
CREATE TEMP FUNCTION tempFunc(str STRING)
RETURNS STRING
LANGUAGE js AS """
return str
.replace(/([^\\W_]+[^\\s-]*) */g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();})
""";
select tempFunc("abce")
and the result is "Abce".
You can try this query in your bigquery editor
I have strings which contain numbers like that:
a20cdac0_19221bdc12022bab3fe05a43df4a7dbe
I need to get only symbols after underscore symbol:
19221bdc12022bab3fe05a43df4a7dbe
Unfortunately, the amount of those symbols is always different, so I can't use just RIGHT function.
I know that probably REGEXP might help, but I can't understand how to use that exactly. Will be very grateful for the help.
Below is for BigQuery Standard SQL (using regexp)
regexp_extract(value, r'_(.*)') regexp_approach
if to apply to sample value from your question
regexp_extract('a20cdac0_19221bdc12022bab3fe05a43df4a7dbe', r'_(.*)') regexp_approach
result is
Yet, another regexp option is to use regexp_replace as in below example
regexp_replace(value, r'^.*?_', '')
Note: using split in this case is also an option unless you have more than one _ in which case you will get part between first and second _
split(value, '_')[safe_offset(1)]
Also, as you can see you need to use safe to prevent error in cases when _ is absent
You can use the split function like this
select split('a20cdac0_19221bdc12022bab3fe05a43df4a7dbe','_')[ORDINAL(2)];
https://cloud.google.com/bigquery/docs/reference/standard-sql/string_functions#split
I have used ends-with function as
(By.xpath("//input[ends-with(#id,'_PHONE$']"));
But it didnot work
The ends-with function is part of XPath 2.0 but browsers generally only support 1.0.
So, if you strictly want to fetch all elements that ends with a specific pattern, you can either fetch all elements that contain that pattern (using the contains() function) and then strictly check the suffix of the id (fetch the attribute value by .getAttribute("id")) using Java's .endsWith() method.
Or
You can use string-length function, substring function and equals to get this XPath:
"//input[substring(#id, string-length(#id) - string-length('_PHONE$1') +1) = '_PHONE$1']"
driver.findElement(By.xpath("//id[contains(text(),'PHONE$')]"));
You can use below XPath as well:-
//input[contains(#id,'PHONE$1')]
Hope it will help you :)
Your id is ending with _PHONE$1 and not _PHONE$. Notice that there is a 1 at the end.
(By.xpath("//input[ends-with(#id,'_PHONE$1']"));
If you still don't want to match that 1 use contains.
By.xpath("//input[contains(#id,'_PHONE$1']"));
Use contains Method
By.xpath("//input[contains(text(), '_PHONE$']");
Or use wait explicit method
I have created a calculated column which creates a URL which calls a Column called "Pitch Name" and from that will create the URL but I have to remove the space/spaces between words from pitch which needs to be displayed as a URL. Example of this is "Coca Cola" needs to be "CocaCola" so the site name can be"http://sitename.com/CocaCola". I've tried using the TRIM function but that doesn't work. I have tried the REPLACE function aswell but that seems not to work either. Any Solutions?
(Taken from an attempt to edit an answer:)
Update: The REPLACE and FIND Functions work but only if the string contains the amount of the desired character. And if the character doesn't appear the same amount of times the formula has asked for you will always get the string #VALUE which means it hasn't worked
SharePoint Calculated Fields use Excel functions, not .NET functions, so you probably want to use SUBSTITUTE rather than REPLACE.
Try something like this:
=SUBSTITUTE([Pitch Name], " ", "")
Update:
According to Xue-Mei Chang in Calculated Field Error:
The "SUBSTITUTE" function is not available in SharePoint.
As a workaround, he proposes:
For the "SUBSTITUTE" function, you have to use a combination of the "REPLACE" function with a "FIND" function. The "FIND" would return the position of the desired character in the string, which would then pass on to the "REPLACE" function so it can replace it with the new character.