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 2008 Forums
 Transact-SQL (2008)
 Function to replace string 'NULL' with null value

Author  Topic 

stahorse
Yak Posting Veteran

86 Posts

Posted - 2013-07-03 : 04:25:35
Hi

I am selecting values to one table and populating them in another table, and I'm using a case statement to check if the word is the string 'NULL' then I replace it with value null.

e.g CASE
WHEN [Gross_Amount] = 'NULL'
THEN NULL
ELSE CAST([Gross_Amount] AS DECIMAL(18,2))
END[Gross_Amount]

So I'm thinking I can write a function that can check to see the value if it is a string NULL, if so replace it with null or return Gross_Amount if not. and rewrite that whole CASE statement, to objective here is to minimise the lines of code.

Can anyone help please.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-03 : 04:31:45
no need. better to keep it inline rather than making it a scalar UDF as scalr UDFs used over larger datasets can cause performance issues as they'll get expanded and executed once for each row in resultset

if you want you can simplify case when expression as follows

NULLIF([Gross_Amount],'NULL')

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

stahorse
Yak Posting Veteran

86 Posts

Posted - 2013-07-03 : 05:05:13
Thank you

I actually thought of that, firstly it gave me a problem then I'm getting something wrong, finally got it though

CAST(NULLIF([Gross Amount],'NULL') AS DECIMAL(18,2)) [Gross Amount]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-03 : 05:08:20
Ok...Cool

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -