Function to Print Words Side by Side in SQL Server

Function to Print Words Side by Side in SQL Server
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.

Function to Print Words Side by Side in SQL Server

As you can see, we have written the initials of the word.

Good luck to everyone in business and life.

26 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!