0

I have a function, where a sentance is passed in. I want to pull data only if the word is found in that sentance. Heres what I have:

select 
    word_data
from words
where
    words.word in STRING_SPLIT('Lorem ipsum dolor sit amet.', ' ');

Does anyone know the correct way to do this?

3
  • STRING_SPLIT returns a dataset, not a scalar value. It goes in the FROM. Have you read the documentation and its examples? It demonstrates exactly how to do what you're after. Commented Feb 1, 2021 at 13:14
  • 1
    Side note: The last "word" in your query will be treated as 'amet.' not 'amet'. If you have the word 'amet' in your column word it will not be matched. Commented Feb 1, 2021 at 13:16
  • to add to what @Larnu said you'll want to handle any punctuation that exists in your string. Commented Feb 1, 2021 at 13:17

1 Answer 1

2

According documentation expression have to be like

SELECT
    [word_data]
FROM
    [words]
WHERE
    [words].[word] IN (
            SELECT
                [value]
            FROM
                STRING_SPLIT('Lorem ipsum dolor sit amet.', ' ')
        );

and same result via JOIN

SELECT
    [word_data]
FROM
    [words]
INNER JOIN (
        SELECT
            [value]
        FROM
            STRING_SPLIT('Lorem ipsum dolor sit amet.', ' ')
    ) AS [s] ON [s].[value] = [words].[word];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.