Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / SQL / December 2008

Tip: Looking for answers? Try searching our database.

Converting decimal to specific char format

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rick - 30 Dec 2008 22:54 GMT
In a stored procedure, I have a decimal-defined variable as DEC(13,2).  
I need to write its value to a column in a table I'll be creating.  This
column needs to display its value as 11 chars, with the first char being
a space and values after that padded with beginning zeroes:

2850.00     should display as ' 0002850.00'
999126.88      should display as ' 0999126.88'
0.00        should display as ' 0000000.00'

Any ideas on how to do this?  I'm trying to decide on how to define the
column, and how to convert the DEC(13,2) variable when I'm inserting it.  
Thanks for any help.
Erland Sommarskog - 30 Dec 2008 23:27 GMT
> In a stored procedure, I have a decimal-defined variable as DEC(13,2).  
> I need to write its value to a column in a table I'll be creating.  This
[quoted text clipped - 8 lines]
> column, and how to convert the DEC(13,2) variable when I'm inserting it.  
> Thanks for any help.


' ' + replace(str(@decval, 10, 2), ' ', '0')

Beware that if the value exceeds 9 999 999, the value will be replaced
by stars.

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

--CELKO-- - 31 Dec 2008 00:28 GMT
Display is done in the front end in a properly designed tiered
architecture, not in the database.  Is this nameless data element used
for computations or is it a string that happens to have digits?  A
data element should be represetrned in one and only one format in the
schema.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.