Parse a PIPE delimited field into seperate fields (MS ACCESS 2007-2013) - sql

I have a database in MC Access 2017-2013 where the fields are stored as a PIPE delimited string. Now I want a way to parse these fields in a SELECT query in separate fields.
Here an example of my database and the PIPE delimited field:
The field contains 9 Pipes. So I want to separate the field in 10 fields.
Here an example of the Output:
I would be great, if someone could help me.
Thanks,
Peter

You need a VBA function in a standard module to implement the splitting, e.g.
Public Function SplitPipes(str As String, i As Long) As String
Dim arSplit As Variant
arSplit = Split(str, "|")
' Check that arSplit has enough elements
If i - 1 <= UBound(arSplit) Then
' Split creates a 0-based array, but it is easier to start with index 1 in the query
SplitPipes = arSplit(i - 1)
Else
' out of elements -> return empty string
SplitPipes = ""
End If
End Function
Then you can use this function for every single field like this:
SELECT SplitPipes([Strategic Group],1) AS Destination,
SplitPipes([Strategic Group],2) AS SE,
...
FROM yourTable;
Note that the function currently has no error handling whatsoever. Added :)

Related

Extract from string before last instance of a character - BigQuery

I am working on a problem where I would like to extract all parts of a string before the last instance of a repeating character.
I have the below data as an example:
string_variable
string_variable_two
My goal is to extract all text in the string before the last underscore "_".
So for the above I would end with:
string
string_variable
I cannot split because in some instances I have a single, and in some I have two. I also tried using a REGEX_EXTRACT but BigQuery will not allow look ahead functions for .*(?=_)
Consider below
select str,
ifnull(safe.left(str, instr(str, '_', -1) - 1), str)
from your_table
if applied to sample data in y our question - output is

How to Extract List of Text File Values Between 2 Identifiers That are Used Several Times

I am trying to make a VB.NET Visual Studio 2019 form that will ask for a text file and output a list of names in a TextBox I have called TextBox4 so I don't have to create any files (or maybe create a text file, copy it to TextBox4, then delete it?). The names from a text file are between "Customer_Name" and "Customer_ID". The file doesn't seem to have any rhyme or reason other than those 2 identifiers, so splitting it up efficiently has been tough. There are generally between 100 and 1000 entries per file if that is relevant.
Sample (mock) data:
"Customer_name":"JOHN DOE","Customer_id":"9251954","Customer_team_id":"HOST","Customer_position_id":"MGR","Customer_short_name":"Joey","Customer_eligibility":"LT5","Customer_page_url":"google.com","Customer_alt_id":"M7","Customer_name":"JANE DOE","Customer_id":"8734817","Customer_team_id":"HOST","Customer_position_id":"TECH","Customer_name":"JOSEPH DOE","Customer_id":"8675307",
I would want to show this in the text box:
JOHN DOE
JANE DOE
JOSEPH DOE
Take a look at this regular expression pattern:
(?:")(?<key>\w+)(?:":")(?<value>((\w|\s|\.))+)(?:",)
This does several things:
(?:") - create non-capturing group to match the open quotation mark
(?<key>\w+) - create a named group to match the key (e.g. Customer_name)
(?:":") - create a non-capturing group to match the close quotation mark, colon, and open quotation mark
(?<value>((\w|\s|\.))+) - create a named group to match the value (e.g. John Doe)
(?:",) - create a non-capturing group to match the close quotation mark
With this, you can loop over the matches and the matches' groups to get just the customer names:
' declare the pattern and input (escaping quotation marks) as well as a collection to store just the customer_name values
Dim pattern As String = "(?:"")(?<key>\w+)(?:"":"")(?<value>((\w|\s|\.))+)(?:"",)"
Dim input As String = """Customer_name"":""JOHN DOE"",""Customer_id"":""9251954"",""Customer_team_id"":""HOST"",""Customer_position_id"":""MGR"",""Customer_short_name"":""Joey"",""Customer_eligibility"":""LT5"",""Customer_page_url"":""google.com"",""Customer_alt_id"":""M7"",""Customer_name"":""JANE DOE"",""Customer_id"":""8734817"",""Customer_team_id"":""HOST"",""Customer_position_id"":""TECH"",""Customer_name"":""JOSEPH DOE"",""Customer_id"":""8675307"""
Dim matches As MatchCollection = Regex.Matches(input, pattern)
Dim names As New List(Of String)()
' loop over each match
For Each match As Match In matches
' loop over each group in the match
For index As Integer = 0 To match.Groups.Count - 1
Dim group As Group = match.Groups.Item(index)
' only do something if we're on the "key" group, the "key" group's value is Customer_name, and there's at least one more group left
If (group.Name = "key" AndAlso group.Value = "Customer_name" AndAlso index < match.Groups.Count - 1)
' only do something if the next group is the "value" group
Dim valueGroup As Group = match.Groups.Item(index + 1)
If (valueGroup.Name = "value") Then
' add the key's value
names.Add(valueGroup.Value)
Exit For
End If
End If
Next
Next
' set the TextBox's lines
TextBox4.Lines = names.ToArray()
Fiddle: https://dotnetfiddle.net/Zja46U
Edit - keep in mind that because we're using named groups, this code can be expanded to get any key/value pair now. It is just for the sake of this example that I'm getting only the Customer_name key/value pair.

Hive array specifying multiple delimiter in collection

I have dataset contains two arrays, both arrays separated by different delimiter..
Ex: 14-20-50-60 is 1st array seperated by -
12#2#333#4 is 2nd array seperated by #..
While creating table how do we specify delimiter in
Collection items terminated by '' ?
input
14-20-50-60,12#2#333#4
create table test(first array<string>, second array<string>)
row format delimited
fields terminated by ','
collection items terminated by '-' (How to specify two delimiters in the collection)
You cannot use multiple delimiters for the collection items. You can achieve what you are trying to do as below though. I have used the SPLIT function to create the array using different delimiters.
Data
14-20-50-60,12#2#333#4
SQL - CREATE TABLE
create external table test1(first string, second string)
row format delimited
fields terminated by ','
LOCATION '/user/cloudera/ramesh/test1';
SQL - SELECT
WITH v_test_array AS
(SELECT split(first, "-") AS first_array,
split(second, "#") AS second_array
FROM test1)
SELECT first_array[0], second_array[0]
FROM v_test_array;
OUTPUT
14 12
Hope this helps.

How do I search for a string from table column in SQL Server using CHARINDEX function

I have a string in table column like below
1. "C-DERD,C-FDER,E-FDFE,E-DFE,C-DFERE"
2. "E-FDFE,C-DFEC,E-DFDFE,E-DE"
If I search for a string like "DE" then it will first check all the individual string that starts with 'C-' AS LIKE CONSTRAINT IN TABLE. If it is found, then it will return TRUE in separate column, in second case it is not found in 'C-' case then it will check in all individual string that start with 'E-' but it is same as searched string. With use of CHARINDEX function only.
As I mention in a comment, you should fix the data structure. Sometimes, though, we are stuck with other people's really bad designs and are not in a position to fix them.
One method is to split the string, although that is a bit painful. In your case you could do:
select t.*,
(case when str like '%CE-DE%' then 1
when str like '%CE-[^,]DE%' then 1
when str like '%CE-[^,][^,]DE%' then 1
when str like '%CE-[^,][^,][^,]DE%' then 1
else 0
end) as flag
This looks for up to three intervening characters between the - and DE, which is sufficient for your examples.

Search string with regular expression SQL Server

The regex I want to use is: ^(?=.*[,])(,?)ABC(,?)$
What I want to get out is:
^ // start
(?=.*[,]) // contains at least one comma (,)
(,?)ABC(,?) // The comma is either in the beginning or in the end of the string "ABC"
$ // end
Of course ABC is ought to be a variable based on my search term.
So if ABC = 'abc' then ",abc", "abc,", ",abc," will match but not "abc" or "abcd"
Better way to do this is also welcome.
The value in the record looks like "abc,def,ghi,ab,cde..." and I need to find out if it contains my element (i.e. 'abc'). I cannot change the data structure. We can assume that in no case the record will contain only one sub-value, so it is correct to assume that there always is a comma in the value.
If you want to know if a comma delimited string contains abc, then I think like is the easiest method in any database:
where ',' + col + ',' like '%,abc,%'