Convert hexadecimal value into bigint - sql-server

I am getting some trouble converting a string (representing a hexadecimal number) into a bigint. I would also like this to happen inside a function and as effiecient as possible.
Is there anyway of exploiting the built-in functions?
Here is an example of what I want to do:
select convert (bigint, '0000010d1858798c')

The SQL Server 2008 release updated the CONVERT() function to be able to convert hexadecimal values:
select convert(bigint, convert (varbinary(8), '0x0000010d1858798c', 1))
Result:
1155754654092 (decimal) ( == 0x0000010d1858798c )

Related

Does snowflake have a ‘hex’ to ‘int’ type native function?

This FAQ answer says that Snowflake doesn't have a native way to convert from hex to int, and suggests a JS alternative:
https://community.snowflake.com/s/article/faq-does-snowflake-have-a-hex-to-int-type-function
But is there a way to convert from hex to bin with pure SQL in Snowflake?
The FAQ answer is wrong (we are going to fix it soon), as there is a native way to convert hex to int in Snowflake:
select to_number('fff','XXX');
-- 4095
Note that to_number needs to have an hexadecimal format string of at least the length of the hexadecimal input. It can be longer too for safety:
select to_number('fff','XXXXXXXXXXX');
-- 4095
Or you can construct the format string out of the length of the input hex string:
select to_number(h, repeat('X', length(h)))
from (
select 'fff' h
);
-- 4095
select to_number(h, repeat('X', length(h)))
from (
select 'fffffffffffffffffffffffffffffff' h
);
-- 21267647932558653966460912964485513215

Convert HASBYTES function output to CHAR

I know that the best data type for storing output of the HASHBYTES function is BINARY/VARBINARY, but we want to store it as CHAR as it is suggested by DataVault best practices, moreover not all tolls support keys of BINARY types, for example PowerBI.
So the question is, how to convert it to CHAR? When I do simple CAST/CONVERT I get different output rather than raw output of HASHBYTES function.
SELECT CONVERT(CHAR(32), HASHBYTES('MD5', 'test'))
SELECT CAST(HASHBYTES('MD5', 'test') AS CHAR(32))
SELECT HASHBYTES('MD5', 'test')
Expected value is 098F6BCD4621D373CADE4E832627B4F6 (without 0x) and actual value is kÍF!ÓsÊÞNƒ&'´ö
--convert binary to char without 0x prefix, using style 2
SELECT CONVERT(CHAR(32), HASHBYTES('MD5', 'test'), 2);

T-SQL Scientific notation field conversion

I load excel file into sql as varchar(max) and got that Scientific e value which now I try to convert into numeric as I need to do compare that value, and here I'm running into problem.
This is main question: How and to what type I can convert this to compare with whole integer value ?
On the pic You can see how this seen in Excel, even formatted to text it somehow still loaded into varchar(max) not like char string. This can be seen from my test code.
DECLARE #C VARCHAR(MAX) = '1.1001562717e+011', #Nc VARCHAR(MAX) = '110015627174';
SELECT #c, LEN(#c) LenC ,
ISNUMERIC(#c) NumYN
---, CAST(#c AS DECIMAL(38,2)) cDec ---CAST(#c AS NUMERIC) cNum --, CAST(#c AS BIGINT) cInt
WHERE #c LIKE '%[^0-9]%'
AND ISNUMERIC(#c) = 1
To start, ISNUMERIC is a terrible function, it does not give good results; it is often wrong. If you try ISNUMERIC('1.1001562717e+011') you'll notice that you get the value 1, however, CONVERT(numeric(13,1),'1.1001562717e+011') will produce an error. A far better function is TRY_CONVERT (or TRY_CAST), which returns NULL if the conversion fails for the specific data type: TRY_CONVERT(numeric(13,1),'1.1001562717e+011').
Being specific on the data type is actually important here, as ISNUMERIC could be (incorrectly) suggesting that the value could be converted to at least 1 of the numeric data types; but that doesn't mean all of them. For scientific data types the only data type you can convert to is a float/real:
SELECT TRY_CONVERT(numeric(13,1),'1.1001562717e+011') AS Numeric,
TRY_CONVERT(bigint,'1.1001562717e+011') AS int,
TRY_CONVERT(float,'1.1001562717e+011') AS float,
TRY_CONVERT(money,'1.1001562717e+011') AS money;
Notice that only float has a value here. As you want a numeric as the final value, then you'll need to CONVERT the value twice:
CONVERT(numeric(13,1),TRY_CONVERT(float,'1.1001562717e+011'))

How to convert decimal value to hexadecimal in SQL SERVER

How to convert decimal value to hexadecimal in SQL SERVER..
example I have ID Decimal(20,0) value(141021750051366541) to convert into hexadecimal something like this 06ca02dc04426208
If your number fits into bigint, you can simply format(cast(#yourNumber as bigint), 'x')

How to convert datetime to int in sql server2012?

Here is my question:
This is a table named HB06,the data type of "WRTime" is datatime.I want to convert all WRTime to int. For example 2012-11-09 10:52:38.000 will be converted to 20121109105238.
Thank you!
You can't. The value is too large for the int data type. But it does fit into a bigint.
What you can do is to convert it to a string with the desired format, and then cast that ti the bigint type. Using the FORMAT function is IMO more straight forward:
DECLARE #a datetime = '20120304 23:34:12'
SELECT #a
SELECT CAST(FORMAT(#a, 'yyyyMMddhhmmss') AS bigint)
The alternative is to use CONVERT function, which uses less CPU. But there's not direct style that matches that format, so you would then REPLACE() various "litter" characters with nothing. I wouldn't bother with the CONVERT() option unless you work over large data sets.
select
cast(replace(replace(replace(convert(varchar(19), WRTime, 121),':',''),'-',''),' ','') as bigint)
FROM HB06
You can try with below one
select concat(convert(varchar,WRTime,112),datepart(HH,WRTime),
datepart(MINUTE,WRTime),datepart(SS,WRTime)) from HB06
Use below Convert function:
SELECT CONVERT(VARCHAR(100),WRTime,112)+REPLACE(CONVERT(VARCHAR(100),WRTime,108),':','')
FROM HB06
SQL Version 2012 or higher you can use the FORMAT function to get just year and month, then cast it as an int.
On versions prior to 2012 you can do the formatting with the convert function, then cast as int.
declare #WRTime datetime
set #WRTime = '2012-11-09 10:52:38.000'
select cast(format(#WRTime,'yyyyMM') as int) --2012 or higher
OR You can use:
SELECT YEAR(#WRTime)*100 + MONTH(#WRTime);
2012-11-09 10:52:38.000 will be converted to 20121109105238??
Int can't convert this so use "BIGINT"
declare #WRTime datetime
set #WRTime = '2012-11-09 10:52:38.000'
select cast(format(#WRTime,'yyyyMMddHHmmssfff') as bigint) --2012 or higher

Resources