Not able to convert directly to decimal value? - sql

I have the following string and want to convert it to DECIMAL(38,0):
a321
The following code is OK:
SELECT CAST(CONVERT(binary(2), 'a321', 2) AS BIGINT); -- 41761
but this one fails:
SELECT CAST(CONVERT(binary(2), 'a321', 2) AS DECIMAL(38,0));
Msg 8114, Level 16, State 5, Line 7 Error converting data type
varbinary to numeric.
It is not a big deal to do two casts like this:
SELECT CAST(CAST(CONVERT(binary(2), 'a321', 2) AS BIGINT) AS DECIMAL(38,0));
but I want to know why is not working. Can anyone explain?

Convert to a bigint first and then convert to a decimal:
SELECT CONVERT(DECIMAL(38, 0), CONVERT(BIGINT, CONVERT(binary(2), 'a321', 2)))
The binary representation of decimals is quite different from integers, and not all binary representations can be converted to a decimal.

This may be because the number you are trying to convert is an integer, not a decimal.
SELECT CAST(41761 AS VARBINARY), CAST(41761.0 as VARBINARY)
The above give different results as I suspect that the later carries information about the decimal component. Likewise, when converting the decimal value to binary, it works as expected.
SELECT CAST(41761.0 as VARBINARY) -- gives 0x060100014A5F0600 on Azure SQL
SELECT CAST(0x060100014A5F0600 as DECIMAL) -- gives 41761 on Azure SQL
I have tagged the server in as MSDN states the results may very between versions.
Do not construct binary values, and then convert them to a data type of the numeric data type category. SQL Server does not guarantee that the result of a decimal or numeric data type conversion, to binary, will be the same between versions of SQL Server.

Related

SQL convert nvarchar into decimal dynamic rows

I am not so familiar with using convert in SQL so thats why i am stuck with the following situation:
The column has a nvarchar(max) and i would like to convert it into decimal (18,2). But there are some rows that consist a "full" amount (see red box). For all values with the full amount i would like to have it as 1222,00
When creating a SQL view I got this error:
Error converting data type nvarchar to numeric.
How can i still convert this column into decimal?
Many thanks
From your pictures it seems to me like you are using SQL-Server. If so your problem is having , as decimal point instead of . It has nothing to do with having whole and decimal numbers in your data.
So you should replace it before converting data.
SELECT CONVERT(decimal(18,2), REPLACE(ColumnName,',','.')) FROM TableName
DB<>fiddle

What to cast column to in order to have csv export not show scientific notation?

Currently, when I export the results of my query (to .csv) the SKU column gets converted to scientific notation. Is there anything I can cast my SKU column to in order to have it come out as the full string? Some SKUs are all numbers, some are all letters, and some are numbers and letters. A lot of the casts I've tried result in this type of error: Cannot cast '190198047908' to INT.
Often numeric works where int does not:
select cast(col as numeric(38, 0))
I don't know if there are decimal points, but the conversion to int suggests that you do not care about any digits to the right of the decimal point.

Nicer way to convert string to int

Is there a nicer way to convert NVARCHAR to INT on SQL Server?
SELECT CONVERT(INT, CONVERT(FLOAT, N'5.0'))
Why do simple CONVERT or CAST cause errors?
SELECT CONVERT(INT, N'5.0')
SELECT CAST(N'5.0 AS INT)
Why do simple CONVERT or CAST cause errors?
Because the value you are trying to convert has a decimal point. And integers don't have decimal points.
You can get around this using DECIMAL as well as floating point numbers:
SELECT CONVERT(decimal(10, 0), N'5.0')
DECIMAL accepts the decimal places (even more than 1) and rounds the value to the appropriate integer, so the result can be either larger or smaller than the input.
This has the same range as integer, so it is pretty equivalent -- and you can convert back:
SELECT CONVERT(int, CONVERT(decimal(10, 0), N'5.0'))
Note that these are still subject to overflow errors, so I recommend TRY_CONVERT().
You can combine try_convert and isnull functions together. try_convert will return null when it fails to convert. And isnull will use the fallback value if the first argument is null. But a good part of it it will use first arguments datatype so end result will be an int if use the below code :
declare #text nvarchar(20) = N'5.6'
select isnull(try_convert(int,#text),convert(float,#text))
Fiddle

SQL - Convert number to decimal

I'm trying to convert a number to a decimal with two decimals places.
SELECT CONVERT(DECIMAL(10,2),12345)
The above would return 12345.00 but I'm trying to achieve 123.45
You need something like that:
SELECT CONVERT(DECIMAL(15,2),12345/100.0)
SELECT CONVERT(DECIMAL(10,2),CAST(12345 as float)/CAST(100 as float))
Correction: The premise is somewhat flawed, as the data type of a literal number without a decimal point is int, not numeric as implied by the question. In that case, you do need to convert the initial value to either numeric or decimal before dividing:
SELECT CONVERT(DECIMAL,12345)/100
or
SELECT CAST(12345 AS DECIMAL)/100
(cast is the SQL standard, so if you ever want to apply this to other databases, it would be the preferred method.)
Alternately, you can just add a decimal point to the divisor, as SQL server will return the more precise data type when doing arithmetic on heterogeneous types:
SELECT 12345/100.0
According to the documentation, the numeric data type is functionally equivalent to the decimal datatype, so there's really no reason to convert between the two. It seems that all you really want to do is divide the value you have by 100:
SELECT 12345/100

Convert INT to VARCHAR SQL

I am using Sybase and I am doing a select which returns me a column called "iftype", but its type is int and I need to convert into varchar. When I try to do the select without the convert function I get this error:
Error code 257, SQL state 37000: Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed. Use the CONVERT function to run this query.
I dont know how to implement the function CONVERT. Can anyone help me, please ?
Use the convert function.
SELECT CONVERT(varchar(10), field_name) FROM table_name
Use the STR function:
SELECT STR(field_name) FROM table_name
Arguments
float_expression
Is an expression of approximate numeric (float) data type with a decimal point.
length
Is the total length. This includes decimal point, sign, digits, and spaces. The default is 10.
decimal
Is the number of places to the right of the decimal point. decimal must be less than or equal to 16. If decimal is more than 16 then the result is truncated to sixteen places to the right of the decimal point.
source: https://msdn.microsoft.com/en-us/library/ms189527.aspx
You can use CAST function:
SELECT CAST(your_column_name AS varchar(10)) FROM your_table_name
Actually you don't need to use STR Or Convert. Just select 'xxx'+LTRIM(ColumnName) does the job.
Possibly, LTRIM uses Convert or STR under the hood.
LTRIM also removes need for providing length. It seems to be working for integer or float without worry of truncation.
SELECT LTRIM(ColumnName) FROM TableName
also, LTRIM is better than STR as
SELECT STR(1234567890.123)
gives 1234567890
whereas
SELECT LTRIM(1234567890.123)
gives 1234567890.123
SELECT Cast(Cast([field_name] AS BIGINT) AS NVARCHAR(255))
FROM table_name
CONVERT(DATA_TYPE , Your_Column) is the syntax for CONVERT method in SQL. From this convert function we can convert the data of the Column which is on the right side of the comma (,) to the data type in the left side of the comma (,) Please see below example.
SELECT CONVERT (VARCHAR(10), ColumnName) FROM TableName