I have good experience in SQL Server,
But suddenly I discovered this strange SELECT command
SELECT $
or
SELECT $ FROM tableName
All the time it returns zero scalar value (0.00),
or a new column with all values of 0.00
What is that?
When SQL Server comes across your $ sign, it automatically converts it into a money data type. Because you don't have an explicit value after the dollar sign, SQL Server is assuming 0.00. From MSDN:
When converting to money or smallmoney, integers are assumed to be monetary units. For example, the integer value of 4 is converted to the money equivalent of 4 dollars (for us_english, the default language). Numbers to the right of the decimal in floating-point values are rounded to four decimal places for money values. Expressions of data types char or varchar that are being converted to an integer data type must consist only of digits and an optional plus or minus sign (+ or -). Leading blanks are ignored. Expressions of data types char or varchar converted to money can also include an optional decimal point and leading dollar sign ($).
After a little messing around, I've figured since this happens no matter what currency symbol is used, SQL server is implying that the field is a currency field.
If you add numbers after the currency symbol, in this case a dollar so:
SELECT $4
SQL server will return 4.00
So SQL Server is taking the use of $ and assuming we want to create a field with the MONEY datatype and as we haven't entered a value after the currency symbol, SQL Server assumes the value is 0, though in my opinion this should return NULL.
You can prove out that SQL Server is treating it as a money type:
select $ as n into #x
exec tempdb..sp_help '#x'
sp_help will output (among other things):
Column_name Type Computed Length Prec Scale Nullable TrimTrailingBlanks FixedLenNullInSource Collation
-------------- ------- ----------- --------- ------- -------- ----------- --------------------- ----------------------- ------------
n money no 8 19 4 no (n/a) (n/a) (null)
Related
I have a table which includes:
COUNT RISK
35 0.6456000000
11 0.5234000000
4 0.8431000000
I need a column to multiply the two columns. However I'm getting the result of:
TOTAL
35
11
4
COUNT - INT
RISK - VARCHAR
SQL is clearly rounding up the decimals as 1. I've tried casting as decimal, numeric and multiplying by 1.0. I need to retain the decimals for an actual calculation. Any help would be great
Convert result to decimal like this
SELECT
CONVERT(DECIMAL(16,10), COUNT * RISK) AS DecimalResult
FROM dbo.whatever;
Or convert COUNT to decimal
SELECT CAST(COUNT AS DECIMAL(16,10)) * RISK
This question is really suspicious. From the surface, it seems the two columns [Count] and [Risk] have different data types with [Count] as integer and [Risk] as decimal or float.
According to BOL, decimal/float data type has higher precedence, I will quote the BOL here
When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. If the conversion is not a supported implicit conversion, an error is returned. When both operand expressions have the same data type, the result of the operation has that data type
So to me, in SQL Server, when you do
Select [Total]=[Count]*[Risk] from [your_table]
You cannot get the result as shown in the original question.
See I have a COBOL column like this
05 AMOUNT PIC 999V99.
Here the V means decimal is implied. So value 123.45 will be represented as 12345
In order to convert it back to decimal(5,2) in Netezza, I tried the following
CAST('12345' AS DECIMAL(5,2)) --This will cause overflow, of course
CAST('12345' AS DECIMAL(5))/100 --Works, but looks awkward
Does any one know a better way to convert '12345' back to decimal(5,2) in Netezza?
Thanks
I use the method of converting it to pack decimal and then to numeric type.
I noticed an extra character is needed when converting to PACKED DECIMAL. The extra character can be any character.
Prefix TOOLKIT.SQLEXT. is where our extension function installed.
SELECT TOOLKIT.SQLEXT.PACKEDDECIMAL2STR(TOOLKIT.SQLEXT.NUM2PACKEDDEC('12345'||' ') ,2)::NUMERIC(5,2);
Result:
123.45
SELECT TOOLKIT.SQLEXT.PACKEDDECIMAL2STR(TOOLKIT.SQLEXT.NUM2PACKEDDEC('-12345'||' ') ,2)::NUMERIC(5,2);
Result:
-123.45
Try this:
select cast(substr(a.field,1,3)||'.'||substr(a.field,4,2) AS DECIMAL(5,2))
from (select '12345' as field) a
I have two columns of data in a SQL Server 2008 table that I need to convert from varchar(50) over to decimal. Get different errors depending on the methods I try to use.
Error converting data type varchar to numeric.
Arithmetic overflow
Some ways I have tried:
CONVERT(DECIMAL(38,10),REPLACE(REPLACE(REPLACE(RTRIM(LTRIM(value)),',',''),' ',''),'-',''))
CASE
WHEN value = 1
THEN CONVERT(numeric(14,2), value)
ELSE 'NA'
END
Here is a sample of the data in the table that I am trying to convert:
271.5
14.95
352.5
150
37.5
20.5
300
90
This shouldn't be this hard...
CAST should work well as long as the data is clean and the range is sufficient.
If you're not sure that all values are actually numeric, you can scrub them before converting them;
SELECT value, CASE WHEN ISNUMERIC(value)=1
THEN CAST(value AS NUMERIC(10,2))
END value2
FROM table1
This will convert the string value to a NUMERIC if it is actually convertible, and otherwise give the value NULL.
An SQLfiddle to test with.
In need to write the number 7.2 in a table field.
What type the field needs to be.
And what type needs the variable to be?
SQL Server 2008 has a DECIMAL type which works quite well.
You can define a precision (total number of digits) and a scale (number of digits after the decimal separator).
DECIMAL(2,1) would be: two digits, one of which after the separator (one before)
DECIMAL(5,2) would be: five figits total, two of which after the separator (three before)
See DECIMAL and NUMERIC in SQL Server Books Online for more details
In .NET, this would be a type decimal as well.
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.