1) why patindex('%[1-9][0-9][0-9][0-9]%', Period) return only 4 & 1 ?
The pattern looks for a sequence of four digits, of which the first digits must not be 0. In your sample data, the year starts in either position 1 or 4.
2) why four [0-9] bracket is there in patindex() with 0-9 ? one or two would be enough
In the light of my answer to your first question, why would one or two be
enough?
3) why first option is [1-9] and rest is [0-9] ?
There is something in the business logic, I guess. And I am not familiar with the underlying requirements. But presumably the programmer thought that if there is a sequence like 0898 it is not a year, and the programmer did not want to be involved.
4) how below sql is working not clear. please help me.
The query may be somewhat easier to understand if we rewrite as:
select q1.Period,
NewPeriod = q1.Period +
CASE WHEN q1.r <= (SELECT r FROM Q WHERE Period = '2Q 2019') THEN 'A' ELSE 'E' END
from Q q1
order by q1.y, q1.r
The idea is apparently that if the period is before the second quarter of 2019, we should tack on A, else we should tack on E. Don't ask me why.