[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL Help Convert Cast Query

Posted on 2015-01-28
7
Medium Priority
?
118 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

649 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