declare @ownerId int = ; declare @ownerType varchar(3) = ; with mfrIdsList as ( select distinct substring(opr.mfrCustNum,1, CHARINDEX(':', opr.mfrCustNum)-1) as mfrIds from tbl_OPR_ClientOperators opr with (nolock) where ownerId = @ownerId and fslTableCode = @ownerType and len(mfrCustNum) > 0 ), mfrClients as ( select mfr_id as mfrId, mfr_name as companyname from tblManufacturers mfr with (nolock) inner join mfrIdsList m on m.mfrIds = mfr.mfr_id and mfr_id is not null ) #forJSON( 'mfrClients', attributes.datastore, 'mfrId' )#