Thursday, March 22, 2012

Conversion

Hi,
Can I convert an nvarchar field to numeric type?
Thanks
Chandrayes
thanks,
Jose de Jesus Jr. Mcp,Mcdba
Data Architect
Sykes Asia (Manila philippines)
MCP #2324787
"Chandra" wrote:

> Hi,
> Can I convert an nvarchar field to numeric type?
> Thanks
> Chandra|||try
Cast (myNvarCharTableColumn as numeric)
"Chandra" wrote:

> Hi,
> Can I convert an nvarchar field to numeric type?
> Thanks
> Chandra|||Hi,
declare @.t nvarchar(10)
set @.t='10'
select convert(numeric(5,2),@.t)
Ensure that you have no character data in te column
Thanks
Hari
SQL Server MVP
"RioDD" <RioDD@.discussions.microsoft.com> wrote in message
news:641160B8-BA5D-4FF4-92E3-76C39B2FA79B@.microsoft.com...
> try
> Cast (myNvarCharTableColumn as numeric)
> "Chandra" wrote:
>|||Also look at this recent thread for some details on numeric data types:
http://msdn.microsoft.com/newsgroup...71-b7768b43fcf3
...in case you missed it.
ML|||I tried Hari's solution but it is giving the following error.
Server: Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to numeric.
what could be the reason?
Thanks
Chandra
"Hari Pra" wrote:

> Hi,
> declare @.t nvarchar(10)
> set @.t='10'
> select convert(numeric(5,2),@.t)
> Ensure that you have no character data in te column
> Thanks
> Hari
> SQL Server MVP
> "RioDD" <RioDD@.discussions.microsoft.com> wrote in message
> news:641160B8-BA5D-4FF4-92E3-76C39B2FA79B@.microsoft.com...
>
>|||Obviously your nvarchar column contains values that cannot be converted to
the numeric data type. Read my other post in this thread.
It's imperative that you test values before attempting to cast/convert them,
since in some circumstances conversions even if successful can lead to
unexpected results.
And when it comes to numbers certain aspects of life can become devastated
by such faults.
ML|||>I tried Hari's solution but it is giving the following error.
> Server: Msg 8114, Level 16, State 5, Line 1
> Error converting data type nvarchar to numeric.
> what could be the reason?
The reason is that you have non-numeric data in your NVARCHAR column. How
is this supposed to work?
SELECT CONVERT(NUMERIC(5,2), 'fooblat$%xpq@.g')
?|||Don't you know that 'fooblat$%xpq@.g' = 42?
:)
ML|||Of course, I do. But I haven't applied that specific hotfix to my
production SQL Servers yet. :-)
"ML" <ML@.discussions.microsoft.com> wrote in message
news:D5B6DD32-14D3-4413-998F-8F2789EE7747@.microsoft.com...
> Don't you know that 'fooblat$%xpq@.g' = 42?
> :)
>
> ML

No comments:

Post a Comment