I've been working on a rather convoluted process to format some data for a work project. We received a data extract and need to export it for import during a migration, but some of the data won't import due to case sensitivity (user logons with sentence case for example).
In an ideal world, I could demand the data be sanitised and formatted before it's provided for me to build the import, but I can't, so I'm stuck where I have to format it myself.
Plan:
- Take string result
- Split string result by pipe delimitation
- Format each split results
( )
into lower case (where applicable) - Put all split results back into one string using FOR XML PATH
Example of problem:
Field 'Assigned To' can contain a pipe delimitted string of users and/or user groups, e.g.
John Smith (jsmith)|College Group of Arts|Bob Jones (BJones)
Now as you can see above, John Smith (jsmith) looks fine, as does College Group of Arts, however Bob Jones has had his logon sentence cased, so I need to use a LOWER command, chained with SUBSTRING and CHARINDEX to convert the logon to lower. Standalone, this approach works fine, but the problem I'm having is where I'm using a function found here on Stack Overflow (slightly manipulated to account for pipe delimitation) T-SQL split string.
When I retrieve the table results of the split string, I can't apply CHARINDEX against any characters in the result string, and I can't work out why.
Scenario:
The raw data extract, untouched, returns the below when queried;
|College of Science Administrators|Bob Jones (BJones)|
I then apply the below query, which calls the function queried above;
declare @assignedto nvarchar(max) = (select assigned_to from project where project_id = 1234)
SELECT SUBSTRING(Name,CHARINDEX(Name,'('),255)
FROM dbo.splitstring(@assignedto)
I then get the below results;
College of Science Administrators
Bob Jones (BJones)
What I'd expect to see is;
College of Science Administrators
(BJones)
I could then apply my LOWER logic to change it to lower case.
If that worked, then thought process was then to take those results and pass them back into a single string using a FOR XML PATH.
So I guess technically, there are 2 questions here;
- Why won't my function let me manipulate the results with CHARINDEX?
- And is this the best way to do what I'm trying to achieve overall?