I want to create a computed column based off a substring of another column in SQL - calculated-columns

I have a column called TAG_
Data in the TAG_ column could like the below:
STV-123456
TV-12456
ME-666666
I want to create two computed columns
One that shows the first part of TAG_ before the hyphen
STV
TV
ME
One that shows the second part of TAG_ after the hyphen
123456
12456
666666
This shouldn't be hard but the light bulb is not on yet. Please help.

try this:
SELECT SUBSTRING(TAG_ ,0,CHARINDEX('-',TAG_ ,0)) AS before,
SUBSTRING(TAG_ ,CHARINDEX('-',TAG_ ,0)+1,LEN(TAG_ )) AS after from testtable
and the result:
Hope this helps!

Example for MySQL, syntax is likely different for other vendors:
create table t
( tag_ text not null
, fst text generated always as (substr(tag_, 1, locate('-', tag_)-1)) stored
, snd text generated always as (substr(tag_, locate('-', tag_)+1)) stored
);
Fiddle

Related

Power Query Data from different columns

i am new to Power query and i would like to learn a bit more about it. I am facing the following problem. My table looks like this (empty fields already removed):
What i'm trying is to get a new table where "Spalte2" holds my list of ISINs and S^"Spalte 8" but also "Spalte 9" and "Spalte 10" hold my portfolio share (komma separated).
EDIT: For clarification I hope to get something like this:
EDIT: I try to get a table in here, hope it works:
Spalte1
Spalte2
Spalte8
Spalte9
Spalte10
Bâloise Holding AG
CH0012410517
1,04
Null
Null
Barry Callebaut AG
CH0009002962
0,63
Null
Null
Galenica AG
CH0360674466
0,58
Null
Null
Givaudan SA
CH0010645932
1,24
Null
Null
HelloFresh SE
DE000A161408
527.705,26
1,85
Null
Kering S.A.
FR0000121485
431.145,00
1,51
Null
Standard Chartered PLC
GB0004082847
4,610 117.699,50
Null
0,41
Unilever PLC
GB00B10RZP78
42,305 315.241,76
Null
1,11
What i'm trying to get is this:
Spalte2
Spalte8
CH0012410517
1,04
CH0009002962
0,63
CH0360674466
0,58
CH0010645932
1,24
DE000A161408
1,85
FR0000121485
1,51
GB0004082847
0,41
GB00B10RZP78
1,11
Which way can i use in PQ to match the ISIN with its portfolio share? Thanks a lot!
Thomas
Am I correct in understanding that you simply want to consolidate the information from the rightmost populated column of each row into one column, and disregard any other information between it and the first column?
If so, then this might be one possible approach.
Starting with a sample table called Table1 in power query:
I just add a new column and use if then statements to select the rightmost populated column's information:
(In the above M code, I check that each column is both not null and not blank, to be thorough.)
I get this result:
Then I select the Spalte2 and Custom columns and remove the other columns to get this:

How to Flatten a semicolon Array properly in Azure Data Factory?

Context: I've a data flow that extracts data from SQL DB, when data comes is just one column with a string separated by tab, in order to manipulate the data properly, I've tried to separate every single column with its corresponding data:
Firstly, to 'rebuild' the table properly I used a 'Derived Column' activity replacing tab with semicolons instead (1)
dropLeft(regexReplace(regexReplace(regexReplace(descripcion,[\t],';'),[\n],';'),[\r],';'),1)
So, after that use 'split()' function to get an array and build the columns (2)
split(descripcion, ';')
Problem: When I try to use 'Flatten' activity (as here https://learn.microsoft.com/en-us/azure/data-factory/data-flow-flatten), is just not working and data flow throws me just one column or if I add an additional column in the 'Flatten' activity I just get another column with the same data that the first one:
Expected output:
column2
column1
column3
2000017
ENVASE CORONA CLARA 24/355 ML GRAB
PC13
2004297
ENVASE V FAM GRAB 12/940 ML USADO
PC15
Could you say me what i'm doing wrong, guys? thanks by the way.
You can use the derived column activity itself, try as below.
After the first derived column, what you have is a string array which can just be split again using derived schema modifier.
Where firstc represent the source column equivalent to your column descripcion
Column1: split(firstc, ';')[1]
Column2: split(firstc, ';')[2]
Column3: split(firstc, ';')[3]
Optionally you can select the columns you need to write to SQL sink

Tableau: Multiple columns in a filter

I have three numeric fields named A,B,C and wants them in a single filter in tableau and based on the one selected in that filter a line chart will be shown. For e.g. in filter Stages B column is selected and line chart of B is shown. Had it been column A selected then line chart of A would be displayed .
Pardon my way of asking question by showing a image. I just picked up learning tableau and not getting this trick any where.
Here is the snapshot of data
Create a (list) parameter named 'ABC'. With the values
A
B
C
Then create a calculated field
IF ABC = 'A' THEN [column_a]
ELSEIF ABC = 'B' THEN [column_b]
ELSEIF ABC = 'C' THEN [column_c]
END
Something like that should work for you. Check out Tableau training here. It's free, but you have to sign up for an account.
Another way without creating a calculated field. Just pivot the three columns to rows and your field on which you can apply filter is created. Let me show you
This is screenshot of input data
I converted three cols to pivots to get data reshaped like this
After renaming pivoted-fields column to Stages I can add directly this one to view and get my desired result.

Excel Power Query -- Select value in column specified in related table -- INDEX+MATCH alternative

Problem
I have two queries, one contains product data (data_query), the other (recode_query) contains product names from within the data_query and assigns them specific id_tags. id_tags are also column names within the data_query.
What I need to achieve and fail at
I need the data_query to look at the id_tag of the specific product name within the data_query, as parsed from the recode_query (this is already working and in place) and input the retrieved value within the specific custom column cell. In Excel, I would be using INDEX/MATCH combo:
{=INDEX(data_query[#Data];; MATCH(data_query[#id_tag]; data_query[#Headers]; 0))}
I have searched near and far, but I probably can't even spot the solution, even if I have come across it, as I am not that deep in the data manipulation and power query myself.
Is this what you're wanting?
let
DataQuery = Table.FromColumns({{1,2,3}, {"Boxed", "Bagged", "Rubberbanded"}}, {"ID","Pkg"}),
RecodeQuery = Table.FromColumns({{"Squirt Gun", "Coffee Maker", "Trenching Tool"}, {1,2,3}}, {"Prod Name", "ID2"}),
Rzlt = Table.Join(DataQuery, "ID", RecodeQuery, "ID2", JoinKind.Inner)
in
Rzlt

Teradata String Manipulation

I have taken a good look at the Teradata Syntax reference to no avail
I have some rows with numbers:
ID
Mickey
Laura9
Larry59N
How do I take away the integers from my rows?
I understand that SUBSTR(id, 0, index(id, '%FORMAT%')) would work, but I don't know what could I enter in the %FORMAT% area to just find integers.
You can use oTranslate to remove numbers:
BTEQ -- Enter your SQL request or BTEQ command:
Select the_name, oTranslate( the_name, 'a0123456789','a')
from
( SELECT 'Larry59N' the_name FROM ( SELECT 'X' DUMMY ) a
UNION ALL
SELECT 'Laura9' FROM ( SELECT 'X' DUMMY ) b
UNION ALL
SELECT 'Mickey' the_name FROM ( SELECT 'X' DUMMY ) c
) d
;
*** Query completed. 3 rows found. 2 columns returned.
*** Total elapsed time was 1 second.
the_name oTranslate(the_name,'a0123456789','a')
-------- -----------------------------------------------------
Larry59N LarryN
Laura9 Laura
Mickey Mickey
HTH.
Cheers.
Unfortunately, I don't believe there is a function native to Teradata that will accomplish this. I would suggest looking at the UDF's posted on the Teradata Developer Exchange (link). The function eReplaceChar in particular looks like it may help you accomplish what you are looking to do with this data. The UDF's found at the link above were published under the Apache 2.0 license so you should not have any problems using them.

Resources