Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Will ISNULL prevent a integer field going into neg

Author  Topic 

sql777
Constraint Violating Yak Guru

314 Posts

Posted - 2008-01-14 : 11:00:43
Hi,

I have a int field in my table names Count.

In my sproc, I am updating this field by subtracting 1 from it, but I am fearing that I might get into a situation where I am subtracting 1 from a value of 0.

intField = intField - 1

Will ISNULL prevent intField from going below 0?

So:

intField = ISNULL(intField - 1, 0)

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-14 : 11:03:50
No.
An INT column can store values ranging from -2147483648 to 2147483647.

UPDATE Table1
SET Col1 = Col1 - 1
WHERE Col1 > 0



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2008-01-14 : 16:11:15
in case intField = 1, (intField - 1) is 0 and not NULL
so ISNULL function wont work here
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-15 : 03:08:10
intField = ISNULL(NULLIF(intField, 0) - 1, intField)



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-16 : 01:50:30
quote:
Originally posted by rohitkumar

in case intField = 1, (intField - 1) is 0 and not NULL
so ISNULL function wont work here


OP didnt specify that. He/She wants to avoid having negative numbers


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -