22

Im inserting Data into a database, they have a decimal and a negative number, is there a way to the DataType Decimal into negative numbers or is there another data type I can use?

1
  • 1
    There's nothing wrong with negative decimal values but I think your question perhaps needs some clarification. What is it precisely that you'd like to do?
    – Carth
    Nov 7, 2011 at 20:11

3 Answers 3

24

The decimal datatype can store negative numbers as well. So to answer your question, yes you can use the decimal datatype to store negative decimal numbers.

Here is some proof:

create table NegativeDecimal
(
    somedec decimal(10, 4) not null
)
go

insert into negativedecimal
select -12.3
union all
select 16.4
go

select *
from NegativeDecimal

somedec
---------------------------------------
-12.3000
16.4000

(2 row(s) affected)

EDIT: This is provided you are using SQL Server. Please specify your RDBMS.

5

You don't state which DBMS you're using, but on MySQL at least, negatives are supported in decimals:

mysql> create table x (x decimal(5,2));
Query OK, 0 rows affected (0.06 sec)

mysql> insert into x (x) values (-3.14);
Query OK, 1 row affected (0.00 sec)

mysql> select * from x;
+-------+
| x     |
+-------+
| -3.14 |
+-------+
1 row in set (0.00 sec)
0

For math definition decimal number could be positive or negative and the data structures know that, for the same reason is not a special type for negative or positive numbers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.