December 16, 2020 at 8:29 pm
I am using the below case statement
, CASE WHEN Attributes LIKE ',Size = %,' Then right(Attributes, len(Attributes) - charindex('Size ', Attributes)) ELSE '' END as Size
To get the below result
,Size = XXL ,Fits to Chest Size = 48 to 50 in,,Closure Type = Snap Button ,Material = Cotton ,Color = Khaki ,Sleeve Length = 33 in,Lining Material = Polyester Thread ,,Fabric Weight = 9 oz,,,Resists = Flame ,,,,,,,,,,,,,,,,,,,,,,,,,,
I want to remove everything and only have Size = XXL
Any help is appreciated.
December 16, 2020 at 9:04 pm
The following seems to work. Add TRIM() if desired fix space before comma.
DROP TABLE IF EXISTS #attributes
CREATE TABLE #attributes
(
Attributes VARCHAR(MAX)
);
INSERT INTO #attributes (Attributes)
VALUES (',Size = XXL ,Fits to Chest Size = 48 to 50 in,,Closure Type = Snap Button ,Material = Cotton ,Color = Khaki ,Sleeve Length = 33 in,Lining Material = Polyester Thread ,,Fabric Weight = 9 oz,,,Resists = Flame');
SELECT CHARINDEX(',',Attributes,2),
CASE WHEN Attributes LIKE ',Size = %' Then SUBSTRING(Attributes,2,CHARINDEX(',',Attributes,2) - 2) ELSE '' END as Size
FROM #attributes;
The simplicity of this approach seems less convoluted, but might be less efficient at scale:
SELECT attributes.value
FROM #attributes
CROSS APPLY STRING_SPLIT(#attributes.Attributes, ',') attributes
WHERE value LIKE 'Size = %';
December 16, 2020 at 9:22 pm
I think you are missing a "LEFT" on your RIGHT. You have the RIGHT side of the string, but have too much. So, what you probably want is:
LEFT(right(Attributes, len(Attributes) - charindex('Size ', Attributes)-1),charindex(',',right(Attributes, len(Attributes) - charindex('Size ', Attributes)-1))
NOTE - I did not test that.
What I changed:
added a "-1" onto the number of characters it grabs from the "RIGHT" command to strip off that first comma that appeared in your result and tossed that into a LEFT where we grab all the characters up to the first comma. This SHOULD take your",Size = XXL ,Fits to Chest..." string and give you "Size = XXL ". If you do an RTRIM on that resulting string, that would remove the trailing space. LTRIM would remove any leading spaces. Can't remember if SQL 2017 supports TRIM or if that was a 2019 feature, but LTRIM and RTRIM have been around for ages.
The above is all just my opinion on what you should do.
As with all advice you find on a random internet forum - you shouldn't blindly follow it. Always test on a test server to see if there is negative side effects before making changes to live!
I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.
December 16, 2020 at 10:18 pm
I am using the below case statement
, CASE WHEN Attributes LIKE ',Size = %,' Then right(Attributes, len(Attributes) - charindex('Size ', Attributes)) ELSE '' END as Size
To get the below result
,Size = XXL ,Fits to Chest Size = 48 to 50 in,,Closure Type = Snap Button ,Material = Cotton ,Color = Khaki ,Sleeve Length = 33 in,Lining Material = Polyester Thread ,,Fabric Weight = 9 oz,,,Resists = Flame ,,,,,,,,,,,,,,,,,,,,,,,,,,
I want to remove everything and only have Size = XXL
Any help is appreciated.
Ok... lets start thinking about "bullet proofing" it.
As a bit of a sidebar, I'd seriously consider turning this into an EAV table to make future look ups of all sorts a whole lot easier while having a chance of the code actually being SARGable for look ups.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 17, 2020 at 5:16 am
I added an RTRIM to get rid of any trailing space(s). Naturally if you don't want to do that, delete the RTRIM from the code.
SELECT
Attributes,
RTRIM(SUBSTRING(Attributes, size_start + 1, CHARINDEX(',', Attributes + ',', size_start + 1) - (size_start + 1))) AS size_string
FROM ( VALUES('Something = Something,Else = Else,blah=blah...,Size = XXL ,Fits to Chest Size = 48 to 50 in,,Closure Type = Snap Button ,Material = Cotton ,Color = Khaki ,Sleeve Length = 33 in,Lining Material = Polyester Thread ,,Fabric Weight = 9 oz,,,Resists = Flame') ) AS test_data(Attributes)
CROSS APPLY (
SELECT CHARINDEX(',Size =', Attributes) AS size_start
) AS ca1
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply