Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How select an aggregated row in SQL?

I have an table with aggregated columns like this in a SQL Server. The goal is: Show me any row that contain Street A or B.

Name Adresses
Mike Street A, Street B, Street C
Smith Street B
Jimmy Street C
Declare @street table (col nvarchar (50) )
INSERT INTO @street Values ('Street A'), ('Street B' )

SELECT *
FROM Table 
WHERE Adresses like '%' + @street + '%'

SELECT *
FROM Table 
WHERE Adresses = ( SELECT * FROM  @street )

SELECT *
FROM Table 
WHERE STRING_SPLIT(Adresses,',') in ( SELECT * FROM  @street )

It does not work. I do not get results. Results should be like:

Name Adresses
Mike Street A, Street B, Street C
Smith Street B

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should get rid of this bad structure and store the data in a better form in future.

Anyway, following your intention to use STRING_SPLIT for that, this would do:

SELECT name, adresses  
FROM yourtable  
WHERE EXISTS 
  (SELECT *  
    FROM STRING_SPLIT(adresses, ',')  
    WHERE value IN ('Street A', 'Street B'));  

You should read the documentation, that’s explained there.

Testing with your sample data succeeds: db<>fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading