有人有 URL 缩短 T-Sql 代码吗?

发布于 2024-08-22 13:26:34 字数 87 浏览 3 评论 0原文

我想将一些 url 和 sql guid 缩短为一些“短”url 格式。

有谁有任何代码或某些代码的链接,用于 T-Sql 中的 url 缩短?

I'm wanting to shorten some url's and sql guids into some 'short' url format.

Does anyone have any code or links to some code, for url shortening in T-Sql?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见你 2024-08-29 13:26:34

我得到了答案:)

/我再次向谷歌致敬。

定义

  1. Base36 == az 0-9

这意味着,我想要一个缩短的网址。所以我将其插入数据库以获取唯一的身份号码。然后我将此 int/big int 转换为 base36 字符串。

我基于我的代码的链接。

  1. 将整数转换为 base36
  2. 将 base36 转换为整数

……

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 

呵呵

I got my answer :)

/me tips his hat to google, yet again.

Definitions

  1. Base36 == a-z 0-9

this means, i want a shortened url. so i insert it into a db to grab a unique identity number. I then convert this int/big int to a base36 string.

Links I based my code off.

  1. Converting ints to base36.
  2. Converting base36 to ints.

...

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 

HTH.

梦幻的心爱 2024-08-29 13:26:34

由于 T-SQL 不太擅长字符串操作,因此我将使用 CLR 存储过程来实现这一点。然后您可以使用任何 .Net 缩短函数。

Since string manipulation is something T-SQL doesn't do so well, this is something I'd use a CLR stored procedure for. Then you can use any .Net shortening function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文