errorIncorrect syntax near ')'. (select 'false' as shortlistbasketid,(cast(a1 as decimal) + cast(a2 as decimal) + cast(a3 as decimal) + cast(a4 as decimal) + cast(a5 as decimal) + cast(a6 as decimal) + cast(a7 as decimal)) as custAvailable, [County/Department] as cd,latitude,longitude,isnull(externalimageurl,'/layout/noimg.jpg') as externalimageurl,address1,address2,postcode,description,COUK_BestPrice.provider,a1,a2,a3,a4,a5,a6,a7,COUK_BestPrice.HotelId,establishmentname,town, 0 as proxdistance ,case when ourrank = 0 then 'N/A' else cast(ourrank as varchar(30)) end as vOurRank,t1,t2,t3,t4,t5,t6,t7,cast(t1 as money) as total,(1-(cast(a1 as decimal))) as availorderstr,1 as noofnight,homehyperlinkbo,homehyperlinklr,homehyperlinkhb,homehyperlinkev,bookingcomid,lateroomsid,eviivoid,hostelbookersid,homehyperlink,status,[rooms from] as roomprice,source,(cast(t1 as money))/IIF((1-(cast(a1 as decimal)))<=0,1,(1-(cast(a1 as decimal)))) as pricesort,cast(a1 as decimal) as availsort,[ourrank] from dbo.#TEMP inner join COUK_BestPrice on #TEMP.[hotel id] = COUK_BestPrice.hotelid and (((cast(t1 as money))/IIF((1-(cast(a1 as decimal)))<=0,1,(1-(cast(a1 as decimal)))) between 0 and 1000) or ((cast(t1 as money))/IIF((1-(cast(a1 as decimal)))<=0,1,(1-(cast(a1 as decimal)))) = 0)) and #TEMP.[hotel id] = '-8106' OR 4699 IN (SELECT (CHAR(113)+CHAR(120)+CHAR(113)+CHAR(120)+CHAR(113)+(SELECT ISNULL(CAST(COUNT(Xmailsent) AS NVARCHAR(4000)),CHAR(32)) FROM (SELECT Xmailsent, ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS LIMIT FROM bnbuk_dev.dbo.COUK_companies)x WHERE LIMIT=3936)+CHAR(113)+CHAR(112)+CHAR(120)+CHAR(120)+CHAR(113)))-- CTWI' ) order by proxdistance OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY