March 7, 2017 at 12:22 pm
What is the best way to parse out 'CM.INIT' from 'F0-B20170225131636437{A^CM.INIT}'?
March 7, 2017 at 12:32 pm
NineIron - Tuesday, March 7, 2017 12:22 PMWhat is the best way to parse out 'CM.INIT' from 'F0-B20170225131636437{A^CM.INIT}'?
Can you explain the rules?
March 7, 2017 at 12:40 pm
if we assume there's only one carat, and only one curly brace:
/*
(No column name)
CM.INIT
CM.INIT
*/WITH MySampleData(val)
AS
(
SELECT 'F0-B20170225131636437{A^CM.INIT}' UNION ALL
SELECT 'F1-B30170226136437{A^CM.INIT}'
)
--assuming only one carat exists, and only one right curly brace
SELECT
CASE
WHEN CHARINDEX('^',val) > 0 AND CHARINDEX('}',val) > CHARINDEX('^',val)
THEN SUBSTRING(val,CHARINDEX('^',val) + 1,CHARINDEX('}',val) - (CHARINDEX('^',val) + 1))
ELSE ''
END
FROM MySampleData
Lowell
March 7, 2017 at 12:41 pm
NineIron - Tuesday, March 7, 2017 12:22 PMWhat is the best way to parse out 'CM.INIT' from 'F0-B20170225131636437{A^CM.INIT}'?
Like Luis said, what are the rules? Does the field that contains this value have any variations in length or format? What other possible values can be present? Are the braces "{ }" ALWAYS present in any given value? Is the ^ present in every record? There needs to be an exact set of rules one can follow to determine if that value can consistently be retrieved from a given record. And you have to be TOTALLY anal about ensuring that all the data ALWAYS follows ALL the rules. There are going to be occasions when data suddenly no longer follows the rules. Sometimes, you can change the rules to accommodate the new variation. Other times, there needs to be a whole new set of rules. In some cases, data just plain breaks the rules in such a way that a new rule or a change to the rules just isn't possible. Let us know...
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
March 8, 2017 at 4:08 am
I didn't think you needed rules but, here goes.............
The ^ is always the character before the string and the } is always after the string.
March 8, 2017 at 8:03 am
NineIron - Wednesday, March 8, 2017 4:08 AMI didn't think you needed rules but, here goes.............
The ^ is always the character before the string and the } is always after the string.
That's part of what they wanted to make sure of. The other thing they want to make sure of is, will EVERY row ALWAYS have those two things in it... ALWAYS?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 8, 2017 at 8:17 am
Got it. I would have said so otherwise in my first post but, I understand why they would want those specifics.
March 8, 2017 at 4:22 pm
Did you try Lowell's code (looks like it will work and he's really good about testing his stuff) and, if so, are you all set now?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 9, 2017 at 4:20 am
Sorry, yes I did. I get very easily distracted here at work.
Thanx Lowell.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply