Why is ISNUMERIC(',') true? [duplicate] - sql

This question already has answers here:
CAST and IsNumeric
(11 answers)
Closed 8 years ago.
Why does the following Query in SQL return true??
I expected it to be false as it cannot be converted into an int or numeric value
select ISNUMERIC(',')
return 1???
select ISNUMERIC('0,1,2')
select ISNUMERIC(',,,')
also returns 1
What could I do for strict numeric checking in SQL?

Because ISNUMERIC answers a question that nobody has ever wanted to ask:
Can this given string be converted into any of SQL Server's numeric data types? And I don't care which of those types it can or cannot be converted into.
This is why TRY_CONVERT was finally introduced into 2012 - to answer a question about a specific data type that you may care about.
For earlier versions, the best you can usually do is to use LIKE to identify the string patterns you do want to attempt to convert.
E.g. if you just want to detect digits, use Value NOT LIKE '%[^0-9]%', which asks for Value strings that do not contain any character which is not a digit.

IsNumeric returns true for "," and "."
ISNUMERIC returns 1 if the string can be converted to any one of ints, numeric/decimal, float, or money. In this particular case, converting ',.' to money succeeds and returns 0.0000, therefore ISNUMERIC returns 1.
Enhancement to ISNUMERIC:
We have now added a new scalar function called TRY_CONVERT that will allow you to convert from a string to type using optional style. If the conversion fails then it will return NULL. The signature of the function is:
TRY_CONVERT(data_type[(length)], expression [,style])

http://msdn.microsoft.com/en-us/library/ms186272.aspx
Microsoft: "It's not a bug, it's a feature "
Certain currency and mathematic symbols return 1 on ISNUMERIC.

As per the manual page:
ISNUMERIC returns 1 for some characters that are not numbers, such as plus (+), minus (-), and valid currency symbols such as the dollar sign ($). Also, it returns 1 for a range of datatypes like:
int
bigint
smallint
tinyint
decimal
So while bigint accepts money values like 230,000, a comma(,) also falls in the set of character which is considered to be a part of a numeric datatype similar to a dot(.) which is a character but is a part of the decimal datatype.

Related

SQL double colon meaning

// A - calculate the sum of payments for one day 2021-01-02
SELECT SUM(PAYMENT) AS TOT_PAYMENTS
FROM PAYMENTS
WHERE (PAY_DATE::timestamp)::date = '2021-01-02'
The datatype of PAY_DATE is TIMESTAMP, so i found a way in another question to convert it to DATETIME while filtering and it seems to be working. My question is where can i find the documentation for this type of method? I could only find references to CAST and PARSE methods, can somebody explain this syntax to me?
Hi 👋🏻 Hope you are doing well!
I know that Amazon Redshift has such syntaxes for the data types converting, e.g. you can find it in the documentation (see the first section CAST):
https://docs.aws.amazon.com/redshift/latest/dg/r_CAST_function.html
expression :: type
expression - an expression that evaluates to one or more values, such as a column name or a literal. Converting null values returns nulls. The expression cannot contain blank or empty strings.
type - one of the supported data types.
return type - the data type specified by the type argument.

How to turn numeric value into currency in BigQuery?

I am new to BigQuery and am trying to convert numeric values (from Salesforce) to currency (preferably dollar value).
Very basically, what I have currently is:
SELECT salesforce.Name,
ROUND(salesforce.Amount,2) as Amount
FROM table.salesforce
Which obviously only rounds the value to two decimal places.
Regarding your question about how to convert a numeric value to currency value in BigQuery, I would advise you to use the FORMAT() and CONCAT() built-in functions.
I see that in your question you mentioned you want to round the numeric values to the second decimal place, you can do that using FORMAT(), you can read more about it here. In addition, to use the "$" sign, you can use CONCAT(). Below is an example where I used some dummy data to exemplify what I explained:
WITH
data AS (
SELECT
20.21 AS num
UNION ALL
SELECT
99999999.12 AS num
UNION ALL
SELECT
12345 AS num )
SELECT
CONCAT('$ ',FORMAT("%'.2f", num)) AS new_num
FROM
data
And the output:
Notice that in the FORMAT() function I used "%'.2f", which rounds the number to the second decimal place. You can find more information about the meaning of each letter/number in the expression using the following guide.
As a bonus information, the currency values are formatted in a way that the dot "." is a decimal separator and the comma "," is a grouping separator. You can switch that using regex expressions with REGEX_REPLACE() and REPLACE() functions. If that is the case, just let me know so I can help.
This is the method that I use:
CAST(YourNumber AS STRING FORMAT '$999,999')
With decimal points:
CAST(YourNumber AS STRING FORMAT '$999,999.00')

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

to_number function in sql

i could not understand why following code
SQL>
Select to_number('1234.64', '9999.9') from Dual;
returns this number 1234.6?is it something like rounding ,truncation or?please help me to understand this code,i know to_number functions,i have used many times this code for simple chars,but here it is not clear anything
This looks a lot like Oracle, but I suspect that the result would be similar in any SQL that used to_number.
The to_number function takes two arguments: the string to be converted to a number, and the format string for the conversion.
In the example, '12345.64' is the string to be converted, while '9999.9' is the format string. In this format string, a 9 stands for a digit while a . stands for the decimal point.
So the function is asking to convert the string '12345.64' to a number with up to 4 digits to the right of the decimal point, and only 1 digit after the decimal point.
The second argument is optional - under normal circumstances, I would omit it.
You should use
SELECT to_number('1234.64', '9999.99') from Dual;
Your mask tells engine you want just one decimal, so number gets rounded.
If you want to get exact number, don't specify any mask:
SELECT to_number('1234.64') from Dual;

Select all rows where a varchar column converts to a decimal

I have a varchar column that has generally has a decimal value, but some times there is some garbage text characters in that field.
Is it possible to filter in the WHERE clause for rows that sucessfully convert to a decimal value?
I am using sql-server 2005
One way is the ISNUMERIC function:
select * from YourTable where ISNUMERIC(col1) = 1
There's one gotcha: isnumeric returns 1 whenever a string can be converted to any numeric type, including money. For example, say you have rows using varying decimal separators, like 7.9 and 7,9. Both will convert to money, and isnumeric returns 1 for both of them. But only one converts to decimal, depending on the SQL Server language settings.