We have an issue with instrument names loaded into our data warehouse where they either loaded with the string all in upper case or in normal case (capital at the beginning of each word). We have a function that deals with changing all of the upper case strings into normal case and an example of some of these are below:
Merck & Co Inc Common Stock Usd.5
Newmont Mining Corp Common Stock Usd Inc 1.6
However there are parts of this which we would like to capitalise when it comes to reporting ie in the examples above 'Usd' should become 'USD', 'Inc' should become 'INC', 'Corp' should become 'CORP'.
These are all held in a cross reference table, however i'm unsure of how i can update my select so that when i select my normal case instrument names it will check the cross reference table to ensure update(replace) the parts that should be capitalised into capitals.
I got as far as this:
CREATE TABLE capital ([InternalValue] VARCHAR(255), [ExternalValue] VARCHAR(255));
CREATE TABLE instrument ([Instrument] VARCHAR(255));
INSERT INTO capital ([InternalValue], [ExternalValue])
VALUES
('Usd', 'USD'),
('Ltd', 'LTD'),
('Corp', 'CORP'),
('Inc', 'INC')
INSERT INTO instrument ([Instrument])
VALUES
('Merck & Co Inc Common Stock Usd.5'),
('Newmont Mining Corp Common Stock Usd Inc 1.6');
SELECT REPLACE(Instrument, 'Usd', 'USD') FROM instrument AS i
But i don't know how i can incorporate the capital table into this so that it can check the whole string for all possible capitalisations. Any help?
If you have your values that you want to use as a replacement in a table able you are going to perform this action repeatedly, then you could create a function to perform the replacement:
Then to query the data you can use:
See SQL Fiddle with Demo
Which will return:
Note: I found the base code here from @SQL Kiwi and altered it to use a function if this is something you will have to do on a consistent basis
I think I read all possible capitalizations wrong.
Originally I read it as all posibilities of a single word.
When you mean all rows in capital.
On my SQL 2008R2 the from is case insensitive
I think your problem is going to be the reverse. How not to find all combinations.
My experience is the select / find side is always case insensitive in TSQL.
I think you are going to need CLR to get the level of case sensitivity you need.
If speed is an issue I would use .NET and read Capital into and Dictionary and process with Regex.