Does anyone have a url shortening T-Sql code?

I want to shorten some URLs and sql links into some “short” URL format.

Does anyone have code or links to some code to shorten URLs in T-Sql?

+3
source share
2 answers

I got my answer :)

/ me calls his hat google again.

Definitions

  • Base36 == az 0-9

This means that I want a shortened URL. so i paste it in db to get a unique id. Then I convert this int / big int to base36 string.

Links I have disabled my code.

...

CREATE FUNCTION [dbo].[ConvertIntegerToBase36] 
(
    @IntegerValue INTEGER
)
RETURNS VARCHAR(50)
AS
BEGIN

    DECLARE @Result VARCHAR(100) = '',
        @ShortCharacterSet VARCHAR(36) = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    WHILE @IntegerValue > 0 BEGIN
        SET @Result = SUBSTRING(@ShortCharacterSet, @IntegerValue % LEN(@ShortCharacterSet) + 1, 1) + @Result;
        SET @IntegerValue = @IntegerValue / LEN(@ShortCharacterSet);
    END

    RETURN @Result
END

...

CREATE FUNCTION [dbo].[ConvertBase36ToInteger]
(
    @EncodedValue VARCHAR(MAX)
)
RETURNS INT
AS
BEGIN
    -- Decoding encoded-strings to ints: http://dpatrickcaldwell.blogspot.com/2009/05/converting-hexadecimal-or-binary-to.html

    DECLARE @Result INTEGER = 0,
        @Index INTEGER = 0,
        @ShortCharacterSet VARCHAR(36) = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    WHILE @Index < LEN(@EncodedValue)  
         SELECT @Result = @Result + POWER(LEN(@ShortCharacterSet), @Index) *   
                          (CHARINDEX  
                             (SUBSTRING(@EncodedValue, LEN(@EncodedValue) - @Index, 1)  
                             , @ShortCharacterSet) - 1  
                          ),  
                @Index = @Index + 1;  


    RETURN @Result
END 

NTN.

+3
source

- , T-SQL , , CLR. .Net.

+1

Source: https://habr.com/ru/post/1733814/


All Articles