decimal to hex conversion in sql server 2008 - sql

the hex value of 2716455883 is A1E9D3CB but using
SELECT CONVERT(VARBINARY(8), 2716455883)
getting answer 0x0A000001CBD3E9A1

SELECT CONVERT(VARBINARY(8), cast(2716455883 as bigint))
It is due to the way SQL Server interprets literals without qualified types. Check this out
select sql_variant_property(2716455883, 'basetype'); -- numeric
select sql_variant_property(2716455883, 'precision'); -- 10
select sql_variant_property(2716455883, 'scale'); -- 0

Related

How to convert to decimal when data is number but not convert when data is text?

I am using SQL Server 2008 r2.
There are 31 fields (D1-D31) as varchar and keep 2 format data as number and text such as 'SD' and '123.456'.
I use the command for Data Base Expert of Crystal Report like this.
select
case
when D1 in('SD') then d1
when D1 is NULL then ''
else convert(decimal(7,2),d1)
end
.
.
.
case D2-D31
from Rec2019 where ID ='P0009'
It's show Error converting data type varchar to numeric.
How to fix it ? please..
A column has one datatype so you cannot place DECIMAL and VARCHAR values inside same column. I think you want to round numeric-ish values. In that case you can use the following:
CASE
WHEN ISNUMERIC(d1) = 1 AND d1 NOT IN ('+', '-', '$') THEN CAST(CAST(d1 AS DECIMAL(7, 2)) AS VARCHAR(9))
WHEN d1 IS NULL THEN ''
ELSE d1
END
Demo on db<>fiddle
For SQL Server 2008, you can check using ISNUMERIC, where as SQL Server 2012+ have TRY_CONVERT or TRY_CAST to make it simple like following.
TRY_CONVERT(decimal(7,2), D1)
Since ISNUMERIC had some small downsides, for instance with scientific notation of numbers, I would use TRY_CAST, like this: (note: SQLServer 2012+)
CREATE TABLE #T (ColA VARCHAR (10))
INSERT INTO #T VALUES ('1'), ('A'), ('2');
SELECT *, TRY_CAST (ColA AS DECIMAL (4,2)) AS Numb
FROM #T
Sidenote: One column has one data-type, you cannot combine datatypes in one column in SQL Server.
To convert when the data is number, and not convert when the data is text, you can check the type with the isnumeric function.
select
case when isnumeric(D1) then convert(decimal(7,2), D1) else null end as D1_asnumeric
from XXX

How to use between condition with numbers for string columns in Oracle

I need to check the between condition for two numbers in the tables but the column in oracle DB has string values .
I tried the following query , nothing helped me
select * from sys.employee_infor where to_number(emp_number) between 1200 and 2400;
select * from sys.employee_infor where (emp_number) >= to_char(1200) and (emp_number) <= to_char(2400);
select * from sys.employee_infor where to_number(emp_number) between '1200' and '2400';
Received error as :
ORA-01722: invalid number
My Emp_number column be like as,
The safer solution is to use CASE:
SELECT *
FROM sys.employee_infor
WHERE CASE WHEN NOT REGEXP_LIKE(emp_number, '\D') THEN TO_NUMBER(emp_number) END BETWEEN 1200 AND 2400
It is likely that you have data in the employee number column that cannot be converted to a number.
Assuming that the employee number is an integer value, the following query will show you the offending rows :
select * from sys.employee_infor where regexp_like(emp_number, '[^0-9]');
You can use that where clause in your query to ignore badly formatted data :
select * from (
select * from sys.employee_infor where not regexp_like(emp_number, '[^0-9]')
) where to_number(emp_number) between 1200 and 2400
PS : as you are looking to compare numbers, not strings.
Another solution is to use the DEFAULT ... ON CONVERSION ERROR option of function TO_NUMBER(), which is available starting Oracle 12c R2. With this option conversion errors are trapped and a default value is returned instead of throwing an error :
select *
from sys.employee_infor
where to_number(emp_number default 0 on conversion error) between 1200 and 2400
Under many circumstances, this will work:
where emp_number >= '1200' and emp_number <= '2400'
or:
where emp_number >= '1200' and emp_number <= '2400' and
length(emp_number) = 4
This is not exactly the same, because it is using string comparisons and not numeric comparisons. On the other hand, it can take advantage of indexes.

Float to varchar with style 3 is failing in sql server 2016

I am getting Arithmetic overflow error while executing following query on sql server 2016.
"select convert(varchar(20), cast('0' as float), 3)"
The same query works fine on sql server 2014.
you are getting this error cause casting '0' to float of style 3 returning 23 charter value which is unable to convert to varchar of length 20 only.
try something like,
select convert(varchar(23), cast('0' as float), 3)
Or
select convert(varchar(max), cast('0' as float), 3)
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
The same query works fine
on sql server 2014.
style in sql 2014
Other values are processed as 0.
A maximum of 6 digits. Use in scientific notation, when appropriate.
style in sql 2016
3 Always 17 digits. Use for lossless conversion. With this style, every distinct float or real value is guaranteed to convert to a
distinct character string.Applies to: Azure SQL Database, and starting in SQL Server 2016.
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql

control in display Decimal separators in sql server

I have problem in display the decimal numbers, it has many number.
My sql statement :
Select sum(HS$totalMoney)
the result :
12132.123444343
I want to display as 12132.12 without the another number
Thanks.
If your logic is for money you should first round the values not truncate
select CONVERT(decimal(18,2),round(12132.123444343 ,2)) gives 12132.12
select CONVERT(decimal(18,2),round(12132.125944343 ,2)) gives 12132.13
Try this one -
SELECT CAST(12132.123444343 AS DECIMAL(10,2))
if you are using mysql, use code blew
SELECT TRUNCATE(sum(HS$totalMoney), 2);
this query slove your problem
SELECT CAST(12132.123444343 AS DECIMAL(10,2))
or you can use
select CONVERT(decimal(18,2),round(12132.1255555 ,2))
SELECT CONVERT(decimal(21, 2), sum(HS$totalMoney))
-- This one will round in SQL Server but truncate in ASE 15 (which was available to me at the time)
SELECT CONVERT(decimal(21, 2), round(sum(HS$totalMoney), 2, 1))
-- This one uses a variant of ROUND supported by SQL Server, but not ASE 15 (and will truncate the third and subsequent decimal places).
The round function has a function parameter to truncate instead of round:
select round(12132.123444343 , 2, 1)
From here:
http://msdn.microsoft.com/en-us/library/ms175003.aspx

SQL IsNumeric Returns True but SQL Reports 'Conversion Failed'

Assuming the following data:
Column1 (data type: varchar(50))
--------
11.6
-1
1,000
10"
Non-Numeric String
I have a query, which is pulling data from this column and would like to determine if the value is a number, then return it as such in my query. So I am doing the following
SELECT CASE
WHEN IsNumeric(Replace(Column1, '"', '')) = 1 THEN Replace(Column1, '"', '')
ELSE 0
END AS NumericValue
SQL is reporting back:
Conversion failed when converting the varchar value '11.6' to data type int.
Why? I have also tried to force cast this:
SELECT CASE
WHEN IsNumeric(Replace(Column1, '"', '')) = 1 THEN cast(Replace(Column1, '"', '') AS float)
ELSE 0
END AS NumericValue
And I got:
Error converting data type varchar to float.
You need to replace comma with a period:
CAST(REPLACE(column, ',', '.') AS FLOAT)
SQL Server outputs decimal separator defined with locale, but does not unterstand anything but a period in CASTs to numeric types.
First convert the string to money, then covert it to any other numeric format since money type gives a true numeric string always. You will never see an error then.
Try the following in your query, and you'll know what I am talking about. Both will return 2345.5656. The Money datatype is rounded to 4 decimal places, and hence the casting causes rounding to 4 decimal places.
SELECT CAST('2,345.56556' as money), CAST('$2,345.56556' as money)
Cast( cast('2,344' as money) as float) will work perfectly or
cast( cast('2,344' as money) as decimal(7,2)) will also work.
Even cast(CAST('$2,345.56556' as money) as int ) will work perfectly rounding it to nearest integer.
There are many issues with SQL isnumeric. For example:
select isnumeric('1e5')
This will return 1 but in many languages if you try to convert it to a number it will fail. A better approach is to create your own user defined function with the parameters you need to check for:
http://www.tek-tips.com/faqs.cfm?fid=6423
ISNUMERIC returns 1 when the input expression evaluates to a valid integer, floating point number, money or decimal type;
So the problem is it is a valid number but not a valid int.
Kyle,
I think this solves the problem. The problem lies in the fact that the ELSE clause initializes your result to be an INTEGER. By making an explicit typecast to FLOAT and adding the suggestion of Quassnoi, it seems to work.
DECLARE #MyTable TABLE (Column1 VARCHAR(50))
INSERT INTO #MyTable VALUES('11.6')
INSERT INTO #MyTable VALUES('-1')
INSERT INTO #MyTable VALUES('1,000')
INSERT INTO #MyTable VALUES('10" ')
INSERT INTO #MyTable VALUES('Non-Numeric String')
SELECT CASE WHEN ISNUMERIC(REPLACE(Column1,'"','')) = 1 THEN REPLACE(REPLACE(Column1,'"',''), ',', '.') ELSE CAST(0 AS FLOAT) END
FROM #MyTable
Regards,
Lieven
IsNumeric(' ') also returns 1, but then CAST as int blows up. Brendan above says write your own function. He is correct.
This solution does not work in all cases (specifically numbers with money and/or thousand separators). Concatenate an exponent representation to the end of the number which is represented by a string...ISNUMERIC() works fine from there. Examples below:
-- CURRENT ISNUMERIC RESULTS
SELECT ISNUMERIC('11.6'); --1
SELECT ISNUMERIC ('-1'); --1
SELECT ISNUMERIC('1,000'); --1
SELECT ISNUMERIC('10"'); --0
SELECT ISNUMERIC('$10'); --1
-- NEW ISNUMERIC RESULTS
SELECT ISNUMERIC('11.6'+'e+00'); --1
SELECT ISNUMERIC ('-1'+'e+00'); --1
SELECT ISNUMERIC('1,000'+'e+00'); --0
SELECT ISNUMERIC('10"'+'e+00'); --0
SELECT ISNUMERIC('$10'+'e+00'); --0
This, at the very least, standardizes the format for using the REPLACE() function.
I have just meet this issue.
You can try this solution if you don't mind about limitation of decimal length.
CONVERT(numeric, CONVERT(money, '.'))
NOTE:
It is supported in SQL Server 2008 or above.
Money range is : -922,337,203,685,477.5808 to 922,337,203,685,477.5807 - four decimals.