how to convert this String to Decimal - sql

i have this String '5666,232343' and i want to convert it to Decimal, i use cast('5666,232343' as decimal(7,5)) but it returns NULL value.
Do you know why it doesn't work with CAST

Zorkolot is right. The current precision and scale that you've used is not sufficient for the value you've provided.
If you're using SQL Server 2012 or higher and you want to keep the comma in the value, then you can use the TRY_PARSE function and set a culture. It will return NULL if it encounters an error instead of not completing the statement and returning red text. This also allows you to add basic error handling to the statement, if you wanted, by getting failed conversions to return the value of zero. For example:
This is your original query (which is currently erroring) with my error handling fix:
select coalesce(try_parse('5666,232343' as decimal(7,5) using 'en-GB'),'0') as [DecimalValue]
This is the same thing as above but I've amended the decimal precision and scale so that the value is successfully converted:
select coalesce(try_parse('5666,232343' as decimal(16,6) using 'en-GB'),'0') as [DecimalValue]
This should prevent you having to perform a REPLACE either manually or by using the SQL function.

You need to cast to a decimal that can hold the value of 5666.232343.
DECIMAL(7,5) allows numbers in this format: ##.#####. The biggest number you can have then is 99.99999. You also need to take the comma out and replace it with a period:
SELECT CAST('5666.232343' as decimal(16,6)) AS [DecimalValue]

The problem is probably the comma. In some databases, some of the functions are not as internationally-sensitive as (I think) they should be. So try:
cast(replace('5666,232343', ',', '.') as decimal(7, 5))

Related

How can I set a minimal amount of numbers after the decimal dot (0.9=>0.90)

I'm using google bigquery, and a column has values I want to round. If I do, and the rounded value ends in a zero, the zero is not displayed.
I've tried the function FORMAT, which apparently has some .number function, but I have no idea how to use it. Whenever I include any 2 things separated by a comma inside its brackets, it complains that it only takes 1 value.
You would use FORMAT() with the precision specifier. For four decimal places always -- including zeros:
select format('%.4f', 1.23)
If the BQ documentation does not answer your questions, I find that that the function seems to be inspired by the classic C printf()/sprintf() functions.
Unaware if in BigQuery (haven't used it ever) there is a better way I guess this will fix your problem since I just tried it in their console.
Cast your float to a string and then check if your last digit is a 0. In case it's not add it:
SELECT case when RIGHT(cast(0.9 as string), 1) <> '0' then cast(0.9 as string)+'0' else cast(0.9 as string) end as FormattedNumber

how to round the decimal value in sql server

I want to round the value from 1572.750000 to 1572.75
SELECT ROUND(1572.750000, 2)
I tried above format, but it was not working.
Please any one help me out.
you Can use this
SELECT cast(1572.750000 as decimal(15,2))
I think the simplest way is to convert to decimal:
select cast(1572.750000 as decimal(10, 2))
If you want a string output, you can use the str() function (documented here).
The confusion occurs because round() changes the value, but it does not change the type. Hence, whatever application is using the value can format it as it wants. When the application knows that the value has only two decimal places (with decimal(10, 2)) or the code converts directly to a string, then this is not an issue.

Specify scale in a ABS function?

Is it possible to specify scale in a ABS function from a decimal ?
= ABS(([Accounting].salary+ABS([Accounting].expenses))/2)
As of right now everything works correctly but there is a problem with results when the number has only 1 number after the decimal point:
100.12 - All ok
230.1 - Not ok, it should be 230.10
I want the outcome to always have two numbers after the decimal point.
The query has to work on both MS SQL Server and Oracle. I can specify the query for each db manually.
To me, this is more a user interface issue than a SQL issue.
230.1 - Not ok, it should be 230.10
Seriously, that's both the same number.
In Oracle you can convert a number into a string in order to present it to an end user.
select to_char(234.1,'$9,999.99') from dual
$234.10
Have a look at format models in the Oracle documentation.
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm
Just change code:
= ABS(([Accounting].salary+ABS([Accounting].expenses))/2)
By the following:
= CAST(ABS(([Accounting].salary+ABS([Accounting].expenses))/2) AS DECIMAL(18 /*set your format here instead of 18*/,2))
I've just checked it in code:
SELECT CAST(230.1 AS DECIMAL(18 /*set your format here instead of 18*/, 2))
And all works fine
In fact, it's just a question of representation of your numbers in the UI for the user. You can convert your number to the string with using to_char () with the format specified. But it is better to define the output format on the front-end

Why does ISNUMERIC('.') return 1?

Recently I was working with ISNUMERIC in SQL Server, when I encountered a problem, which led to finding this snippet of code.
SELECT ISNUMERIC('.')
This returns 1, as in true, shouldn't this return 0 as in false?
See IsNumeric() Broken? Only up to a point.
SELECT CAST('.' AS MONEY)
returns 0.00 (though the cast fails for int and float)
ISNUMERIC just checks that the value can be cast to any one of the numeric datatypes which is generally useless. Usually you want to know whether it can be cast to a specific type.
Additionally it doesn't even seem to do that task correctly for all possible inputs.. ISNUMERIC(' ') returns 0 despite casting successfully to both int and money. Conversely ISNUMERIC(N'8') returns 1 but does not cast successfully to anything that I tried.
Some useful helper functions for that are here IsNumeric, IsInt, IsNumber.
SQL Server 2012 introduced TRY_PARSE and TRY_CONVERT that help with this greatly.
Because "." is used in a decimal number !
see here
isnumeric for '-' & '.' Why isnumeric('-') & isnumeric('.') returning
1?
Answer: Because "-" means negative and "." is used in a decimal
number. I have no clue why they named it ISNUMERIC though. They
should have named it, ISNUMBERRELATED.
I think it also interprets a number of other non-numeric fields as numeric, there's further info here -
http://classicasp.aspfaq.com/general/what-is-wrong-with-isnumeric.html

ISNUMERIC('07213E71') = True?

SQL is detecting that the following string ISNUMERIC:
'07213E71'
I believe this is because the 'E' is being classed as a mathmatical symbol.
However, I need to ensure that only values which are whole integers are returned as True.
How can I do this?
07213E71 is a floating number 7213 with 71 zeros
You can use this ISNUMERIC(myValue + '.0e0') to test for whole integers. Slightly cryptic but works.
Another test is the double negative myValue NOT LIKE '%[^0-9]%' which allows only digits 0 to 9.
ISNUMERIC has other issues in that these all return 1: +, -,
To nitpick: This is a whole integer. It is equivalent to 7213 * 10 ^ 71.
In the documentation it says
ISNUMERIC returns 1 when the input expression evaluates to a valid integer, floating point number, money or decimal type; otherwise it returns 0. A return value of 1 guarantees that expression can be converted to one of these numeric types.
Your number is also float (with exponential notation), therefore the only way to have ISINTEGER is to define it yourself on SQL. Read the following link.
http://classicasp.aspfaq.com/general/what-is-wrong-with-isnumeric.html
Extras:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=59049
http://www.tek-tips.com/faqs.cfm?fid=6423
I have encountered the same problem. IsNumeric accepts "$, €, +, -, etc" as valid inputs and Convert function throws errors because of this.
Using "LIKE" SQL statement fixed my problem. I hope it'll help the others
SELECT UnitCode, UnitGUID, Convert(int, UnitCode) AS IntUnitCode
FROM [NG_Data].[NG].[T_GLB_Unit]
WHERE ISNULL(UnitType,'') <>'Department'
AND UnitCode NOT LIKE '%[^0-9]%'
ORDER BY IntUnitCode
PS: don't blame me for using "UnitCode" as nvarchar :) It is an old project :)
You have to ensure it out of the call to the database, whatever the language you work with, and then pass the value to the query. Probably the SQL is understanding that value as a string.