Declare variable for HashBytes - sql

I am following simple example from this question:
DECLARE #HASHBYTES VARBINARY(128) = hashbytes('sha2_256', 'secret')
SELECT cast(N'' as xml).value('xs:base64Binary(sql:variable("#HASHBYTES"))', 'varchar(128)');
It return a correct hashed value: K7gNU3sdo+OL0wNhqoVWhr3g6s1xYv72ol/pe/Unols=
Now I tried to declare secret as variable following Microsoft Hashbytes documentation example:
DECLARE #HashThis nvarchar(32);
SET #HashThis = CONVERT(nvarchar(32),'secret');
DECLARE #HASHBYTES VARBINARY(128) = hashbytes('sha2_256', #HashThis)
SELECT cast(N'' as xml).value('xs:base64Binary(sql:variable("#HASHBYTES"))', 'varchar(128)');
it return a wrong hash:
s6jeuSg7FUrj9WBqMj0GbSRudEDi2NTJqHqmG5UYOiY=
Is there any way to declare secret as variable to get the correct hash?
I am new in to this Hashbytes stuff in SQL. I am using it on SQL Server 2017.

The issue is that you are using nvarchar to declare your secret. but it should be varchar and it would solve the problem.
So lets test it:
DECLARE #HashThis varchar(32);
SET #HashThis = CONVERT(varchar(32),'secret');
DECLARE #HASHBYTES VARBINARY(128) = hashbytes('sha2_256', #HashThis)
SELECT cast(N'' as xml).value('xs:base64Binary(sql:variable("#HASHBYTES"))', 'varchar(128)');
Will return what you originally expected:
K7gNU3sdo+OL0wNhqoVWhr3g6s1xYv72ol/pe/Unols=
Btw, you do not need to CONVERT, you can just pass secret as varchar. some thing like:
DECLARE #HashThis varchar(32);
SET #HashThis = 'secret';

Related

From BigInt to HexString in littleEndian in SQL script

I have the following code in Sql Server 2019.
declare #Id bigint
declare #HexId varchar(50)
set #Id = 98360090175733911
set #HexId = CONVERT(VARCHAR(50),CONVERT(VARBINARY(16),#Id),2)
select #HexId
This is working, but the result must be in little endian.
Can someone help me with this problem?
Kind regards,
Bert Berrevoets.
I have tried the reverse function, but this was not ok.
to get the string i used the internal function master.dbo.fn_varbintohexstrit seems to be the format you wanted
declare #Id bigint
declare #HexId varchar(50)
set #Id = 98360090175733911
set #HexId = master.dbo.fn_varbintohexstr(CONVERT(BINARY(16), REVERSE(CONVERT(BINARY(16), #Id))))
select #HexId
Get you
0x9700001bfb715d010000000000000000

MS SQL - Variable in a variable

I've been wondering if it's possible to declare and set a variable within the definition of another variable.
For example:
declare #variable varchar(250)
set #variable =
'INSERT INTO [BLAH] (Nope, Hype, Friends)
VALUES (declare #value varchar(250) set #value = 'example' exec #value, #value, #value)'
exec #variable
I'm probably doing multiple really basic mistakes here - just trying to understand as much as fast as possible
it can be done as below.
declare #variable varchar(800)
set #variable =
' declare #value varchar(10) = ''example''
INSERT INTO [BLAH] (Nope, Hype, Friends)
VALUES ( #value, #value, #value)'
exec #variable
Also get a knowledge of SQL injection before you work with dynamic sql.
Yes you can declare a set of variable inside a variable: doing like this.
DECLARE #variable varchar(max)
SET #variable =
'declare #value varchar(max) = ''example''
Insert into BLAH
values(#value,#value)'
EXEC (#variable)
Here we simply declared a variable #value inside another variable #variable

sql print returns strange result

I have the following sql code:
DECLARE #result VARCHAR
SET #result = 'FN'
print(#result)
It returns F to console.
why?
If you define a VARCHAR, you have to specify it's length. If you don't it defaults to 1.
Use:
DECLARE #result VARCHAR(2)
SET #result = 'FN'
print(#result)

CAST always returns 1

I'm executing this TSQL Code:
DECLARE #myString varchar;
SET #myString = '123.0'
SELECT CAST(#myString as decimal(25,10))
But I keep getting 1.00000 as an result
Changing myString to '123' doesn't change that.
Any advise on what I'm doing wrong is appreciated.
Thanks in advance!
ALWAYS use length when using varchar() (and related types) in MySQL. The default is 1 in this context. So this fixes your problem:
DECLARE #myString varchar(255);
SET #myString = '123.0';
SELECT CAST(#myString as decimal(25,10));
You are getting 1, because your code is interpreted as
DECLARE #myString varchar(1);
SET #myString = '123.0';
SELECT CAST(#myString as decimal(25,10));
The documentation is not shy about this:
When n is not specified in a data definition or variable declaration
statement, the default length is 1. When n is not specified when using
the CAST and CONVERT functions, the default length is 30.
You are missing the varchar declareation
DECLARE #myString varchar(10);
SET #myString = '123.0'
SELECT CAST(#myString as decimal(25,10))

Parse JSON string in SQL

I have a column of JSON strings in my SQL table. I want to extract the 'page' value, any idea how?
{"action.type":"click","page":1424}
Hive actually has a command just for that get_json_object
Here is a pure SQL version.
DECLARE #json varchar(64);
DECLARE #index int;
SELECT #json = '{"action.type":"click","page":1424}';
DECLARE #length int = LEN(#json);
DECLARE #pageIndex int = CHARINDEX('page":', #json);
DECLARE #difference int = #length - (#pageIndex + 6); -- 6 is for page":
SELECT #index = CHARINDEX('page', #json);
SELECT SUBSTRING(#json, #index + 6, #difference);
This will give you a result of 1424
It is really long-winded, but it shows step-by-step how it get's that value. You can easily refactor that into a stored procedure.