I am trying to replace 5e0361f5af70f400441148 in https://place.mycompany.com/analyst/5e0361f5af70f40044112148/dbt to id. In the end should receive
https://place.mycompany.com/analyst/id/dbt
I use regexp_replace('https://place.mycompany.com/analyst/5e0361f5af70f400441148/dbt','[[:digit:]]','','g') but i do not know how to replace alphabet also
If that Id is always using lowercase a-f, the following will do:
SELECT regexp_replace('https://place.mycompany.com/analyst/5e0361f5af70f400441148/dbt' , '/[0-9,a-f]+/','/id/');
Otherwise add the A-F range: '/[0-9,a-f,A-F]+/'
This will break if other parts of your url contain [0-9,a-f] characters only.
If your input contains more than one id, add a 'g' modifier like so:
SELECT regexp_replace('https://place.mycompany.com/analyst/5e0361f5af70f40044112148/dbt/5e036445af70f40b1012b48f/top-chart', '/[0-9,a-f]+/','/id/','g');
Related
I am attempting to import a CSV into ADF however the file header is not the first line of the file. It is dynamic therefore I need to match it based on the first column (e.g "TestID,") which is a string.
Example Data (Header is on Line 4)
Date:,01/05/2022
Time:,00:30:25
Test Temperature:,25C
TestID,StartTime,EndTime,Result
TID12345-01,00:45:30,00:47:12,Pass
TID12345-02,00:46:50,00:49:12,Fail
TID12345-03,00:48:20,00:52:17,Pass
TID12345-04,00:49:12,00:49:45,Pass
TID12345-05,00:50:22,00:51:55,Fail
I found this article which addresses this issue however I am struggling to rewrite the expression from using an integer to using a string.
https://kromerbigdata.com/2019/09/28/adf-dynamic-skip-lines-find-data-with-variable-headers
First Expression
iif(!isNull(toInteger(left(toString(byPosition(1)),1))),toInteger(rownum),toInteger(0))
As the article states, this expression looks at the first character of each row and if it is an integer it will return the row number (rownum)
How do I perform this action for a string (e.g "TestID,")
Many Thanks
Jonny
I think you want to consider first line that starts with string as your header and preceding lines that starts with numbers should not be considered as header. You can use isNan function to check if the first character is Not a number(i.e. string) as seen in the below modified expression:
iif(isNan(left(toString(byPosition(1)),1))
,toInteger(rownum)
,toInteger(0)
)
Following is a breakdown of the above expression:
left(toString(byPosition(1)),1): gets first character fron left side of the first column.
isNan: checks if the character is "not a number".
iif: not a number, true then return rownum, false then return 0.
Or you can also use functions like isInteger() to check if the first character is an integer or not and perform actions accordingly.
Later on as explained in the cited article you need to find minimum rownum to skip.
Hope it helps.
I can replace underscores with spaces from the $1 like this ${1/[_]/ /g}
And the capitalization is done with ${1/(.*)/${1:/capitalize}/}
But how to combine both to get a result like this:
model_name --> Model Name?
Try:
"${1/([^_]*)(_?)/${1:/capitalize}${2:+ }/g}"
That will capture everything before an underscore in group 1 and capitalize that group.
The underscore, if any, will be in capture group 2. ${2:+ } is a conditional: if there is a capture group 2, add a space.
Note the global g at the end is necessary to repeatedly match each of these groups along the input.
It will work with any length input from:
model
model_name
model_name_more_stuff // etc.
I have a table column containing values which I would like to remove all the hyphens from. The values may contain more than one hyphen and vary in length.
Example: for all values I would like to replace 123 - ABCD - efghi with 123ABCDefghi.
What is the easiest way to remove all hyphens & update all column values in the table?
You can use the regexp_replace function to left only the digits and letters, like this:
update mytable
set myfield = regexp_replace(myfield, '[^\w]+','');
Which means that everything that is not a digit or a letter or an underline will be replaced by nothing (that includes -, space, dot, comma, etc).
If you want to also include the _ to be replaced (\w will leave it) you can change the regex to [^\w]+|_.
Or if you want to be strict with the characters that must be removed you use: [- ]+ in this case here a dash and a space.
Also as suggested by Luiz Signorelly you can use to replace all occurrences:
update mytable
set myfield = regexp_replace(myfield, '[^\w]+','','g');
You can use this.
update table
set column = format('%s%s', left(column, 3), right(column, -6));
Before:
After:
I have registration codes in my PostgreSQL table which are written messy, like MU-321-AB, MU/321/AB, MU 321-AB and so forth...
I would need to clear all of this to get MU321AB.
For this I uses following expression:
SELECT DISTINCT regexp_replace(ccode, '([^A-Za-z0-9])', ''), ...
This expression work as expected in 'NET' but not in PostgreSQL where it 'clears' only first occurrence of unwanted character.
How would I modify regular expression which will replace all unwanted chars in string to get clear code with only letters and numbers?
Use the global flag, but without any capture groups:
SELECT DISTINCT regexp_replace(ccode, '[^A-Za-z0-9]', '', 'g'), ...
Note that the global flag is part of the standard regular expression parser, so .NET is not following the standard in this case. Also, since you do not want anything extracted from the string - you just want to replace some characters - you should not use capture groups ().
I want to list the trigger no system ending with "_BI" in firebird database,
but no result with this
select * from rdb$triggers
where
rdb$trigger_source is not null
and (coalesce(rdb$system_flag,0) = 0)
and (rdb$trigger_source not starting with 'CHECK' )
and (rdb$trigger_name like '%BI')
but with this syntaxs it gives me a "_bi" and "_BI0U" and "_BI0U" ending result
and (rdb$trigger_name like '%BI%')
but with this syntaxs it gives me null result
and (rdb$trigger_name like '%#_BI')
thank you beforehand
The problem is that the Firebird system tables use CHAR(31) for object names, this means that they are padded with spaces up to the declared length. As a result, use of like '%BI') will not yield results, unless BI are the 30th and 31st character.
There are several solutions
For example you can trim the name before checking
trim(rdb$trigger_name) like '%BI'
or you can require that the name is followed by at least one space
rdb$trigger_name || ' ' like '%BI %'
On a related note, if you want to check if your trigger name ends in _BI, then you should also include the underscore in your condition. And as an underscore in like is a single character matcher, you need to escape it:
trim(rdb$trigger_name) like '%\_BI' escape '\'
Alternatively you could also try to use a regular expressions, as you won't need to trim or otherwise mangle the lefthand side of the expression:
rdb$trigger_name similar to '%\_BI[[:SPACE:]]*' escape '\'