?
Solved

SQL Help Convert Cast Query

Posted on 2015-01-28
7
Medium Priority
?
111 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 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 66

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

752 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