Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1083053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:22:58+00:00 2026-05-16T22:22:58+00:00

I have quite a large nvarchar which I wish to pass to the HashBytes

  • 0

I have quite a large nvarchar which I wish to pass to the HashBytes function.
I get the error:

“String or binary would be truncated.
Cannot insert the value NULL into
column ‘colname’, tbale ‘table’;
column does not allow nulls. UPDATE
fails. The statement has been
terminated.”

Being ever resourceful, I discovered this was due to the HashBytes function having a maximum limit of 8000 bytes. Further searching showed me a ‘solution’ where my large varchar would be divided and hashed seperately and then later combined with this user defined function:

function [dbo].[udfLargeHashTable] (@algorithm nvarchar(4), @InputDataString varchar(MAX))
RETURNS varbinary(MAX)
AS
BEGIN
DECLARE
    @Index int,
    @InputDataLength int,
    @ReturnSum varbinary(max),
    @InputData varbinary(max)

SET @ReturnSum = 0
SET @Index = 1
SET @InputData = convert(binary,@InputDataString)
SET @InputDataLength = DATALENGTH(@InputData)

WHILE @Index <= @InputDataLength
BEGIN
    SET @ReturnSum = @ReturnSum + HASHBYTES(@algorithm, SUBSTRING(@InputData, @Index, 8000))
    SET @Index = @Index + 8000
END
RETURN @ReturnSum
END

which I call with:

set @ReportDefinitionHash=convert(int,dbo.[udfLargeHashTable]('SHA1',@ReportDefinitionForLookup))

Where @ReportDefinitionHash is int, and @ReportDefinitionForLookup is the varchar

Passing a simple char like ‘test’ produces a different int with my UDF than a normal call to HashBytes would produce.

Any advice on this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:22:58+00:00Added an answer on May 16, 2026 at 10:22 pm

    Just use this function (taken from Hashing large data strings with a User Defined Function):

    create function dbo.fn_hashbytesMAX
        ( @string  nvarchar(max)
        , @Algo    varchar(10)
        )
        returns varbinary(20)
    as
    /************************************************************
    *
    *    Author:        Brandon Galderisi
    *    Last modified: 15-SEP-2009 (by Denis)
    *    Purpose:       uses the system function hashbytes as well
    *                   as sys.fn_varbintohexstr to split an 
    *                   nvarchar(max) string and hash in 8000 byte 
    *                   chunks hashing each 8000 byte chunk,,
    *                   getting the 40 byte output, streaming each 
    *                   40 byte output into a string then hashing 
    *                   that string.
    *
    *************************************************************/
    begin
         declare    @concat       nvarchar(max)
                   ,@NumHash      int
                   ,@HASH         varbinary(20)
         set @NumHash = ceiling((datalength(@string)/2)/(4000.0))
        /* HashBytes only supports 8000 bytes so split the string if it is larger */
        if @NumHash>1
        begin
                                                            -- # * 4000 character strings
              ;with a as (select 1 as n union all select 1) -- 2 
                   ,b as (select 1 as n from a ,a a1)       -- 4
                   ,c as (select 1 as n from b ,b b1)       -- 16
                   ,d as (select 1 as n from c ,c c1)       -- 256
                   ,e as (select 1 as n from d ,d d1)       -- 65,536
                   ,f as (select 1 as n from e ,e e1)       -- 4,294,967,296 = 17+ TRILLION characters
                   ,factored as (select row_number() over (order by n) rn from f)
                   ,factors as (select rn,(rn*4000)+1 factor from factored)
    
              select @concat = cast((
              select right(sys.fn_varbintohexstr
                             (
                             hashbytes(@Algo, substring(@string, factor - 4000, 4000))
                             )
                          , 40) + ''
              from Factors
              where rn <= @NumHash
              for xml path('')
              ) as nvarchar(max))
    
    
              set @HASH = dbo.fn_hashbytesMAX(@concat ,@Algo)
        end
         else
         begin
              set @HASH = convert(varbinary(20), hashbytes(@Algo, @string))
         end
    
    return @HASH
    end
    

    And the results are as following:

    select 
     hashbytes('sha1', N'test') --native function with nvarchar input
    ,hashbytes('sha1', 'test') --native function with varchar input 
    ,dbo.fn_hashbytesMAX('test', 'sha1') --Galderisi's function which casts to nvarchar input
    ,dbo.fnGetHash('sha1', 'test') --your function
    

    Output:

    0x87F8ED9157125FFC4DA9E06A7B8011AD80A53FE1  
    0xA94A8FE5CCB19BA61C4C0873D391E987982FBBD3  
    0x87F8ED9157125FFC4DA9E06A7B8011AD80A53FE1   
    0x00000000AE6DBA4E0F767D06A97038B0C24ED720662ED9F1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a several functions which are quite large, and are only used in
I have a multi-line text view that can get quite large. When the user
We have quite Large Web Based System, which is recently converted from Website to
I have quite a large IF statement which needs to update another form element
I have quite a long script which involves chopping lots of large text files
I have a VS 2008 VB.NET Solution, which is quite large. Every once in
I have dynamic and quite large contents which needs to be populated on user
I have many subversion checkouts (~50) some of which are quite large (hundreds of
In a winforms application I have a MyForm.cs that starts to get quite large.
I have a resource that is quite large (over 100 MB). I would rather

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.