I'm use the query function, and what I want is to combine columns A, B, and C into a string with hyphens separating them. i.e. "2-4-6".
A|B|C
-|-|-
2|4|6
How could I do this?
I've tried the following but it returns an error:
=query(raw!A1:C, "select 'A & '-' & B & '-' &C")
try:
=ARRAYFORMULA(SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(raw!A1:C),,999^99))), " ", "-"))
or use:
=ARRAYFORMULA(raw!A:A&IF(raw!B:B="",,"-"&raw!B:B)&IF(raw!C:C="",,"-"&raw!C:C))
Related
I want to combine multiple results of if statement into a form of a sentence.
Code:
=CONCAT("Fail column", IF($T3="No", " T", "")& IF($U3="No", ", U", "") & IF($W3<7, ", W", "") & IF($X3>3, ", X", "") & IF($AE3="No", ", AE", "") & IF($AF3="No", ", AF", ""))
Sample data :
If the first statement returns blank, the next statement would not show the comma at the beginning. And let say all pass, they would be shown as "Yes".
My expected output can be:
Fail column T, U, W, X, AE, AF
Fail column U, W, X, AE, AF
Fail column T
Fail column W, X
Yes
I'm thinking you could try:
Formula in R3:
=IF(OR(T3="No",U3="No",W3<7,X3>3,AE3="No",AF3="No"),"Fail column: "&TEXTJOIN(", ",TRUE,IF(T3="No","T",""),IF(U3="No","U",""),IF(W3<7,"W",""),IF(X3>3,"X",""),IF(AE3="No","AE",""),IF(AF3="No","AF","")),"Yes")
The key here is TEXTJOIN instead of CONCAT to exclude any empty values from the concatenated string.
Note: Excel and Google Spreadsheets are two different apps and the functions are not always exchangeable. Your question's title suggests that you are actually using Excel, however your tags include GS.
correct formula would be:
=ARRAYFORMULA(REGEXREPLACE(IF(
(T3:T="yes")*(U3:U="yes")*((W3:W<7)*(W3:W<>""))*(X3:X>3)*(AE3:AE="yes")*(AF3:AF="yes"),
"yes", "Fail column: "&
IF(T3:T="no", "T, ", )&
IF(U3:U="no", "U, ", )&
iF(W3:W>=7, "W, ", )&
IF((X3:X<=3)*(X3:X<>""), "X, ", )&
IF(AE3:AE="no", "AE, ", )&
IF(AF3:AF="no", "AF, ", )), ", $|Fail column: $", ))
I have 6 columns and 4 rows like:
A B C D E F, A B C D, A B C, A B C D E
now I want my end data like:
A>B>C>D>E>F, A>B>C>D, A>B>C, A>B>C>D>E
I have used concatenate formula, but getting a result like:
A>B>C>D>E>F, A>B>C>D>, A>B>C>>, A>B>C>D>E>
Here I don't want >> character when there is no data available in the column
I have tried
=Concatenate(A,">",B,">",C,">",D,">",E,">",F)
=SUBSTITUTE(CONCATENATE(D1,">",E1,">",F1,">",G1,">",H1), ">>", ">")
there are several ways how to solve it like using REGEXREPLACE or SUBSTITUTE:
=SUBSTITUTE(A12, ">>", ">")
=REGEXREPLACE(A12, ">>", ">")
or IF statement like:
=ARRAYFORMULA(IF(A1:F1<>"", A1:F1&">", ))
=JOIN( , ARRAYFORMULA(IF(A1:F1<>"", A1:F1&">", )))
or perhaps simple TEXTJOIN
=TEXTJOIN(">", 1, A1:F1)
I am converting DB2 SQL to Microsoft TSQL Syntax.
DB2 has a CONCAT function working completely different from TSQL that I need to convert.
Example problems...these are all legitimate syntax examples:
Concat as function
CONCAT ( sample1.ColumnName, sample2.ColumnName)
Converts to:
sample1.ColumnName + sample2.ColumnName
Concat as inline
sample1.ColumnName CONCAT sample2.ColumnName
Converts to
sample1.ColumnName + sample2.ColumnName
Concat Symbol as inline
sample1.ColumnName || sample2.ColumnName
Converts to
sample1.ColumnName + sample2.ColumnName
CONCAT as Simple Composite
CONCAT ( sample1.ColumnName, TRIM(sample2.ColumnName))
Converts to
sample1.ColumnName + TRIM(sample2.ColumnName)
CONCAT as Conversion from %!#^#^&
CONCAT ( sample1.ColumnName, sample3.ColumnName CONCAT TRIM(sample2.ColumnName))
Converts to
sample1.ColumnName + sample3.ColumnName + TRIM(sample2.ColumnName)
CONCAT as the Worse Case Scenario
CONCAT ( CONCAT(sample1.ColumnName, sample4.ColumnName), sample3.ColumnName CONCAT TRIM(sample2.ColumnName))
Converts to
sample1.ColumnName + sample4.ColumnName + sample3.ColumnName + TRIM(sample2.ColumnName)
It would be nice to have recursive regex. I can make multiple calls if only processing the outer or inner instance of the replacement pattern.
The one I'm hung up on the most is the version of CONCAT ( value1, value2 )
I can identify CONCAT (. How do I identify value1, value2 and the correct instance of the closing )?
If I can do that, then I can drop CONCAT ( and ). I can return value1 + "+" + value2.
If that scenario could be defined, I could extrapolate other formulas needed, knowing how to specify the necessary groups.
I feel that I would approach it the same way as you have described in your title. First, remove everything off the front of your string and also any closing parenthesis that are not followed by another closing parenthesis. I would match the following and replace with nothing:
^CONCAT.*CONCAT\s*\( | ^Concat (?:Symbol )?as inline | \)(?!\))
Then, do a second replace to look for commas, the string CONCAT or a set of double vertical pipes || and replace it with a plus sign:
\s*(?: , | CONCAT | \|\| )\s*
Here is a demo in PHP
db2 concat to other.
do while locate('Concat(', mystring) = 0
Convert conat(blah , blah ) to blah || blah
loop
Make sure it still works on db2 and you haven't broke anything.
convert || to +
I am trying to remove a string from another string using VBA replace function.
The string from which I am trying to remove looks like below which contains cell address concatenated by ;
"$B$1;$B$21;$B$2;$C$3;$B$20;$B$201"
and the string which I would like to remove is $B$2 by say xxx.
The replace function matches all occurrences of $B$2 in the string and gives me the output as below
$B$1;xxx1;xxx;$C$3;xxx0;xxx01
However I would like to search for $B$2 exactly in the string and expect an output like
$B$1;$B$21;xxx;$C$3;$B$20;$B$201
I one way I could think of doing this is by splitting up the string on ;(separator and looping and looking at each value) but I am looking at more direct solution here. Like using pattern matching techniques or something else.
You can include the following ; in the replace operation, to make sure you only match "complete" references. You just need to take a precaution for also matching the last entry, by adding a dummy semicolon at the end:
s = "$B$1;$B$21;$B$2;$C$3;$B$20;$B$201"
find = "$B$2"
repl = "xxxx"
result = Replace(s & ";", find & ";", repl & ";")
result = Left(result, Len(result)-1) ' Remove the final semicolon
Although this works for your case, in a more general exercise, you would also want to test for the preceding delimiter, and then the last two lines would look like this:
result = Replace(";" & s & ";", ";" & find & ";", ";" & repl & ";")
result = Mid(result, 2, Len(result)-2)
you cold use:
Function myReplace(strng As String, findStr As String, replacementStrng As String)
myReplace = Replace(strng & ";", findStr & ";", replacementStrng & ";")
myReplace = Left(myReplace, Len(myReplace) - 1)
End Function
to be exploited in your "main" sub like follows:
strng = "$B$1;$B$21;$B$2;$C$3;$B$20;$B$201"
MsgBox myReplace(strng, "$B$2", "xxx")
I have to SELECT a substring from a table between nth and mth occurrences of a special character (say -).
For eg: If column data is 'a-b-c-d-e-f-g-h', n is 2 & m is 5, my select statement should return: 'c-d-e'
I tried various regex combinations but I think '\K' cannot be used.
Please help.
You might want to try using instr like this:
with PARAM as (select 'a-b-c-d-e-f-g-h' as S,
'-' as D, 2 as N, 5 as M from dual)
select substr(substr(S, instr(S, D, 1, N) + 1),
1, instr(S, D, 1, M - N) - 1) as RANGE
from PARAM;
The with statement is just there to make the expression clearer.
S is the input string. D is the delimiter.
If you want to use regular expressions then you might try this:
with PARAM as (select 'a-b-c-d-e-f-g-h' as S,
'-' as D, 2 as N, 5 as M from dual)
select -- ^([^D]*D){N}([^D]*(D[^D]*){M-N-1}).*$
regexp_replace(S, '^([^'||D||']*'||D||'){'||N||'}([^'||D||']*('||D||'[^'||D||']*){'||(M-N-1)||'}).*$', '\2') as RANGE
from PARAM;
The regular expression starts with skipping N groups of text and delimiters (([^D]*D){N}) and then forms a group \2 of one text followed by M-N-1 groups of text and delimiters (([^D]*(D[^D]*){M-N-1}).
In the regex approach, delimiters with special meaning in regular expressions have to be quoted.
You can try the below approach:
Select substr('a-b-c-d-e-f-g-h',instr('a-b-c-d-e-f-g-h','-',1,2)+1,5) as occurence from dual;