>>create function dbo.ClaimItemStatus >>( >> @ClaimItemPk int, >> @Start Date, >> @End Date, >> @DayLimit Int >>) >>RETURNS TABLE >>AS >> >>with cte as (select case when @Start IS NOT NULL and @End IS NOT NULL >> THEN 'Settled' >> WHEN @Start IS NOT NULL > >**** I'm getting a syntax error on the case here: Expecting AND, OR, or THEN**** >> case when @DayLimit > (select count(*) .... ) -- here we can either use direct query or your scalar function - I prefer direct count > >> THEN 'OverDue' >> else >> 'Pending' >> END >> ELSE -- what else is not covered ? >> 'Unknown' >> END as StatusDescr) >> >>select StatusDescr, case StatusDescr WHEN 'Settled' then 2 when 'Pending' then 4 when 'OverDue' then 3 when 'Unknown' then 5 end as [Status] >>from cte >> >>GO >>The error is very explicit. You're missing THEN keyword before inner case starts.