0

Using a script that doesnt accept null but Nan i'm trying to insert null value instead of 0 in sql server table. So to do that I had an idea to create stored procedure that will cast the Nan to null float. here is the stored procedure :

create procedure [dbo].fromNanToNull(@val VARCHAR)
   AS BEGIN
   DECLARE @Work float
   if @val like 'N%'    set @Work=CAST(NULL AS float)
   else set @work=CAST(@val AS float)
    return @work
   END

Execution :

 exec  [dbo].fromNanToNull 'Nan'

but instead of getting null in return i Got this message which tell that the return is forced to 0 instead. Here is the message :

The 'fromNanToNull' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.

2
  • 1
    The first immediate error I see is your statement set @Work=CAST(NULL AS float). NULL is NULL, meaning NOTHING and hence it should not be cast(ed) to any type. Commented Sep 19, 2018 at 10:14
  • @FDavidov totally agree with you. Commented Sep 19, 2018 at 10:18

1 Answer 1

1

As per my understanding @val is starting with n means you are converting @work value to float but even @work is float datatype only if @val is starting with n means you should display @work value else table value please explain clearly

    CREATE PROCEDURE [DBO].FROMNANTONULL(@VAL VARCHAR)
       AS BEGIN
      DECLARE @VAL VARCHAR(10)
 DECLARE @WORK FLOAT
IF (@VAL LIKE 'N%') 
  BEGIN 
SET @WORK=CAST(NULL AS FLOAT)
  END 
ELSE 
  BEGIN 
SET @WORK=CAST(@VAL AS FLOAT)
  END
       END
Sign up to request clarification or add additional context in comments.

2 Comments

It was the retrun statement that throw the error. The exepression @WORK=CAST(NULL AS FLOAT) is useless. I could use @work = null instead. Thank you for your help
so now you don't want any return value or what

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.