Solved

SQL Help Convert Cast Query

Posted on 2015-01-28
7
101 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
ID: 40575991
>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
ID: 40584636
I've requested that this question be deleted for the following reason:

Found answer not relating to posted solution.
0
 

Author Comment

by:PeelSeel2
ID: 40584740
This worked

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

from table
0
 
LVL 65

Expert Comment

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

Author Comment

by:PeelSeel2
ID: 40584863
Your solution works great.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

803 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