Let's assume I have the same following code in three procedures:
SELECT CASE
WHEN [column1] LIKE '%/[a-z][a-z]-[a-z][a-z]/%' THEN REVERSE ( SUBSTRING ( REVERSE ( [column1] ) ,1,PATINDEX ( '%/%',REVERSE ( [column1] )) - 1 ))
WHEN [column1] LIKE '' THEN ISNULL ( NULLIF ( [column1],'' ) ,'(not set)' )
ELSE 'Other'
END AS ColumnReturned
FROM X
1) Is it possible to have a "function" instead of this piece of code stored somewhere else, in one place? So then I can call only:
SELECT myFunction (column1)
FROM X
in the procedures. So if I want to change the logic, I'd change it only in one place.
2) If I could have such function as per 1) point, can I have also multiple functions in one "file"? So I have list of different functions in one place? Then I'd be able to call:
SELECT functionList.myFunction1 (column1)
,functionList.myFunction2 (column2)
FROM X
Or the functions need to be separeated and only share scheme?
I am new to T-SQL functions so will be happy if you could point me some starting point.