Groups | Blog | Home
all groups > sql server (alternate) > april 2004 >

sql server (alternate) : Converting negative value


Espen Johnsen
4/14/2004 11:39:11 AM
Hi!

I have a large table width values like this:

000024634300
000-37500000
002783868891
000009603857
000-60000000
000001672396
000000195200
000010315112
000017000000

I need to convert the numbers into an integer-type field. Is this
possible with just native sql-commands? I don't want to make an
active-x script because it will be very slow on the large table.

David Portas
4/14/2004 12:38:09 PM
SELECT col,
CASE WHEN col LIKE '%-%' THEN -1 ELSE 1 END*
CAST(REPLACE(col,'-','') AS NUMERIC(12))
FROM SomeTable

The value 2,783,868,891 exceeds the maximum for an INTEGER column so I've
used NUMERIC here. You could also use BIGINT.

--
David Portas
SQL Server MVP
--

AddThis Social Bookmark Button