Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Problems converting nvarchar to numeric

Author  Topic 

R
Constraint Violating Yak Guru

328 Posts

Posted - 2008-06-23 : 08:05:13
Hi all

I want to pass a list of nvarchar characters to my procedure, then import them into a temporary table as numeric(5,2) numbers.

The code below does this but the values are corrupted (e.g. 76.25 becomes 7 in the table).

Can anyone please tell me what I'm doing wrong? Thank you!


declare @separator_position int
declare @array_value nvarchar
declare @separator char

DECLARE @numberList NVARCHAR(MAX)
SET @numberList = '23|76.25|100.36|45.54|87.14|74.25|8|98|'
SET @separator = '|'

SET @separator_position = NULL
SET @array_value = NULL
DECLARE @myTable TABLE(ID INT IDENTITY(1,1), compLevel NUMERIC(5,2))
WHILE patindex('%' + @separator + '%', @numberList) <> 0
BEGIN
SELECT @separator_position = patindex('%' + @separator + '%' , @numberList)
SELECT @array_value = LEFT(@numberList, @separator_position - 1)
INSERT INTO @myTable (compLevel) VALUES (CONVERT(NUMERIC(5,2), @array_value))
SELECT @numberList = stuff(@numberList, 1, @separator_position, '')
END

SELECT * FROM @myTable

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-23 : 08:14:21
quote:
Originally posted by R

Hi all

I want to pass a list of nvarchar characters to my procedure, then import them into a temporary table as numeric(5,2) numbers.

The code below does this but the values are corrupted (e.g. 76.25 becomes 7 in the table).

Can anyone please tell me what I'm doing wrong? Thank you!


declare @separator_position int
declare @array_value nvarchar(length)
declare @separator char

DECLARE @numberList NVARCHAR(MAX)
SET @numberList = '23|76.25|100.36|45.54|87.14|74.25|8|98|'
SET @separator = '|'

SET @separator_position = NULL
SET @array_value = NULL
DECLARE @myTable TABLE(ID INT IDENTITY(1,1), compLevel NUMERIC(5,2))
WHILE patindex('%' + @separator + '%', @numberList) <> 0
BEGIN
SELECT @separator_position = patindex('%' + @separator + '%' , @numberList)
SELECT @array_value = LEFT(@numberList, @separator_position - 1)
INSERT INTO @myTable (compLevel) VALUES (CONVERT(NUMERIC(5,2), @array_value))
SELECT @numberList = stuff(@numberList, 1, @separator_position, '')
END

SELECT * FROM @myTable



You have not specified length of nvrachar variable. so it will take default value which is 1 and that is why you're always getting only first digit.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-23 : 08:15:59
Madhi has also written an article on this:-

http://sqlblogcasts.com/blogs/madhivanan/Default.aspx?PageIndex=2
Go to Top of Page

R
Constraint Violating Yak Guru

328 Posts

Posted - 2008-06-23 : 08:26:13
Hi visakh16

Thank you for your post. Your advice has solved my problem :-)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-23 : 09:23:16
quote:
Originally posted by visakh16

Madhi has also written an article on this:-

http://sqlblogcasts.com/blogs/madhivanan/Default.aspx?PageIndex=2


More accurately
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspx

The page content can be changed if posts are added

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -