SELECT 'SP'+SUBSTRING(CAST(YEAR(GETDATE()) AS NVARCHAR(MAX)),3,2)+SUBSTRING('00',1,2-LEN(CAST(MONTH(GETDATE()) AS NVARCHAR(MAX))))+CAST(MONTH(GETDATE()) AS NVARCHAR(MAX))
+SUBSTRING('00',1,2-LEN(CAST(DAY(GETDATE()) AS NVARCHAR(MAX))))+CAST(DAY(GETDATE()) AS NVARCHAR(MAX))
+substring('000',1,3-len(cast(cast(substring(MAX(REF_NO),9,3) AS INT)+1 AS NVARCHAR(MAX))))+cast(cast(substring(MAX(REF_NO),9,3) AS INT)+1 AS NVARCHAR(MAX)) FROM Order_Rech
WHERE LEFT(ref_no,2)='SP'