You can't use a variable as a length specification for varchar type. Just use varchar(max)
as a variable datatype and you'll be OK.
HASHBYTES should return a string
-
05-07-2023 - |
Question
I wrote a function:
CREATE FUNCTION dbo.HashConvert
(
@value varchar(max)
@len int
)
RETURNS varchar(max)
BEGIN
declare @h varchar(max)
set @h = convert(varchar(@len), hashbytes("sha1", substring(@value, 1, @len))
/*other code*/
END
Can I use convert
is this way? I need h
to have a length equal to @len
set @h = SUBSTRING(CONVERT(varchar(MAX), HASHBYTES('SHA1', SUBSTRING(@value, 1, @len)),2),1,@len)
Is it ok? Or better to use varchar(22)
?
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow