Function to Print Words Side by Side in SQL Server
Hello everyone. In this article, I will try to give information about the function that prints the initials of words side by side in SQL Server.
In SQL Server, in some cases, you may want to print the initials of the words of a given sentence side by side.
You can easily do this using the function below.
CREATE FUNCTION [dbo].[fn_Cumle_Bas_Harfi_Alma] ( @cumle NVARCHAR(4000) )
RETURNS NVARCHAR(2000)
AS
BEGIN
DECLARE @donensonuc NVARCHAR(2000);
SET @cumle=RTRIM(LTRIM(@cumle));
SET @donensonuc=LEFT(@cumle,1);
WHILE CHARINDEX(' ',@cumle,1)>0 BEGIN
SET @cumle=LTRIM(RIGHT(@cumle,LEN(@cumle)-CHARINDEX(' ',@cumle,1)));
SET @donensonuc+=LEFT(@cumle,1);
END
RETURN @donensonuc;
END
GO
--Using the Function
SELECT dbo.fn_Cumle_Bas_Harfi_Alma('Yavuz Selim Kart') AS SadeceBasHarfler
When you create and run the above function, you will see a result similar to the one below.
As you can see, we have written the initials of the word.
Good luck to everyone in business and life.