It is interesting to know what the format of this answer will be in this version.

    1 answer 1

    If all three text fields in one table, and the formatting function is needed only for this table, then, in my opinion, the easiest way to do this is with the help of the computed column (computed column):

    create table Students ( FirstName nvarchar(50) not NULL, LastName nvarchar(50) not NULL, MiddleName nvarchar(50) not NULL, FullName as LastName + ' ' + left(FirstName, 1) + '.' + left(MiddleName, 1) + '.' ) 

    Then, when selecting, simply specify the name of the calculated column:

     select FullName from Students 

    The calculation of FullName will occur when referring to this column. Also, when creating a table, you can mark the calculated column with the persisted keyword:

     FullName as ... persisted 

    In this case, its value will be stored in a table, and recalculated when data changes in the original columns. When sampling, the calculation will not occur, but the calculated stored value will be delivered.

    You can also implement it through a function (for example, if the columns are not in the same table, or it will be applied in the same type to several similar tables). For application in the request to several lines of inline table function will be more profitable than a scalar. An example of such a function for this case:

     create function dbo.tfFullName ( @firstName nvarchar(50), @lastName nvarchar(50), @middleName nvarchar(50) ) returns table as return select FullName = @lastName + ' ' + left(@firstName, 1) + '.' + left(@middleName, 1) + '.' GO 

    Using:

     select f.FullName from Students s cross apply dbo.tfFullName(s.FirstName, s.LastName, s.MiddleName) f GO