How to create a column that is a 0-9 hash text in another column?

Our application has the following table definition:

CREATE TABLE [dbo].[Phrase] (
    [PhraseId]    UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
    [English]     NVARCHAR (250)   NOT NULL,
    [EnglishHash] AS (CONVERT([bigint],hashbytes('md5',[English])%(5)+(5))) PERSISTED,
    PRIMARY KEY CLUSTERED ([PhraseId] ASC)
);

The goal was for the EnglishHash column to be either 0,1,2,3,4,5,6,7,8 or 9

However, it gives only values: 1,2,3,4,5,6,7,8 or 9

Can someone help explain how I can change this to give values ​​0-9 inclusive?

Please note that I tried Sandip's suggestion. This gives me a distribution, but over 11,000 records distribution is not what I expected:

0   593
9   652
3   1324
6   1253
7   1293
1   1932
4   1325
5   1282
2   1295
8   635
+4
source share
2 answers

9 , 5. . , , 4 -4 ( +5). . 10 bigint .

SELECT 0%5
    , 1%5
    , 2%5
    , 3%5
    , 4%5
    , 5%5
    , 6%5

SELECT 0%5
    , -1%5
    , -2%5
    , -3%5
    , -4%5
    , -5%5
    , -6%5

ABS(CONVERT(bigint, HASHBYTES('md5',[English])))%10

.

SELECT ABS(CONVERT(bigint, HASHBYTES('md5',[text])))%10 AS 'Result'
    , COUNT(*) AS 'Distribution'
from sys.messages
GROUP BY ABS(CONVERT(bigint, HASHBYTES('md5',[text])))%10
ORDER BY ABS(CONVERT(bigint, HASHBYTES('md5',[text])))%10

:

Result  Distribution
0   25326
1   25218
2   25115
3   25322
4   25167
5   25322
6   25278
7   25119
8   25139
9   25158
+9

, :

--C=0
--B=1
--F=2
--t=3
--D=4
--S=5
--G=6
--A=7
--j=8
--P=9

DECLARE @myText VARCHAR='A'
SELECT ABS((HashBytes( 'md5', @myText ) %9)-1)
+1

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


All Articles