MSSQLTips.com - your daily source for SQL Server tips

Google
 
Web mssqltips.com

ESSENTIALS: Home | Tips | Search | Categories | Top 10 | Products | Authors | Blogs | Forums | Webcasts | Advertise | About
how to convert nvarchat to decimal (sql server 2005) - MSSQLTips

MSSQLTips

MSSQLTips.com - your daily source for SQL Server tips
Welcome to MSSQLTips Sign in | Join | Help
in Search

how to convert nvarchat to decimal (sql server 2005)

Last post 06-12-2008 11:08 PM by kosmas. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 06-12-2008 8:22 AM

    how to convert nvarchat to decimal (sql server 2005)

    SELECT        f1, f2, SUM(Convert(decimal(20,2), f3)
    FROM           t1
    GROUP BY   f1, f2
    ORDER BY   f1, f2

    (all three fields are nvarchar and f3 has values like '123,345.65') 

    I am getting an error when converting nvarchar can some one please help me to get around this...
    Thanks..

     

     

    Filed under:
  • 06-12-2008 11:02 AM In reply to

    • aprato
    • Top 10 Contributor
    • Joined on 12-01-2007
    • Greater Boston
    • Posts 263

    Re: how to convert nvarchat to decimal (sql server 2005)

     Is the above the actual query?  If so, you're missing a closing parenthesis (see below)

    SELECT        f1, f2, SUM(Convert(decimal(20,2), f3)) -- close off the select with a trailing paren
    FROM           t1
    GROUP BY   f1, f2
    ORDER BY   f1, f2

  • 06-12-2008 11:50 AM In reply to

    Re: how to convert nvarchat to decimal (sql server 2005)

    aprato:

     Is the above the actual query?  If so, you're missing a closing parenthesis (see below)

    SELECT        f1, f2, SUM(Convert(decimal(20,2), f3)) -- close off the select with a trailing paren
    FROM           t1
    GROUP BY   f1, f2
    ORDER BY   f1, f2

    No the error is not the parenthesis....
    it throws error unable to convert string to numeric/decimal!

    is there a way to convert nvarchar to decimal ?

      

     

  • 06-12-2008 1:17 PM In reply to

    • aprato
    • Top 10 Contributor
    • Joined on 12-01-2007
    • Greater Boston
    • Posts 263

    Re: how to convert nvarchat to decimal (sql server 2005)

     Hmmmmm.... the convert should work. Are you sure that you don't have non numeric values in that nvarchar?  Perhaps
    the following would do the trick?

     

    SELECT        f1, f2, SUM(Convert(decimal(20,2), case when isnumeric(f3) <> 1 then '0.00' else f3 end))

    FROM           t1
    GROUP BY   f1, f2
    ORDER BY   f1, f2

  • 06-12-2008 11:08 PM In reply to

    • kosmas
    • Top 50 Contributor
    • Joined on 06-11-2008
    • Greece
    • Posts 4

    Re: how to convert nvarchat to decimal (sql server 2005)

     The problem is that the decimal type ignores the thousant separators (,). Only the money data type handle this.

    Try to cast first in money and then in decimal 

    SELECT        f1, f2, SUM(Convert(decimal(20,2), cast(f3 as money)))
    FROM           t1
    GROUP BY   f1, f2
    ORDER BY   f1, f2


     

Page 1 of 1 (5 items)