Let’s say we have this values in the database: num — 1 2 4 5 6 8 9 11 To Find the first missing value, we can use this: ;WITH CteRN AS( SELECT *, RN = num – ROW_NUMBER() OVER(ORDER BY num) FROM tbl ) SELECT TOP 1 num – RN FROM CteRN WHERE RN […]
Tag: cte
Get a Tree view with SQL Query
First of all, I must thank to Maulik Dhorajia for his great post. That post saved me a lot. That’s the @Company table we will use: And here is the SQL Query we need to use: — Working Example ;WITH CTECompany AS ( SELECT EmpID, ParentID, PersonName , 0 AS HLevel, CAST(RIGHT(REPLICATE(‘_’,5) + CONVERT(VARCHAR(20),EmpID),20) AS […]