MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 30th, 2012, 11:05 AM
macrado macrado is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 75 macrado User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 17 m 4 sec
Reputation Power: 10
Formatting a numeric value

OK, so let me preface this by saying that I know that formatting a numeric value should normally be done in the presentation layer, however, I have no say in the matter.

My problem is that I have a numeric value that needs to be returned pre-formatted with commas and three decimal places. Originally, the query returns the value as:

sum(convert(decimal(18,3), col6val)

This works, but there are no commas.

Since there is no simple way to format the sting, I had to investigate a solution. I found a few solutions saying to cast it as money, and convert that value to varchar, as follows:

convert(varchar, cast(sum(convert(decimal(18,3), col6val)) as money),1)

So as I read it, this converts col6val to decimal value with 3 decimal places of precision. Then that is summed. The resulting value is cast as money to get the commas. Finally, this is converted to varchar to preserve those commas in the return value.

When I execute this, though, I get the following error:

Error converting data type varchar to numeric.

I should say that this value is being selected into a temporary table, and that this field is of type varchar(50). If I remove the convert(varchar...) part of the query, I get no errors, but the string is still not formatted.

Can anyone help me here? Is there something I'm doing wrong, or is there a better way to format a numeric value with commas?

Thanks for any help!
__________________
~~Macrado~~

Reply With Quote
  #2  
Old April 30th, 2012, 02:51 PM
macrado macrado is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 75 macrado User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 17 m 4 sec
Reputation Power: 10
I found a workaround, figured I'd update here.

First of all, there was/is something funky going on with the temporary table that I'm selecting this data into. The field I'm referencing is a varchar field, but if I select any non-numeric characters into it, I get the error I mentioned earlier. I don't know what's going on there though, it's kind of weird. There are multiple select queries that populate that table, and in at least one of those queries, I select string data into it and it works fine. So who knows what's up there.

Anyway, my solution was to select it into the temp table as numeric data, and then just run an update query to add the commas after the fact.

I found a handy function that formats a string with commas at every 3 characters at http://stackoverflow.com/questions/4377352/how-do-i-format-a-number-with-commas-in-t-sql


Code:
CREATE FUNCTION [dbo].[fn_FormatWithCommas] 
(
    -- Add the parameters for the function here
    @value varchar(50)
)
RETURNS varchar(50)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @WholeNumber varchar(50) = NULL, @Decimal varchar(10) = '', @CharIndex int = charindex('.', @value)

    IF (@CharIndex > 0)
        SELECT @WholeNumber = SUBSTRING(@value, 1, @CharIndex-1), @Decimal = SUBSTRING(@value, @CharIndex, LEN(@value))
    ELSE
        SET @WholeNumber = @value

    IF(LEN(@WholeNumber) > 3)
        SET @WholeNumber = dbo.fn_FormatWithCommas(SUBSTRING(@WholeNumber, 1, LEN(@WholeNumber)-3)) + ',' + RIGHT(@WholeNumber, 3)



    -- Return the result of the function
    RETURN @WholeNumber + @Decimal

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Formatting a numeric value

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap