Alright I am not experienced in SQL Server but anybody here knows how can I transform for example this set of four INT values into this value in HEX?
I have for example:
3533
And I need it converted to:
0x03050303
So for example if I have:
3533 then I get 0x03050303
3459 then I get 0x03040509
Thanks in advance!
Since this is not normal hex conversion, normal tricks won't apply.
The hexadecimal value for the decimal value 3533 is 0xDCD and that's not what you want.
Instead you want each digit of the decimal value separated out into its own byte, so basically you want this:
DEC = abcd
"HEX" = 0a0b0c0d
You can do this with this simple calculation:
DECLARE #VALUE INT = 1234
SELECT CONVERT(VARBINARY(8),
(#VALUE % 10) +
(#VALUE / 10 % 10) * 16*16 +
(#VALUE / 100 % 10) * 16*16*16*16 +
(#VALUE / 1000 % 10) * 16*16*16*16*16*16
) AS X
Output:
01020304
Note that the integer value can be found without the conversion, the conversion above is just to get it formatted like a hex value when testing. "Hex" is just a visual representation, the underlying number is the same.
In other words, the numeric value that corresponds to 0x0a0b0c0d can be found by just:
DECLARE #VALUE INT = 1234
SELECT
((#VALUE % 10) +
(#VALUE / 10 % 10) * 16*16 +
(#VALUE / 100 % 10) * 16*16*16*16 +
(#VALUE / 1000 % 10) * 16*16*16*16*16*16) AS X
The hex output is just a formatted representation.
Use this:
declare #i as int,#str as varchar(20),#counter as int;
set #counter = 1;
set #i = 3459 ;
set #str = '0x'
while (#counter <= len(#i))
begin
set #str = #str + '0' + substring(convert(varchar(20),#i),#counter,1)
set #counter = #counter +1
end
print #str
Related
Convert number from 002541500(Last 4 digits are decimal places) to 254.1500 in sql server
This is simple maths:
CONVERT(int,Yourcolumn) / 10000.00
If you have to have 4 decimal places, use CONVERT:
CONVERT(decimal(10,4), CONVERT(int,YourColumn) / 10000.00)
declare #i varchar(50) = '00251500'
--Option 1:
select convert(decimal(9,4), CONVERT(int, #i) / 10000.00)
By multiplying the varchar with 1, the basetype gets converted to numeric. By multiplying this result with .0001 the scale changes from 0 to 4.
SELECT 1 * '902541501' * .0001
Result
90254.1501
SQL_VARIANT_PROPERTY:
SELECT
SQL_VARIANT_PROPERTY(1 * '902541501' * .0001, 'BaseType') BaseType,
SQL_VARIANT_PROPERTY(1 * '902541501' * .0001, 'Scale') Scale
Result
BaseType Scale
numeric 4
I found the way to fix the issue
declare #imp_ori varchar(50) = '00251500'
select
cast(left(#imp_ori,4) + '.' + right(#imp_ori,4) as numeric(10,4)) as StringBuildCast
result: 25.1500
I have a number, e.g., 11.61. I then make the following division: 1 / 11.61.
DECLARE #value1 numeric(28, 20)
,#value2 numeric(28, 20)
SET #value1 = 11.61
SET #value2 = 1 / #value1
The results look like
(No column name) (No column name)
11.61000000000000000000 0.08613264427217915000
I then want to reverse the division: 1 / #value2
SET #value1 = 1 / #value2
The result looks like
(No column name) (No column name)
11.61000000000000079000 0.08613264427217915000
The original and expected value should be 11.61. However, as you can see the result is 11.61000000000000079000. How can this error be avoided?
use CAST
like CAST(#value1 as decimal(10,2)) and CAST(#value2 as decimal(10,2))
it will show only 2 decimal places.
How can I count the number of digits after decimal point in SQL Server 2008?
Eg: 1.99999999495049E-05
I need to know how many digits there are after the decimal point.
If you want to count the number of digits after decimal then the below query works fine.
SELECT LEN(SUBSTRING(cast(COLUMNNAME as varchar), CHARINDEX('.',COLUMNNAME ) + 1, 1000)) AS DIGITSAFTERDECIMALPOINTS
FROM TABLENAME
Hope this helps!
Wow, that's superbly tricky. This sort of logic should get you there:
DECLARE #Value float(53) = 1.99999999495049E-5;
DECLARE #StringValue varchar(40) = CONVERT(varchar(40), #Value, 2);
DECLARE #ELocation int = CHARINDEX('e', #StringValue);
DECLARE #ExponentDigits int = 0 - CAST(SUBSTRING(#StringValue, #Elocation + 1, 40) AS int);
DECLARE #MantissaString varchar(40) = REPLACE(SUBSTRING(#StringValue, 1, #ELocation - 1), '.', '');
DECLARE #NumberOfMantissaDigits int = LEN(CAST(CAST(REVERSE(#MantissaString) AS bigint) AS varchar(20)));
SELECT #ExponentDigits + #NumberOfMantissaDigits - 1 AS NumberOfDecimalPlaces;
I've left them all as separate statements so you can work out what's going on. I could put it all in a single line expression but I'd shudder to try and understand it later.
If you would like to count the number of digits after a decimal point in Teradata
SELECT LENGTH(SUBSTRING(COLUMN_NAME FROM POSITION('.' IN COLUMN_NAME) + 1 FOR 1000))
FROM TABLE
why SET #H1 = CAST((#rCount / #poolTot) AS VARCHAR(50)) + '%' keeps returning 0%?
#H1 is a VARCHAR(50) while #rCount and #poolTot are integers with values of 8 and 10. I should get something like .80%
Thanks for helping
Either your dividend or divisor (or both) need to be a datatype with decimal places (decimal, float, etc). One way of doing this is to muliply one of the numbers by 1.0, to force the conversion. Or you can do an explicit conversion.
Your results may vary, depending on the data type you choose. Using the first method below, you will get "0.80000000000000%". If you cast it cast #poolTot as a float, like the second example, you will get "0.8%". It really depends on what you want your results to look like.
DECLARE #H1 VARCHAR(50)
,#rCount INT = 8
,#poolTot INT = 10
SET #H1 = CAST(((#rCount)/ (#poolTot * 1.0)) AS VARCHAR(50)) + '%'
SELECT #H1
_
DECLARE #H1 VARCHAR(50)
,#rCount INT = 8
,#poolTot INT = 10
SET #H1 = CAST(((#rCount)/ cast(#poolTot as float)) AS VARCHAR(50)) + '%'
SELECT #H1
If you want to convert the result in decimal you can do something like
Declare #rCount as int=8;
declare #poolTot as int =10
select CAST((CONVERT(DECIMAL(10,2), #rCount)) /
(CONVERT(DECIMAL(10,2), #poolTot))as varchar(50)) + '%'
Output will be like
Again if you want only two digit after decimal you can do something like
Declare #rCount as int=8;
declare #poolTot as int =10
declare #Temp as decimal(10,2)=(CONVERT(DECIMAL(10,2), #rCount)) /
(CONVERT(DECIMAL(10,2), #poolTot))
select convert(varchar(10),#Temp,2) + '%'
Than it will display like
I think you can use this:
SET #H1 = CAST((100 * #rCount / #poolTot) AS VARCHAR(50)) + '%'
I will be creating a sequential Serial Number made from Hexadecimal values
With this Format:
XX-XX-XX-YYYY
Which XX-XX-XX is default value
And YYYY is the incrementing hexa decimal value
Now to create the serial number based on hex value I need Add 6 to the last generated hex value
MIN: 2D41 + 6 = 2D47
2D47 + 6 ... and so on
MAX: 4100 generation of serial will stop when I meet the MAX value.
I already created it in c# but I need to do it on SQL
int num1 = int.Parse("2D41", NumberStyles.HexNumber); //Convert hex to int
int result = num1 + 6; //Add + 6 for increment
string myHex = result.ToString("X"); //Convert result to hex
MessageBox.Show(myHex); // result 2D47
How can this be done in T-SQL?
DECLARE #x VARBINARY(8) = 0x00002D41;
SELECT CONVERT(VARBINARY(8), CONVERT(INT, #x) + 6);
In order to handle the output as a string:
DECLARE #x VARBINARY(8) = 0x00002D41;
SELECT CONVERT(CHAR(10), CONVERT(VARBINARY(8), CONVERT(INT, #x) + 6), 1);
Hope this helps you
declare #seed varchar(max) = '2D41';
declare #limit varchar(max) = '4100';
select convert(int, convert(varbinary(max), '0x'+#seed,1)),
convert(int, convert(varbinary(max), '0x'+#limit,1));
;with seedlimit(seed, limit) as (
select convert(int, convert(varbinary(max), '0x'+#seed,1)),
convert(int, convert(varbinary(max), '0x'+#limit,1))
)
select SerialNumber = 'XX-XX-XX-' + right(convert(varchar(10),cast(s.seed + 6 * v.number as varbinary(max)),1),4)
from seedlimit s
join master.dbo.spt_values v on type='p'
where s.seed + 6 * v.number <= s.limit;
The basic ingredients are in there for you to create a view/procedure/function out of the answer,
Output:
SerialNumber
-------------
XX-XX-XX-2D41
XX-XX-XX-2D47
...
XX-XX-XX-40F7
XX-XX-XX-40FD
If you already have it in C#, leave it there and simply convert your code to a SQL CLR function.
For a simple example see:
http://blog.sqlauthority.com/2008/10/19/sql-server-introduction-to-clr-simple-example-of-clr-stored-procedure/