2

In an SQL Server database, I have to update some fields, the query is (, is the decimal separator) :

UPDATE MyTbable 
SET 
Field1 =   CAST('111,11' AS DEC(18,4)),
Field2 =  CAST('222,22' AS DEC(18,4)),
Field3 =  CAST('333,33' AS DEC(18,4))
WHERE Id = '1'

I receive the error : Error converting data type varchar to numeric. I tried with the cast, same problem

Field1,Field2 and Field3 are numeric 38,2

Any idea ?

Thanks,

1
  • try to remove the char ',' in the string Commented Feb 15, 2012 at 11:17

3 Answers 3

2

Take the commas out.

UPDATE MyTbable 
SET 
Field1 =  CAST(REPLACE('111,11',',','.') AS DEC(18,4)),
Field2 =  CAST(REPLACE('222,22',',','.') AS DEC(18,4)),
Field3 =  CAST(REPLACE('333,33',',','.') AS DEC(18,4))
WHERE Id = '1'
Sign up to request clarification or add additional context in comments.

Comments

1

Use . instead of ,

UPDATE MyTbable 
SET 
  Field1 =  CAST('111.11' AS DEC(18,4)),
  Field2 =  CAST('222.22' AS DEC(18,4)),
  Field3 =  CAST('333.33' AS DEC(18,4))
WHERE Id = '1'

If you want to select these values afterwards as string and with comma then do that:

select replace(cast(Field1 as varchar(30)), '.',','),
       replace(cast(Field2 as varchar(30)), '.',','),
       replace(cast(Field2 as varchar(30)), '.',','),
where Id = '1'

1 Comment

OK but when I do a select, the separator must be comma
1

remove the commas in you query like :

UPDATE MyTbable 
SET 
Field1 =   CAST('11111' AS DEC(18,4)),
Field2 =  CAST('22222' AS DEC(18,4)),
Field3 =  CAST('33333' AS DEC(18,4))
WHERE Id = '1'

or you can rmove the comma using replace:

replace('111,11',',','.')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.