1 Replies - 1102 Views - Last Post: 23 May 2013 - 08:52 AM

#1 kristina1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 30-July 12

Why does ISNull function return null value

Posted 06 April 2013 - 09:47 AM

I wrote a stored procedure for inserting in a table and i added this code in it
declare @count as int
select @count=ISNULL(COUNT(*),0) from [my_tableName]
set @parameterName=CAST(@count as varchar(50))

The problem is that some records in the table, have a column (that one which i fill with the @parameterName ) with the value of Null and i don't understand why. It happens one in 100 records, but it happens.
If someone has experienced some similar situation, i would be grateful if he/she would give some help to understand what is wrong.
I wrote the code meaning that @parameter would have the value of 0 or the number or records (Count(*)) of the table.
Thanks a lot

Is This A Good Question/Topic? 0
  • +

Replies To: Why does ISNull function return null value

#2 Psyguy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 365
  • Joined: 12-January 11

Re: Why does ISNull function return null value

Posted 23 May 2013 - 08:52 AM

I'm not sure why you would use a ISNULL on a COUNT(*). The COUNT function can only return a number. If there is no data, it returns zero. Have you tried just using

declare @count int

select @count = count(*)
from [TableName]
set @parameterName = convert(varchar(50), @count)

I used convert rather than cast.

EDIT: see the following thread

This post has been edited by Psyguy: 23 May 2013 - 08:54 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1