Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | 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)
 round - show leading zero
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

arkiboys
Flowing Fount of Yak Knowledge

1433 Posts

Posted - 10/11/2012 :  07:24:04  Show Profile  Reply with Quote
I would like to get the leading zero in the below sql.

declare @NewNum float = 0.701
declare @dp tinyint = 2
set @NewNum = ROUND(@NewNum, @dp)
print @Newnum

Should return 0.70 if @dp is 2 decimal places.
or if you pass the number 0 it should return 0.00

How is this done please?
Thanks

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/11/2012 :  07:42:41  Show Profile  Reply with Quote
CAST(@NewNum AS DECIMAL(19,2));
If you have very precise/stringent formatting requirements, usually that is better done at the presentation side (such as a client GUI, reporting services etc.)
Go to Top of Page

arkiboys
Flowing Fount of Yak Knowledge

1433 Posts

Posted - 10/11/2012 :  07:50:34  Show Profile  Reply with Quote
Hi,
This does not seem to work in my sql.
I tried what you mentioned but still gives 0.7 and NOT 0.70
Go to Top of Page

SergioM
Posting Yak Master

170 Posts

Posted - 10/12/2012 :  14:09:49  Show Profile  Reply with Quote
Hmm, it works for me. Try it.
DECLARE @NewNum float = 0.701
PRINT CAST(@NewNum AS DECIMAL(19,2));


-Sergio
I use Microsoft SQL 2008
Go to Top of Page

arkiboys
Flowing Fount of Yak Knowledge

1433 Posts

Posted - 10/13/2012 :  16:48:41  Show Profile  Reply with Quote
Thanks
Go to Top of Page
  Previous Topic Topic Next 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.21 seconds. Powered By: Snitz Forums 2000