SQL Server SQL Syntax Select Custom String from an Array not selecting Correctly

q3aa0525  于 2023-11-16  发布在  其他
关注(0)|答案(3)|浏览(115)

The Issue:

I am trying to select from multiple tables, Table A and B but this is mainly regarding the Only column I am choosing from Table B. The values in this column contain an Array of string.

Aim is to get get rid of any [] and then look at string. There are two main types of strings, one that begins with “some text needed”, and others that have same "some text needed" followed by comma and further text, Other type of string contain random text NOT Starting with "some text needed".the examples include:

  • "some text needed"
  • "some text needed, other"
  • "some text needed, extra text"
  • "some text needed, extra - text, required"
  • "today is a rainy day"
  • "Extra hot"

What is Required:

For condition where string does not start with "some text needed" simply return the text value and for others return the values after the comma (,) while ignoring "some text needed," so the output would look something like:

Desired Results:

  • "other"
  • "extra text"
  • "extra - text, required"
  • "today is a rainy day"
  • "Extra hot"

I have used following SQL code, which gets rid of [ and ] but it is always bringing me back "some text needed, other" instead of just "other"

  1. SELECT
  2. col1, col2, ..., col10,
  3. CASE
  4. WHEN CHARINDEX('[', cier.if_text_is_required) > 0
  5. THEN REPLACE(REPLACE(cier.if_text_is_required, '[', ''), ']', '')
  6. WHEN CHARINDEX('some text needed,', cier.if_text_is_required) > 0
  7. THEN
  8. CASE
  9. WHEN CHARINDEX(',', cier.if_text_is_required, CHARINDEX('some text needed,', cier.if_text_is_required) + LEN('some text needed,')) > 0
  10. THEN LTRIM(SUBSTRING(cier.if_text_is_required, CHARINDEX(',', cier.if_text_is_required, CHARINDEX('some text needed,', cier.if_text_is_required) + LEN('some text needed,')) + 2, LEN(cier.if_text_is_required)))
  11. ELSE cier.if_text_is_required
  12. END
  13. ELSE cier.if_text_is_required
  14. END AS if_text_is_required
  15. FROM TableA
  16. LEFT JOIN (
  17. SELECT id, if_text_is_required, ROW_NUMBER() OVER (PARTITION BY id ORDER BY date DESC, rn_id DESC) AS rn
  18. FROM TableB
  19. WHERE if_text_is_required IS NOT NULL
  20. ) AS cier ON TableA.id = cier.id AND cier.rn = 1

I can get rid of [ and ] however, I am unable to SELECT Only the string after the comma when string contains, "some text needed".

Can someone spot any issues with the above SQL and can assist me? All assistance is appreciated.

hlswsv35

hlswsv351#

  1. SELECT
  2. A.*,
  3. CASE
  4. WHEN B.YourColumn LIKE 'some text needed%'
  5. THEN STUFF(B.YourColumn, 1, CHARINDEX(',', B.YourColumn + ','), '')
  6. ELSE REPLACE(REPLACE(B.YourColumn, '[', ''), ']', '')
  7. END as manipulated_column
  8. FROM
  9. TableA AS A
  10. JOIN
  11. TableB AS B
  12. ON A.JoinColumn = B.JoinColumn
  • STUFF() : This function is used to remove a part of the string and replace it with another string.
  • CHARINDEX() : This finds the index of the first occurrence of a string within another one.
  • REPLACE() : This is used twice to remove both the '[' and ']'
wn9m85ua

wn9m85ua2#

Here's how I'd go about it. I've created my own dataset here since none was provided in the question, and got rid of the joins and expressions that are immaterial to the question. Ostensibly, your question is about formatting the strings. So I'll start with this data:

  1. drop table if exists #data
  2. select String
  3. into #data
  4. from
  5. (
  6. values
  7. ('[some text needed]'),
  8. ('[some text needed, other]'),
  9. ('[some text needed, extra text]'),
  10. ('[some text needed, extra - text, required]'),
  11. ('[today is a rainy day]'),
  12. ('[Extra hot]')
  13. ) a (string)

Again, I think the square brackets question is probably immaterial to the question since you even said it's not causing you any problems. But I'll leave them in since you mentioned it.

The first thing I'd do is strip off the square brackets. You can do that with the trim function and providing a list of characters to trim. In this case, I've chosen to trim [] to get rid of the brackets and any whitespace that might remain.

First, I'm going to assign that special value "some text needed" to a variable so I don't have to re-type it everyone.

Then you just have to handle your different cases. Since you can have a string which is just "Some text needed" with no item after it, let's treat that as a special case, and set its output value to an empty string.

The other special case is when we have "some text needed" followed by a comma, and some other stuff. So we'll check to see if your cleaned-up string starts with "some text needed," (notice the comma). I like to handle that by using the stuff function. STUFF is great for when you need to get rid of some number of characters from the start of your string. So here I'm removing all the characters up to that comma, and adding 2 (one because the stuff function needs the index of the next character, and one more because if i just take len(@Slug) it won't account for the comma).

Then of course the default case is to just echo the original string.

  1. declare @slug nvarchar(128) = 'some text needed'
  2. select
  3. String,
  4. TrimmedString,
  5. case
  6. when TrimmedString = @slug then ''
  7. when TrimmedString like @Slug + ',%' then stuff(TrimmedString, 1, len(@Slug) + 2, '')
  8. else TrimmedString
  9. end
  10. from #data
  11. cross apply
  12. (
  13. select TrimmedString = trim('[] ' from string)
  14. ) b

Note, if you're running a version of SQL Server that doesn't support TRIM , you can just replace that function call with the code you already had working. This is just a shortcut if you have access to it.

展开查看全部
ltskdhd1

ltskdhd13#

Changing Case When to ‘some text needed,%' (adding comma) helps deal with issue and returns everything without square brackets.

  1. SELECT
  2. A.*,
  3. CASE
  4. WHEN B.YourColumn LIKE '
  5. THEN STUFF(B.YourColumn, 1, CHARINDEX(',', B.YourColumn + ','), '')
  6. ELSE REPLACE(REPLACE(B.YourColumn, '[', ''), ']', '')
  7. END as manipulated_column
  8. FROM
  9. TableA AS A
  10. JOIN
  11. TableB AS B
  12. ON A.JoinColumn = B.JoinColum

However, I am unable to SELECT only the second part of script after the comma. If string has 2 parts separated by comma I only want to select everything after first comma, if multiple commas then Only string after comma until third comma (excluding third comma).

Hope it helps explain the issue

展开查看全部

相关问题