1

I have a current query in an MS Access database that uses the Format() function to convert a number (long integer) to a string.

The table has been redesigned and moved to SQL server. The field I need to convert is not a money field that I need to drop everything after the period and convert it to a string with a certain number of characters.

Sample Data:

Amount
228.00000
1896.00000
6411.00000
317.00000
4830.00000
898.00000
1778.00000
1308.00000
45139.00000
424.00000

Current MS Access query:

SELECT Format(CallAmt],"000000000000000") AS Amount
FROM Fed

New query:

SELECT Convert(varchar(15), FedAmount)
FROM Fed.SM_T_Call

I would like my final Product to look like this:

Amount
000000000000228
000000000001896
000000000006411
000000000000317
000000000004830
000000000000898
000000000001778
000000000001308
000000000045139
000000000000424

I don't know how to drop everything after the period and add the extra zeros to my result. Can anyone offer any assistance?

7
  • What datatype is FedAmount in SQL Server? You said its not a money field, what is it in SQL Server after it was moved Commented Aug 16, 2011 at 17:49
  • 1
    It would help if you edit your question and show how you want the final output to look. Commented Aug 16, 2011 at 18:03
  • yep, need final output. Not all of us bother with/use VBA. Commented Aug 16, 2011 at 18:16
  • my bad, I edited to add my final product. sorry. Commented Aug 16, 2011 at 18:36
  • Use my RIGHT solution then to have leading spaces with a CAST to int Commented Aug 16, 2011 at 18:51

5 Answers 5

6

You can use:

  • CAST to decimal to have a fixed number of decimal places
  • Use STR to right justify
  • Use RIGHT('000000000000000' + CAST(MyNumber AS varchar(15)), 15) to have leading zeros

Or all of the above: but we don't know what output you want...

Edit, after update

RIGHT('000000000000000' + CAST(CAST(Amount AS int) AS varchar(15)), 15)

Edit, October 2011

SQL Server 2012 (Denali) will have Format function.

Edit, after comment by onedaywhen

DECLARE @len tinyint;
SET @len = 15;
RIGHT(REPLICATE('0', @len) + CAST(CAST(Amount AS int) AS varchar(255)), @len)
Sign up to request clarification or add additional context in comments.

3 Comments

thank you so much. I had no clue how I was going to figure this out. Sorry again for not including sample data.
Using REPLICATE('0', 15) would make your code easier to read.
@onedaywhen: True. In real life I'd probably wrap it in a UDF and have length as a parameter...
0
SELECT CAST(CAST(FLOOR(FedAmont) AS INT) AS VARCHAR(40)) + '.00'

Comments

0

You can use the decimal data type:

declare @var decimal(15,6)
set @var = 228564.12345678

And then if you want it to be a varchar after that:

select convert(varchar(15),@var)

Or in a select, it could look like this:

select 
   [field1] as OldValue,
   convert(decimal(15,6),[field1]) as NewValue
from 
   myTable

Comments

0
select Cast(Cast(fedamount as int), as varchar(15))
from Fed.SM_T_Call

Might actually work?

Comments

0

@gbn'd answer is a good one for SQL Server and uses proprietary functions RIGHT() and REPLICATE().

Noting that you've already had to port this once, the following may be a more portable version because it uses Standard SQL functions. Note the string concatenation operator in Standard SQL is ||, whereas SQL Server uses + for the same (and MS still claims it is compliant, go figure). However, the proposed code still scores highly on the portability scale because transforming `|| to + is trivial:

REVERSE(CAST(REVERSE(CAST(CAST(Amount AS INTEGER) AS VARCHAR(15))) 
   + '000000000000000' AS CHAR(15)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.