Solved

SQL Help Convert Cast Query

Posted on 2015-01-28
7
98 Views
Last Modified: 2015-02-02
I have a table with the following data

ID      Max Value
1      1.56
2      10.753
3      100.008
4      18.6
5      .4
6      2.47
7      1.49
8      13.65
9      29.68
10      37.22

I want to select all records, but have Max Value displayed like 000.000.

For Example

ID      Max Value
1      001.560
2      010.753
3      100.008
4      018.600
5      000.400
6      002.470
7      001.490
8      013.650
9      029.680
10      037.220

Any help is appreciated
0
Comment
Question by:PeelSeel2
  • 3
  • 2
7 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
Comment Utility
>I want to select all records, but have Max Value displayed like 000.000.
The below code block works, but couple of things you'll have to work out...
What's the data type of MaxValue?  If something other then numeric(?, 3) then you'll have to handle the three decimal points in the query by using CAST(? as numeric(?,3) in the inside of MaxValue
How to handle values 1,000 or greater, or negative numbers?
IF OBJECT_ID('tempdb..#tmp') IS NOT NULL
	DROP TABLE #tmp
GO

CREATE TABLE #tmp (id int, MaxValue numeric(19,3))

INSERT INTO #tmp (id, MaxValue) 
VALUES
(1,1.56), (2,10.753), (3,100.008), (4,18.6), (5,.4), 
(6,2.47), (7,1.49), (8,13.65), (9,29.68), (10,37.22)

SELECT id, RIGHT('00' + CAST(MaxValue as varchar(100)),7)
FROM #tmp

Open in new window

0
 

Author Comment

by:PeelSeel2
Comment Utility
I've requested that this question be deleted for the following reason:

Found answer not relating to posted solution.
0
 

Author Comment

by:PeelSeel2
Comment Utility
This worked

select id,
, format(Max Value, '000.000') as value

from table
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
... Ok, and did what I gave you not work?
0
 

Author Comment

by:PeelSeel2
Comment Utility
Your solution works great.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now