IF Exists ( select Code from PR_Header where Code = (Select 'PR-'+''+ CAST(Count(a.ID)+1 as varchar ) from PR_Header a Group by a.Company))Begin Select 'PR-'+''+ CAST(Count(ID)+2 as varchar ) as PR_PR ,'Default PR',Company from PR_Header Group by CompanyEndElse Select 'PR-'+''+ CAST(Count(ID)+1 as varchar ) as PR_PR ,'Default PR',Company from PR_Header Group by Company

You are quite welcome. I would caution you that there are a few things in that code that I would recommend changing. You are concatenating empty strings which is totally pointless. You also have a number of cast to varchar where you don't specify the size of the varchar. Do you know what the default size for a varchar is? I can't remember either. This is why you should always add the size to your declaration.