0

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)?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.