I have string column "49b8b35e-b62c-4a42-9d73-192d131d127a,03c8a7e0-5153-11ec-873a-0242ac11000a,eec8aee4-0500-4940-b319-15924cc2d248"
this string column has 3 values separate by ",". (value1,value2,value3). there is no guarantees that vaule2 and value3 need to be present always. but value1 does always.
so i need to split this single column into 3 different columns keeping the above conditions of their existence in mind and they need to be separated by
before "," into a new column.
i have so far wrote as
select regexp_extract('49b8b35e-b62c-4a42-9d73-192d131d127a,03c8a7e0-5153-11ec-873a-0242ac11000a,eec8aee4-0500-4940-b319-15924cc2d248', '^(.+?),') as value1
but after that my logic and thinking giving errors and no luck to me.
You can use split() function, it returns array and you can access elements using array index []:
select split("49b8b35e-b62c-4a42-9d73-192d131d127a,03c8a7e0-5153-11ec-873a-0242ac11000a,eec8aee4-0500-4940-b319-15924cc2d248",',')[0] --to get first element
Use [0] for first element, [1] for second element and [2] for third, If some elements are absent and no commas (for example split() returned array of size=1 and you want 2nd element) it will return NULL, if commas exist but empty string in between them, it will return empty, so adjust your logic accordingly.
And if you prefer regexp_extract function:
regexp_extract('49b8b35e-b62c-4a42-9d73-192d131d127a,03c8a7e0-5153-11ec-873a-0242ac11000a,eec8aee4-0500-4940-b319-15924cc2d248', '^([^,]+),*([^,]*),*([^,]*)',1) as value1,
regexp_extract('49b8b35e-b62c-4a42-9d73-192d131d127a,03c8a7e0-5153-11ec-873a-0242ac11000a,eec8aee4-0500-4940-b319-15924cc2d248', '^([^,]+),*([^,]*),*([^,]*)',2) as value2,
regexp_extract('49b8b35e-b62c-4a42-9d73-192d131d127a,03c8a7e0-5153-11ec-873a-0242ac11000a,eec8aee4-0500-4940-b319-15924cc2d248', '^([^,]+),*([^,]*),*([^,]*)',3) as value3
The same pattern is used, only the group number is different. If element is absent, regexp_extract returns empty string.
Related
I need some help with the next. I have a field text in SQL, this record a list of times sepparates with '|'. For example
'14613|15474|3832|148|5236|5348|1055|524' Each value is a time in milliseconds. This field could any length, for example is perfect correct '3215|2654' or '4565' (only 1 value). I need get this field and replace all number with -1000 value.
So '14613|15474|3832|148|5236|5348|1055|524' will be '-1000|-1000|-1000|-1000|-1000|-1000|-1000|-1000'
Or '3215|2654' => '-1000|-1000' Or '4565' => '-1000'.
I try use regexp_replace(times_field,'[[:digit:]]','-1000','g') but it replace each digit, not the complete number, so in this example:
'3215|2654' than must be '-1000|-1000', i get:
'-1000-1000-1000-1000|-1000-1000-1000-1000', I try with other combinations and more options of regexp but i'm done.
Please need your help, thanks!!!.
We can try using REGEXP_REPLACE here:
UPDATE yourTable
SET times_field = REGEXP_REPLACE(times_field, '\y[0-9]+\y', '-1000', 'g');
If instead you don't really want to alter your data but rather just view your data this way, then use a select:
SELECT
times_field,
REGEXP_REPLACE(times_field, '\y[0-9]+\y', '-1000', 'g') AS times_field_replace
FROM yourTable;
Note that in either case we pass g as the fourtb parameter to REGEXP_REPLACE to do a global replacement of all pipe separated numbers.
[[:digit:]] - matches a digit [0-9]
+ Quantifier - matches between one and unlimited times, as many times as possible
your regexp must look like
regexp_replace(times_field,'[[:digit:]]+','-1000','g')
I have 10 columns and their values could be either null, or a name of a fruit.
I would like to add another column with all the fruits that every row has. I have used Concat(column1 , column2,..., column10) as name.
Issue : There are no commas coming on the result and if I add the comma before concatenating, we are having them together, the last word is also a comma.
Any ideas?
Thanks!
You can use the standard concatenation (||) in conjunciton with COALESCE function, which returns the value of the first non-null argument.
Example:
select coalesce(column1||',', '')||coalesce(column2||',', '')|| ... ||coalesce(column10||, '');
Given this data:
col1
----
foo
bar
I want concatenate the rows together, and end up with 'foo','bar'.
Using collect_set gets me an array, concat_ws gets me a comma separated string.
select
concat_ws(',',collect_set(col1))
I cannot figure out how to get the single quotes in there.
concat('''',col1,'''') just returns the value of col1.
What am I doing wrong?
You need to escape the quotes.
concat('\'',col1,'\'')
How can the matched regular expression be returned from an SQL select? I tried using REGEXP_EXTRACT with no luck (function not available). What I've done that does work is this:
SELECT column ~ '^stuff.*$'
FROM table;
but this gives me a list of true / false. I want to know what is extracted in each case.
If you're trying to capture the regex match that resulted from the expression, then substring would do the trick:
select substring ('I have a dog', 'd[aeiou]g')
Would return any match, in this case "dog."
I think the missing link of what you were trying above was that you need to put the expression you want to capture in parentheses. regexp_matches would work in this case (had you included parentheses around the expression you wanted to capture), but would return an array of text with each match. If it's one match, substring is sort of convenient.
So, circling back to your example, if you're trying to return stuff if and only if it's at the beginning of a column:
select substring (column, '^(stuff)')
or
select (regexp_matches (column, '^(stuff)'))[1]
Use regexp_matches.
SELECT regexp_matches(column,'^stuff.*$')
FROM table
The regexp_matches function returns a text array of all of the captured substrings resulting from matching a POSIX regular expression pattern. It has the syntax regexp_matches(string, pattern [, flags ]). The function can return no rows, one row, or multiple rows (see the g flag below). If the pattern does not match, the function returns no rows. If the pattern contains no parenthesized subexpressions, then each row returned is a single-element text array containing the substring matching the whole pattern. If the pattern contains parenthesized subexpressions, the function returns a text array whose n'th element is the substring matching the n'th parenthesized subexpression of the pattern (not counting "non-capturing" parentheses; see below for details). The flags parameter is an optional text string containing zero or more single-letter flags that change the function's behavior. Flag g causes the function to find each match in the string, not only the first one, and return a row for each such match.
I'm using Amazon Redshift which uses PostgreSQL 8.0.2 (I should have mentioned this in the question). For me what worked was REGEXP_SUBSTR
e.g.
SELECT REGEXP_SUBSTR(column,'^stuff.*$')
FROM table
jkdfhdjfhjh&name=ijkjkjkjkjkjk&id=kdjkjkjkjkjkjjjd&class=kdfjjfjdhfjhf
The above string has some characters starting with & and ending with =
for example we have &name= and I just need this from the above string.
similarly I need &id=, &class=
I need the output under a single column.
Final Extract
----------------------
&id=, &class=, &name=
can anyone help me out in writing a query for this.
You could try this :
select regexp_replace('jkdfhdjfhjh&name=ijkjkjkjkjkjk&id=kdjkjkjkjkjkjjjd&class=kdfjjfjdhfjhf', '\\w*?(&.*?=)\\w+((?=&)|$)', '\\1, ', 'g');
result:
regexp_replace
-------------------------
&name=, &id=, &class=,
Then it's up to you to remove the last ,.
The regexp_replace function is available in version 8.1 and after.
If you want the values along with each variable, I would implement this by splitting on "&" into an array and then taking a slice of the desired elements:
SELECT (string_to_array('jkdfhdjfhjh&name=ijkjkjkjkjkjk&id=kdjkjkjkjkjkjjjd&class=kdfjjfjdhfjhf','&'))[2:4];
Output in PostgreSQL 8.4 (array type):
{name=ijkjkjkjkjkjk,id=kdjkjkjkjkjkjjjd,class=kdfjjfjdhfjhf}
The example string is very wide so here's the general form to show the array slicing more clearly:
SELECT ((string_to_array(input_field,'&'))[2:4];
NOTE: You must have the extra parentheses around the string_to_array() call in order for the array slicing to work--you'll get an error otherwise.