reverse string

We have a string, such as: DECLARE @Source VARCHAR(MAX)= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' Now I want to reverse it: ZYXWVUTSRQPONMLKJIHGFEDCBA A...

We have a string, such as:

DECLARE @Source VARCHAR(MAX)= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'


Now I want to reverse it:

ZYXWVUTSRQPONMLKJIHGFEDCBA


At this point, you can write a method to handle:

SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Insus.NET -- Create date: 2019-05-16 -- Update date: 2019-05-16 -- Description: Reverse character CREATE FUNCTION [dbo].[svf_ReverseString] ( @Source VARCHAR(MAX) ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @Destination VARCHAR(MAX) = '' WHILE LEN(@Source) > 0 BEGIN IF LEN(@Source) = 0 BEGIN SET @Destination = @Source + @Destination SET @Source = '' END ELSE BEGIN SET @Destination = SUBSTRING(@Source, 1, 1) + @Destination SET @Source = SUBSTRING(@Source, 2, LEN(@Source)) END END RETURN @Destination END GO
Source Code

9 November 2019, 11:02 | Views: 7384

Add new comment

For adding a comment, please log in
or create account

0 comments