Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 26th, 2006, 09:26 AM
Authorized User
 
Join Date: Oct 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default convert nvarchar to money

I've looked in Transact-SQL help and searched P2P but can't see that I can convert an nvarchar column to money type. My goal is, to take numbers and format them with a comma as thousands separator. The nvarchar column contains numbers that were converted to nvarchar for another reason, and now I want them back as numbers.

When I do this:
   case when columnx='.00' then '' else convert(money,columnx,1) end
I get an error saying that an implicit conversion is not allowed and I must use the convert function. ????
I'm using money,1 because I think that style allows for a comma as thousands separator, but I got it from Help in the context of changing FROM money TO nvarchar, the other way 'round from what I want to do.
 
Old October 26th, 2006, 01:06 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

Try this as a sample


declare @money money;
set @money=9999;
select CAST ( @money AS varchar (50 ) )Money;

or
select cast (9999 as varchar(30)) Your_C

Jaime E. Maccou
 
Old October 26th, 2006, 06:37 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

This will do it... be warned... numeric datatypes cannot hold commas... only character data types can... that's why the code below converts NVARCHAR to MONEY and then back again using the formatting operand of "1" in the convert.

DECLARE @TestNum NVARCHAR(16)
    SET @TestNum = N'1234567.89'

SELECT CONVERT(NVARCHAR(16),CAST(@TestNum AS MONEY),1)


Obviously, this is just test code and you need to do something more like the following...


 SELECT CONVERT(NVARCHAR(16),CAST(yourcolumnname AS MONEY),1) AS somename
FROM yourtablename

--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert from nvarchar(25) to smalldatetime aspless SQL Language 6 September 14th, 2007 09:21 AM
convert data from nvarchar to Date Time rahulgzb SQL Server 2000 4 September 7th, 2007 09:45 PM
change nvarchar to varchar vincentc SQL Server 2000 3 May 24th, 2005 10:56 PM
Convert int to nvarchar bekim SQL Language 1 August 12th, 2004 06:33 AM
retrieve numerice value from nvarchar mateenmohd SQL Server 2000 6 July 21st, 2003 03:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.