How to convert decimal value to hexadecimal in SQL SERVER - 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')

Related

how does SQL Server actually store russian symbols in char?

I have a column NAME, which is CHAR(50).
It contains the value 'Рулон комбинированный СТЕРИТ 50мм ? 200 м'
which integer representation is:
'1056,1091,1083,1086,1085,32,1082,1086,1084,1073,1080,1085,1080,1088,1086,1074,1072,1085,1085,1099,1081,32,1057,1058,1045,1056,1048,1058,32,53,48,1084,1084,32,63,32,50,48,48,32,1084'
but CHAR implies that it contains 8 bit. How does SQL Server store values like '1056,1091,1083,1086,1085' which are UNICODE symbols?
OK, and also ? symbol is actually × (215) (Multiplication Sign)
If SQL Server can represent '1056' why it can't represent '215'?
What the 255 values in a char mean is determined by the database collation. For Russia this is typically Cyrillic_General_CI_AS (where CI means Case Insentitive and AS means Accent Sensitive.)
There's a good chance this matches Windows code page 1251, so л is stored as hex EB or decimal 235. You can verify this with T-SQL:
create database d1 collate Cyrillic_General_CI_AS;
use d1
select ascii('л')
-->
235
In the Cyrillic code page, decimal 215 means Ч, not the multiplication sign. Because SQL Server can't match the multiplication sign to the Cyrillic code page, it replaces it with a question mark:
select ascii('×'), ascii('?')
-->
63 63
In the Cyrillic code page, the char 8-bit representation of the multiplication sign and the question mark are both decimal 63, the question mark.
I have a column NAME, which is CHAR(50).
It contains the value 'Рулон комбинированный СТЕРИТ 50мм ? 200 м'
which integer representation is:
'1056,1091,1083,1086,1085,32,1082,1086,1084,1073,1080,1085,1080,1088,1086,1074,1072,1085,1085,1099,1081,32,1057,1058,1045,1056,1048,1058,32,53,48,1084,1084,32,63,32,50,48,48,32,1084'
Cyted above is wrong.
I make a test within a database with Cyrillic collation and integer representation is different from what you showed us, so or your data type is not char, or your integer representation is wrong, and yes, "but CHAR implies that it contains 8 bit" is correct and here is how you can prove it to youerself:
--create table dbo.t (name char(50));
--insert into dbo.t values ('Рулон комбинированный СТЕРИТ 50мм ? 200 м')
select cast (name as binary(50))
from dbo.t;
select substring(cast (name as binary(50)), n, 1) as bin_substr,
cast(substring(cast (name as binary(50)), n, 1) as int) as int_,
char(substring(cast (name as binary(50)), n, 1)) as cyr_char
from dbo.t cross join nums.dbo.nums;
Here dbo.Nums is an auxiliary table containig integers. I just convert your string from char field into binary, split it byte per byte and convert into int and char.

Error converting data type varchar to numeric when converting substring to decimal

I have a table that includes a VARCHAR column populated with values like
Employee is not entitled to SSP because their average weekly earnings are below LEL.Average Earnings: £44.13750
I am trying to extract the value after the £ sign so I have tried:
SELECT
ClientId
, Max(PayPeriod) As PeriodNo
, CASE WHEN MAX(Comment) like '%£%' THEN
CAST(MAX(right(Comment, charindex('£', reverse(Comment))-1) )AS DECIMAL(5,2))
ELSE 0 END AS AvgEarnings2
FROM
t_PayrollSSPEmployeeComment
WHERE
comment like '%LEL%' AND comment like '%£%'
GROUP BY
ClientID
I only want to retrieve the value so I can cast it to decimal like 110.25
But I am getting an error
Error converting data type varchar to numeric.
I can get the value with or without the £ sign, but I need to cast it to the right number of decimal places for a report.
What am I doing wrong here?
Can you try this, SUBSTRING function can be used to extract number after character £.
SELECT
ClientId
,Max(PayPeriod) As PeriodNo
,CASE WHEN MAX(Comment) like '%£%' THEN
CAST(SUBSTRING(MAX(Comment),2,LEN(MAX(Comment))) AS DECIMAL(18,2))
ELSE 0 END AS AvgEarnings2
FROM t_PayrollSSPEmployeeComment
WHERE comment like '%LEL%' AND comment like '%£%'
GROUP BY ClientID
If you are using SQL Server 2012 or higher, use Try_Convert function:
Try_Convert (Decimal(5, 2), MAX(right(Comment, charindex('£', reverse(Comment))-1) ))
and see, wherever your error code does not convert the expression to decimal type, instead of error, the result will be null and check the problem with that value.

Some doubts related Microsoft SQL Server bigint

I have the following doubt related to Microsoft SQL Server. If a bigint column has a value as 0E-9, does it mean that this cell can contain value with 9 decimal digits or what?
BIGINT: -9,223,372,036,854,775,808 TO 9,223,372,036,854,775,807
INT: -2,147,483,648 TO 2,147,483,647
SMALLINT: -32,768 TO 32,767
TININT: 0 TO 255
These are for storing non-decimal values. You need to use DECIMAL or NUMERIC to store values shuch as 112.455. When maximum precision is used, valid values are from - 10^38 +1 through 10^38 - 1.
OE-9 isn't NUMERICor INTEGER value. It's a VARCHAR unless you are meaning something else like scientific notation.
https://msdn.microsoft.com/en-us/library/ms187746.aspx
https://msdn.microsoft.com/en-us/library/ms187745.aspx
No, the value would be stored as an integer. Any decimal amounts will be dropped off and only values to the left of the decimal will be kept (no rounding).
More specifically, bigint stores whole numbers between -2^63 and 2^63-1.

SQL Server Convert scientific notation (exponential notation) string to numeric

I would like to understand the following T-SQL statement:
DECLARE #s NVARCHAR(16) = N'1.377532E-39';
SELECT #s AS [orig]
, CONVERT(REAL, #s) AS [real]
, CONVERT(FLOAT, #s) AS [float]
-- , CONVERT(NUMERIC(2, 0), #s) AS [numeric direct]
/* does not work: Msg 8114, Level 16, State 5 */
, CONVERT(NUMERIC(2, 0), CONVERT(FLOAT, #s)) AS [numeric via float];
My desired output is a value with NUMERIC(2,0) precision (in this case 0). Direct conversion does not work; I have to convert from NVARCHAR to FLOAT first and then from FLOAT to NUMERIC(2,0).
Direct conversion works if the string does not contain a scientific notation number. I would like to understand why? Thanks for any hint.
From what I can tell, it is simply a matter of the Float datatype being able to convert a string representing scientific notation to a number, whereas the Numeric datatype cannot. Once Float has converted a number out of scientific notation into a number (as opposed to a string that represents a number), then Numeric can handle the conversion.
In the documentation for the Float and Numeric datatypes, there is reference in both to the use of scientific notation, but this is to define the minimum and maximum values accepted by the datatype. In the Float documentation, it is stated that:
Conversion of float values that use scientific notation to decimal or
numeric is restricted to values of precision 17 digits only. Any value
with precision higher than 17 rounds to zero.
This indicates that Float can interpret scientific notation whereas Decimal and Numeric datatypes can only handle a number with a maximum precision of 17 resulting from a number specified using scientific notation.
I hope that makes sense. Leave a comment if you have any questions.

Convert hexadecimal value into bigint

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 )

Resources