D
D
dienerie2022-04-05 16:12:10
SQL Server
dienerie, 2022-04-05 16:12:10

How to automatically generate a hash in the field for new lines?

It is necessary to generate a hash for new entries, for example, for 20 characters. I figured out how to do it using the UNIQUEIDENTIFIER data type and the default NEWID () values, but a GUID is generated there. And you need a code shorter and without a dash.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2022-04-05
@dienerie

short code without dash
SELECT LEFT(REPLACE(CONVERT(varchar(255), NEWID()),'-',''),20)

I want like YouTube .
You can convert a numeric counter to symbols, for example:
SELECT dbo.IntToAlpha( 1136572 )
cmrii
CREATE OR ALTER FUNCTION dbo.IntToAlpha ( @IN INT ) RETURNS VARCHAR(16)
WITH EXECUTE AS CALLER
AS
BEGIN
  DECLARE @ST VARCHAR(16) = ''
  WHILE @IN > 0 BEGIN
    SET @ST = CHAR((@IN % 26) + 97 ) + @ST
    SET @IN = @IN / 26
  END
  RETURN @ST
END
--
GO

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question