From BigInt to HexString in littleEndian in SQL script - sql

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

Related

Declare variable for HashBytes

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';

SQL Server : REPLACE function replace ' ' with ',' in string

I am using SQL Server 2012. My input string is
'data1''data2''data3''data4''data5'
I want output to be:
'data1','data2','data3','data4','data5'
I tried the following
DECLARE
#P_NAME VARCHAR(20),
#V_NAME_1 VARCHAR(20);
SET #P_NAME = 'E056''E056''E056''E056''E056'
SET #V_NAME_1 = REPLACE(#P_NAME,'''',''',''')
SELECT #V_NAME_1
but it does not return the expected output.
Please let me know what I am missing.
Thanks,
Amol
You need to increase the length of your VARCHAR, I've set it to 50 below. Little tweak to your SET statement too. I've changed the values for testing;
DECLARE
#P_NAME VARCHAR(50),
#V_NAME_1 VARCHAR(50);
SET #P_NAME='E056''E057''E058''E059''E060'
SET #V_NAME_1=''''+REPLACE(#P_NAME,'''',''',''')+''''
SELECT #V_NAME_1

Must declare the scalar variable error when calling stored proc

I'm having trouble getting this SP call to work. It worked previously and no changes were made, but I cannot get it to work for the life of me now.
The error code I'm getting is:
Error code 137, SQL state S0002: Must declare the scalar variable "#AppName".
This is using netbeans sql editor. if I hard code the AppName variable, it then throws the same error for AppVer. A coworker can use the exact code and it works fine in his non-netbeans ide (I think vis studio?). This leads me to believe that it's a netbeans issue rather than the code itself.
Any ideas?
DECLARE #AppName nvarchar(100) = 'Excel ASAP Utilities';
DECLARE #AppVer nvarchar(50) = '3.07b';
DECLARE #AppManufacture nvarchar(100) = null;
DECLARE #RequestedByID dbo.UserID = 'userid';
DECLARE #RequestDescription nvarchar(3500) = 'This is a test';
DECLARE #NumberNeededFor int = null;
DECLARE #AssignToID dbo.UserID = '?';
DECLARE #RequestType_ID int = 6;
DECLARE #LCM_ID int = 50;
DECLARE #ProcessID int = 3;
DECLARE #RM_ID int;
EXECUTE dbo.Request_InsertNewShortForm
#AppName,
#AppVer,
#AppManufacture,
#RequestedByID,
#RequestDescription,
#NumberNeededFor,
#AssignToID,
#RequestType_ID,
#LCM_ID,
#ProcessID,
#RM_ID OUTPUT;
SELECT 'Return Value' = #RM_ID;

Unable to retrieve value returned from mysql function

Since a week am working with MYSQL , got to execute the Stored Procedure as well as Views but Facing some problem retrieving the values returned from a function.
Here's the Function:
CREATE DEFINER=`root`#`localhost` FUNCTION `GetProductIdsStringByEnquiryId`
(
InEnquiryId int
) RETURNS varchar(4000) CHARSET utf8
BEGIN
DECLARE InProductIds varchar(4000);
DECLARE ProductId varchar(50);
DECLARE x,y,z INT;
DECLARE sp1_cursor CURSOR FOR SELECT ProductId FROM enquiryproductid where
EnquiryId=InEnquiryId;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET z = 1;
SET InProductIds='';
OPEN sp1_cursor;
REPEAT
FETCH sp1_cursor INTO ProductId;
SETInProductIds=concat(InProductIds,ProductId,',');
UNTIL (z=1)
END REPEAT;
CLOSE sp1_cursor;
RETURN InProductIds ;
END
I was initially working with SQL SERVER 2005, and the function which I have written in their I tried converting it as above in MYSQL,
Here's the SQL Function Code:
CREATE function [dbo].[GetBranchIdsStringByEmployeeId]
(
#EmployeeId as integer
)
returns nvarchar(4000)
as
begin
declare #BranchIds as nvarchar(4000)
set #BranchIds=''
if exists(select 1 from dbo.BranchEmployees where EmployeeId=#EmployeeId)
begin
select #BranchIds=#BranchIds+cast(BranchId as nvarchar(50))
+',' from dbo.BranchEmployees where EmployeeId=#EmployeeId
order by BranchId
end
return #BranchIds
end
Can anybody Please Let me know if the Function What I have written in MYSQL is in ProperManner or not? Please do help me out.
Thank You.
Not read fully through it, but few comments
Variable assignment in mysql uses := (in set #variable it is ok to use =, in select #variable:=#variable+1)
Are you trying to
SELECT group_concat(BranchId)
FROM dbo.BranchEmployees
WHERE EmployeeId = #EmployeeId
?

T-SQL: Concept similar to C# params

Does T-SQL allow a variable number of arguments to a stored procedure like params in C#?
EDIT: I'm using SQL Server 2005. That 2008 answer makes me wish we were using it...
In SQL 2008 there's Table-Valued Parameters (TVPs)
Your stored proc can accept lists of parameters..
Finally we're able to do a IN clause without relying on XML!
Mike
No, not for things like UDFs or stored procedures. That's what tables are for. Put the values in a table somewhere (with a common key) and pass the correct key to your procedure.
Typically
CREATE PROCEDURE dbo.sptest
( #xml TEXT )
AS
BEGIN
DECLARE #flag1 INT
DECLARE #flag2 VARCHAR(50)
DECLARE #flag3 DATETIME
DECLARE #idoc INT
exec sp_xml_preparedocument #idoc OUTPUT, #xml
SELECT #flag1 = firstparam, flag2 = secondparam, flag3 = thirdparam
FROM OPENXML(#idoc, '/root', 2) WITH
( firstparam INT, secondparam VARCHAR(50), thirdparam DATETIME) as x
END
exec sptest '<root><firstparam>5</firstparam><secondparam>Joes Bar</secondparam><thirdparam>12/30/2010</thirdparam></root>'
Extend as necessary
Another approach I've seen to passing in params or arrays is to pass in an XML string, dump that to a temporary table/table variable and work with it from that point. Not the easiest when you want to manually run a stored procedure, but it works as a work around to the lack of array/dynamic param support.
I've used a little function to separate a CSV string into a table
That way I could go
SELECT col1, col2
FROM myTable
WHERE myTable.ID IN (SELECT ID FROM dbo.SplitIDs('1,2,3,4,5...'))
My function is below:
CREATE FUNCTION [dbo].[SplitIDs]
(
#IDList varchar(500)
)
RETURNS
#ParsedList table
(
ID int
)
AS
BEGIN
DECLARE #ID varchar(10), #Pos int
SET #IDList = LTRIM(RTRIM(#IDList))+ ','
SET #Pos = CHARINDEX(',', #IDList, 1)
IF REPLACE(#IDList, ',', '') <> ''
BEGIN
WHILE #Pos > 0
BEGIN
SET #ID = LTRIM(RTRIM(LEFT(#IDList, #Pos - 1)))
IF #ID <> ''
BEGIN
INSERT INTO #ParsedList (ID)
VALUES (CAST(#ID AS int)) --Use Appropriate conversion
END
SET #IDList = RIGHT(#IDList, LEN(#IDList) - #Pos)
SET #Pos = CHARINDEX(',', #IDList, 1)
END
END
RETURN
END
I'm sure there are better ways to implement this, this is one way I found online and it works well for what I'm doing. If there are some improvement that can be made please comment.