-1

I got this question in SQL Server developer interview. Can anyone help please?

If there is a column of type varchar(max) with a lot of words, how can I list the words?

Thanks

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • 1
    Assuming the words are delimited by a common character, then you'd use some kind of function to split them out. Depending on the SQL Server version, there may be a built-in function that will do the trick, or you'd need to create one. For examples, see https://stackoverflow.com/questions/10581772/how-to-split-a-comma-separated-value-to-columns – Craig Jun 16 '21 at 04:43
  • 1
    Please provide a [mre] with some sample data, your desired results, and your research+attempt so far. – Dale K Jun 16 '21 at 05:24
  • The response I personally would respect more is a question about why anyone would need to do this using tsql (perhaps phrased a little differently). That simply is not a task well suited to a relational database. – SMor Jun 16 '21 at 11:06
  • Please define what a word is and how you define one in the data. – Gordon Linoff Jun 16 '21 at 12:18

1 Answers1

0

enter image description here

select DepartmentID,Name, Split_Values
from HumanResources.Department
cross apply (SELECT value as Split_Values FROM STRING_SPLIT(Name, ' ')) as ca
where DepartmentID  in (12,14,9)

We are able to split the data of one column as an Array / MultiValue using the STRING_SPLIT function.

We executed the above query on AdventureWorks2019 database for DepartmentID 12, 14 and 9 and returning the expected result. Here 'name' is column with words and 'Split_Values' is the output of name column.