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
 Remove decimals and add leading zeros
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

accessdbguru
Starting Member

USA
26 Posts

Posted - 08/17/2012 :  11:36:54  Show Profile  Reply with Quote
I have this simple SQL statement and I need to make it specific character length, add leading zeros and remove the decimals.

SQL is below:

SELECT COUNT ([check number]) AS TotalChecks,
SUM ([Check Amount]) AS TotalAmount FROM SAccounts
INNER JOIN Checks ON SAccounts.ID = Checks.[Account ID] where Name='PrimerBank' and Checks.[Check Date]='2012-08-13'


Result is:
TotalChecks TotalAmount
56 456567.84


Should display as:
TotalChecks TotalAmount
000000056 000045656784


Summary:
TotalCheck - should have 9 spaces with leading zeros
TotalAmount - should have 12 spaces with leading zeros and remove the decimal point

Please Help! Thank you.

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/17/2012 :  11:43:02  Show Profile  Reply with Quote
Many of the experts on this forum would recommend against doing this type of formatting in SQL - instead, they would advise you to do this on the front-end or client application if you have one. But if you do want to do this in SQL, you can do it like this:
DECLARE @specificCharacterLength INT = 20;

SELECT
	RIGHT(REPLICATE('0',@specificCharacterLength) + REPLACE(CAST(TotalChecks AS VARCHAR(32)),'.',''),@specificCharacterLength)


Edit: A word of caution though. If your data types are not exact numeric types (for example if they are float), the results may not be what you expect. If that is the case, cast the float to a decimal or another exact numeric instead of using it directly as I have done above.

Edited by - sunitabeck on 08/17/2012 11:44:25
Go to Top of Page

accessdbguru
Starting Member

USA
26 Posts

Posted - 08/17/2012 :  11:52:27  Show Profile  Reply with Quote
Thank you. Please reply with the SQL statement?
Currently I have this:

SELECT COUNT ([check number]) AS TotalChecks,
SUM ([Check Amount]) AS TotalAmount FROM SAccounts
INNER JOIN Checks ON SAccounts.ID = Checks.[Account ID] where Name='PrimerBank' and Checks.[Check Date]='2012-08-13'


Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/17/2012 :  12:08:23  Show Profile  Reply with Quote
SELECT
	RIGHT(REPLICATE('0',9) + REPLACE(CAST(COUNT([check number]) AS VARCHAR(32)),'.',''),9) AS TotalChecks,
	RIGHT(REPLICATE('0',12) + REPLACE(CAST(CAST(SUM([Check Amount]) AS DECIMAL(19,2)) AS VARCHAR(32)),'.',''),12) AS TotalAmount
FROM
	SAccounts
	INNER JOIN Checks
		ON  SAccounts.ID = Checks.[Account ID]
WHERE
	NAME = 'PrimerBank'
	AND Checks.[Check Date] = '2012-08-13'
Go to Top of Page

accessdbguru
Starting Member

USA
26 Posts

Posted - 08/17/2012 :  15:33:18  Show Profile  Reply with Quote
That works like a charm! Thank you
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/17/2012 :  15:52:45  Show Profile  Reply with Quote
You are very welcome :)

At the risk of sounding like an annoying broken record: more often than not, it is simpler and better to do the formatting at the client/presentation/reporting side.
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22461 Posts

Posted - 09/03/2012 :  10:10:43  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by sunitabeck

You are very welcome :)

At the risk of sounding like an annoying broken record: more often than not, it is simpler and better to do the formatting at the client/presentation/reporting side.


You expressed my thoughts

Madhivanan

Failing to plan is Planning to fail
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