Saturday, 1 October 2016

Compare Id from a Comma separated value in Sql Server

I have a column in which UserIds are stored in a comma separated form (as shown below). Now I have a problem that I need to check whether the current user is available in that list or not. 

I was using split function for this but that was taking too much time. As we need to perform this action on millions of records and table is having 20 other columns which we need to returns. 

Id     UserId 
--  ---------------
1    10,11,12,13    
2    10,13,15,4 

I was using split function for this but that was taking too much time. As we need to perform this action on millions of records and table is having 20 other columns which we need to returns.

Then I found the below solution of my problem. 

SELECT * FROM dbo.History 
WHERE (','+ RTRIM(ShareWith)+',') LIKE '%,'+CAST(@UserId AS VARCHAR)+',%')



6 comments:

madad ali said...
This comment has been removed by a blog administrator.
maha mohamed said...

شركة زهرة الخليج
شركة خدمة 5 نجوم
شركة ابداع للخدمات المنزلية

fgbhn said...

Do not suffer after the day with our company, the Knights of Dammam, where our company provides the best home services at all and we have a section of cleaning and control and sewerage and insulation, as well as we have a special section of anti-termites and we have a special cleaning of reservoirs, and our shop cleaning boards and the best cleaning and cleaning materials Modern machines.
Home cleaning company in Dammam


Transport company in Dammam


House cleaning company Babha

Taslik sewer company in Dammam

Roof Insulation Company in Dammam

An insect control company in Dammam


Company for the control of white ants in Dammam


Company cleaning boards and Knab Dammam


fgbhn said...

Shams El Ghadeer Co. For Household Services Our company provides the best cleaning services in Riyadh since our company provides house cleaning services, removal services, leakage detection, surface insulation, pest control, cleaning service, tanks & paints service

Surface Insulation Company in North Riyadh


Furniture transfer company in Riyadh


The company revealed leaks in the north of Riyadh


Company cleaning tanks in Riyadh


An insect control company in Riyadh


Paints Company in Riyadh


Company cleaning houses in Riyadh


fgbhn said...

South Summit offers the best home services in the southern region where it provides the best home services
(Cleaning houses - cleaning trolls - water leak detection - pest control)
شركة تنظيف منازل بابها

شركة تنظيف خزانات بابها

شركة كشف تسربات بابها

شركة مكافحة حشرات بابها

شركة تنظيف منازل بخميس مشيط

شركة كشف تسربات بخميس مشيط

شركة تنظيف خزانات بخميس مشيط

شركة مكافحة حشرات بخميس مشيط

fgbhn said...

Asir Home Services Company in the Southern Region Our company offers the best home services
(Cleaning houses - cleaning tanks - insect control - leak detection)
House cleaning company Balqmah

Bbeisha insect control company

Cleaning company in Khamis Mushait

Company cleaning its door cabinets

House cleaning company Khamis Mushait

The company revealed leaks in Khamis Mushait

Company revealed leaks door

An insect control company in Khamis Mushait

Insect Control Company

Insect Control Company