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
 General SQL Server Forums
 New to SQL Server Programming
 NULL and Decimals in Same Column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ASpencer
Starting Member

3 Posts

Posted - 07/14/2012 :  10:16:40  Show Profile  Reply with Quote
Hello,

I am new to MS SQL. I have a table that I am trying to remove decimals from the Score and NULLs in the same column. I know I can use the floor function to get rid of the decimal and trailing 0's. But how do I get rid of the NULL value and replace with a blank space?

For Example: this is what the table current look like

SCORE Table

NULL
600.000000
NULL
NULL
350.000000

This is the code i used to get rid of the trailing 0's

floor(Score) ....which works fine but, how do I replace the NULL with a blank.


Please Help. Thank You.

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/14/2012 :  10:20:46  Show Profile  Reply with Quote
That depends on the data type of your column. If it is a numeric type - decimal, float etc. - you cannot put a blank (which is really an empty string) into it. You can set it to 0, or any other value, but that may not be what you want.

Now, if your column is a character type (and I hope it isn't) then you can replace NULLs with empty string using this:
UPDATE YourTable SET ColName = '' WHERE ColName IS NULL;
Go to Top of Page

ASpencer
Starting Member

3 Posts

Posted - 07/14/2012 :  10:38:21  Show Profile  Reply with Quote
Thanks for responding quickly.

That score field type is (decimal(23,10), null).....how do I replace the NULL value with 0 and remove the decimal point & trailing 0's in one statement.

select floor(score) ....only removes the trailing zeros. How do I replace the NULL with 0 in the same select statement?

Thank You.
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/14/2012 :  10:47:30  Show Profile  Reply with Quote
SELECT COALESCE(FLOOR(score),0) FROM YourTable;

COALESCE (OR ISNULL) function picks the first value that is non-null from the parameters. So if FLOOR(SCORE) happens to be null, it will move on to the next parameter (0) and pick that.
Go to Top of Page

ASpencer
Starting Member

3 Posts

Posted - 07/14/2012 :  10:54:15  Show Profile  Reply with Quote
Thank you very much. It worked.

Artnette
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/14/2012 :  13:10:33  Show Profile  Reply with Quote
You are very welcome.

As an aside, I assume you know that when you use floor function, it would chop off the fractional part - for example, 3.999 would get chopped to 3 rather than rounded to 4. If you want to round, use ROUND function, or CAST to decimal with scale 0.
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