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
Related
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
I'm trying to figure out why I'm getting the following error. Why is it trying to convert to an int when I'm trying to tell it to convert to a decimal?
Conversion failed when converting the varchar value '938.00' to data type int.
Here is the business end. If I filter out the period, it does work but I need that period for when I import the data into Excel.
convert(decimal(9,2),'"+$WorkSheet.Range('D13').Text.replace("'","").replace(" ","").replace("$","").replace(",","").replace("-","")+"'+0)
Presumably because you are using + 0, which means that + is treated as addition rather than concatenation. I think simple solution would be replace:
replace('convert(decimal(9,2), #val)',
'#val',
$WorkSheet.Range('D13').Text.replace("'", "").replace(" ", "").replace("$", "").replace(",", "").replace("-", "")
)
This is what I came up with. It is gnarly looking, but seems to work. Testing will continue. Thanks again.
convert(decimal(9,2), ISNULL(NULLIF('"+$WorkSheet.Range('D14').Text.replace("'","").replace(" ","").replace("$","").replace(",","").replace("-","")+"', ''),'0')),
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))
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.
I found something like this in a SqlServer DB stored proc:
SELECT stuff
FROM mytable
WHERE mytable.column = + #parameter
It seems to run without error, so I assume it's okay. What would the "+" be doing?
(Not surprisingly, this is a difficult topic to effectively search on, so I apologize in advance if this is a duplicate.)
+ is the opposite of - which changes sign.
It does nothing.
Unary + doesn't do anything in T-SQL.
Perhaps the person who wrote it saw 0+#parameter and deleted the 0 to "optimize it". 0+#parameter generates an error if #parameter isn't numeric, whereas unary + doesn't do anything.
What type is #parameter? This probably just a unary plus, (as opposed to minus). For numeric types this has no effect.
Returns the data type of
numeric_expression, except that an
unsigned tinyint expression is
promoted to a smallint result.
Positive unary operator
It looks like it is a short hand notation for converting the parameter into a numeric type. Depending on your database server, it might not even be needed.