Share via

Convert C# to sql

Ebrahim El-Sayed 21 Reputation points
2021-12-29T10:11:18.097+00:00

how to convert this to Sql Function

string Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string R = "";
long N = 324535354; // param
do { R += Chars[(int)(N % 0x3E)]; } while ((N /= 0x3E) != 0);
return R;

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2021-12-29T12:29:01.203+00:00

Try this function:

create or alter function MyFunction( @N bigint ) 
returns varchar(max)
as
begin
    declare @R varchar(max) = ''
    declare @Chars varchar(max) = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

    while 1 = 1
    begin
        set @R += substring( @Chars, (@N % len(@Chars)) + 1, 1)
        set @N /= len(@Chars)
        if @N = 0 break
    end

    return @R
end

Example:

select dbo.MyFunction( 324535354 )

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Ebrahim El-Sayed 21 Reputation points
    2022-01-02T12:21:00.03+00:00

    ALTER function [dbo].[Function] (@omarcanchanya varchar(max))
    returns varchar(max)
    as begin
    -- Declare the return variable here
    declare
    @input VARCHAR(100),
    @output VARCHAR(100)

    SET @input = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    SELECT @output = '';
    WHILE @omarcanchanya <> 0
    BEGIN
    SET @output = @output + SUBSTRING(@input,(@omarcanchanya % 62),1);
    SET @omarcanchanya /= 62;
    end

    return @output

    END

    Was this answer helpful?


  2. LiHong-MSFT 10,061 Reputation points
    2021-12-30T06:01:52.513+00:00

    Hi,@Ebrahim El-Sayed
    We can use the BREAK keyword to exit the loop,as Viorel and DanGuzman answered above.
    In this situation, we can also modify the WHILE conditional judge statement to achieve the desired result.
    Please check this:

    IF OBJECT_ID('dbo.udf_test')IS NOT NULL   
    DROP FUNCTION dbo.udf_test;  
    GO   
    CREATE FUNCTION dbo.udf_test(@N bigint)  
    RETURNS varchar(100)  
    AS  
     BEGIN  
         DECLARE @Chars varchar(100) = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';  
         DECLARE @R varchar(100) = '';  
      WHILE @N != 0  
      BEGIN  
      SET @R = @R + SUBSTRING(@Chars, @N % CAST(0x3e AS int) + 1, 1);  
      SET @N = @N / CAST(0x3e AS int);  
      END;  
      RETURN @R;  
     END;  
     GO  
      
    SELECT dbo.udf_test(324535354);  
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

  3. Dan Guzman 9,516 Reputation points
    2021-12-29T13:07:39.467+00:00

    T-SQL doesn't have a DO control flow statement but one can provide the same functionally with a BREAK condition on the exit criteria:

    CREATE OR ALTER FUNCTION dbo.Example(@N bigint)
    RETURNS varchar(100)
    AS
    BEGIN
        DECLARE @Chars varchar(62) = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        DECLARE @R varchar(50) = '';
        WHILE 1 = 1
        BEGIN
            SET @R += SUBSTRING(@Chars, @N % CAST(0x3e AS int) + 1, 1);
            SET @N /= CAST(0x3e AS int);
            IF @N = 0 BREAK;
        END;
        RETURN @R;
    END;
    GO
    

    Example usage:

    SELECT dbo.Example(324535354);
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.