SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Function to replace string 'NULL' with null value
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

stahorse
Yak Posting Veteran

85 Posts

Posted - 07/03/2013 :  04:25:35  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 07/03/2013 :  04:31:45  Show Profile  Reply with Quote
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

85 Posts

Posted - 07/03/2013 :  05:05:13  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 07/03/2013 :  05:08:20  Show Profile  Reply with Quote
Ok...Cool

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000