I have data stored in a nvarchar(10) column that I need to convert to decimal(10,3). Each record is a full 10 characters with leading zeros. The last three numbers represent decimals. How do I convert the following example?
col1
----------
0000100001
0002507630
0090078607
0258736000
Expected Output
col1 col2
-------------------------------
0000100001 100.001
0002507630 2507.630
0090078607 90078.607
0258736000 258736.000
I tried using
Cast(covert(decimal(10, 3), col1) as Decimal(10, 3)) as col2
The output I receive is
Arithmetic overflow error converting nvarchar to data type numeric.
How do do properly convert this data?
DECLARE #t AS TABLE (a NVARCHAR(10));
INSERT INTO #t VALUES ('0000100001'),('0002507630'),('0090078607'),('0258736000');
SELECT a,CAST(CAST(a AS INT)/1000.000 AS DECIMAL(10,3))
FROM #t;
Try this:
SELECT CAST (LEFT(col1, 7) + '.' + RIGHT(col1,3) AS DECIMAL(10,3))
SQL Fiddle demo
Related
I am inserting table A to table B. The problematic column looks like -$25.2. I first replaced the $ and tried insert. Got this error
Error converting data type nvarchar to float.
I then checked by
SELECT *
FROM B
WHERE ISNUMERIC([Col Name]) <> 1
and no results were returned.
This is odd. It is supposed to return something.
What should I check next?
I also tried something like
CAST(REPLACE([Col Name], '-$', '') AS FLOAT)
Try using this
DECLARE #Text nvarchar(100)
SET #Text = '-$1234.567'
SET #Text = Replace(#Text,'$', '')
Select CONVERT(float, #Text) AS ColumnValue
Select ABS(CONVERT(float, #Text)) AS ColumnValue
While the 'money' data type isn't great for doing calculations, in this scenario you can use it as an intermediary.
declare #a nvarchar(10)
set #a = '-$25.2'
select
#a,
cast(cast(#a as money) as float)
Only use this though if your data only goes to a max of 4 decimal places, otherwise you will lose precision in the conversion.
I have the following table called as Hexa_Table which consists of only one column namely val.
Table: Hexa_Table
CREATE TABLE Hexa_Table
(
val VARCHAR(50)
);
Insertion:
INSERT INTO Hexa_Table VALUES('123456789101213');
INSERT INTO Hexa_Table VALUES('414F2D53594F545641');
INSERT INTO Hexa_Table VALUES('1234F6789A1213G');
INSERT INTO Hexa_Table VALUES('414F2D363530303035');
INSERT INTO Hexa_Table VALUES('12345678910');
Note: Now I want to update only those values which are Hexadecimal and want to update it to String, for which I need to identify which are the Hexadecimal values in the table.
For example I have record number 2 that is 414F2D53594F545641 if you convert it will get AO-SYOTVA. And in 4th record I have 414F2D363530303035 if you convert it will get AO-650005.
Converted by using : This
Questions:
1. How to identify hexadecimal values in the table?
2. How to update hexadecimal values to string?
You can use CONVERT with style 1. Something like this in your scenario.
You can use Filter NOT LIKE '%[^0-9a-f]%' to match exact hex pattern and LEN(val) %2 = 0 to check if it has exact number of required bytes for convert
SELECT val,
CAST(CONVERT(varbinary(4), '0x' + val, 1) As VARCHAR(100)) as charstring,
CONVERT(varbinary(4), '0x' + val, 1) as HexVal
FROM Hexa_Table
WHERE val NOT LIKE '%[^0-9a-f]%'
AND LEN(val) %2 = 0;
Output:
val charstring HexVal
414F2D53594F545641 AO-S 0x414F2D53
414F2D363530303035 AO-6 0x414F2D36
Reference
How can I convert a varchar with hexadecimal value to int?
MSDN Convert
You can find hexadecimal values in your table using this:
like '%' + CHAR(0x00) +'%'
However in your example which you have shown, the values are stored already as string so I am not sure what you mean to convert the values as string.
On a side note:
If you want to know how to convert the hex to varchar then you need to use CONVERT like this":
CONVERT(VARCHAR(MAX), 0x48656c70)
To find out hexadecimal values you can in following:
select val
from Hexa_Table
where val like '%' + CHAR(0x00) +'%'
It is already stored as VARCHAR, what and how do you want to update?
Try this code:
BEGIN TRAN
SELECT * FROM Hexa_Table
WHERE val = #value_insert
IF (##ROWCOUNT = 0)
INSERT Hexa_Table
SELECT CONVERT(VARCHAR(50), #value_insert)
COMMIT
create table hexvalue(Data varchar(10))
insert into hexvalue values('5'),('0E'),('12'),('17'),('15'),('EF'),('EF')
select convert(int, convert(varbinary, '0x'+Data, 1)) from hexvalue
This is throwing Error converting data type varchar to varbinary exception, except for '5'.
How can I fix this?
Hex always have two characters. Try this
create table hexvalue(Data varchar(10))
insert into hexvalue values('05'),('0E'),('12'),('17'),('15'),('EF'),('EF')
select convert(int, convert(varbinary, '0x'+Data, 1)) from hexvalue
I have a situation like this
I got a column with 'money' type, 2 decimal . Example data:(65.00)
I need to add 12 zero / 000000000000 to it so that the output would be like this:
(65.00 convert to 6500) + (000000000000) = 000000006500
Output: 000000006500
How can I achieve this?. Thank you for your help and suggestion
You can do this with a couple of casts, multiplying by 100, and using REPLICATE('0') to pad with the requisite number of zeroes).
I'm assuming you DO want up to 2 x trailing decimals, but no more.
DECLARE #value MONEY;
SET #value = 65.123;
DECLARE #intValue BIGINT;
SET #intValue = CAST(#value * 100.0 AS BIGINT);
SELECT REPLICATE('0',12-LEN(#intValue)) + CAST(#intValue AS NVARCHAR(20));
Returns 000000006512
If you need to do this on a set, a CTE can be used for the intermediate step, e.g.
WITH cte AS
(
SELECT CAST(MoneyField * 100.0 AS BIGINT) AS intValue
FROM SomeTable
)
SELECT
REPLICATE('0',12-LEN(cte.intValue)) + CAST(cte.intValue AS NVARCHAR(20))
FROM cte;
Fiddle here
It is Possible .But output Column should be in the type of varchar(15) .If you want to do further operation of your output you have to convert that into int or whatever
SELECT CONCAT(REPEAT('0',12-LENGTH(65.00)),(65.00*100));
I have a table called testTable with two columns, id that is auto incremented and someValue.
The data contained in the someValue column are: 12, 1.2, .4, 1d4, +, -, .
Data type for someValue is varchar(50).
Why are the following queries throwing
Error converting data type varchar to numeric.
select ID, someValue
from testTable
where ISNUMERIC(someValue + 'd0') = 1 and CAST(someValue as decimal(8,2)) > 0.1;
select tt.ID,tt.someValue
from (select ID, someValue
from testTable
where ISNUMERIC(someValue + 'd0') = 1) as tt
where CAST(tt.someValue as decimal(8,2)) > 0.1;
You have a few problems; CAST does not work on non-decimal input, and ISNUMERIC accepts strings that can convert to monetary amounts, including non-decimal values like '-', '.', or 'US$100'.
The right way to solve this is to add a Decimal column to your database, have whatever populates someValue to populate the Decimal column with the value you want to compare against, and only compare against the Decimal column.
If, for some reason, you cannot do that, you can use ISNUMERIC and only include non-monetary decimal amounts, and use Convert instead of CAST:
select ID, someValue
from testTable
where ID IN
(select ID from testTable where ISNUMERIC(someValue) = 1
AND Patindex('%[^0-9-+.]%', someValue) = 0
AND someValue NOT IN ('-', '+', '.')
)
and Convert(decimal, someValue) > 0.1
In your first statement you take directly from testTable and attempt:
CAST(someValue as decimal(8,2))
This will throw an error as you have not filtered out non-numerical values.