October 18, 2018 at 10:09 am
I have a varchar column that has amounts that are mixed ie
42,180.00
$123.44
4,555.99
£100,000
$1,393.21
I want to create a new column with those amounts above as a decimal/numeric type column. How can i do this?
October 18, 2018 at 10:17 am
GrassHopper - Thursday, October 18, 2018 10:09 AMI have a varchar column that has amounts that are mixed ie
42,180.00
$123.44
4,555.99
£100,000
$1,393.21I want to create a new column with those amounts above as a decimal/numeric type column. How can i do this?
select CONVERT(decimal(10,2),REPLACE(REPLACE(REPLACE(T.C,'$',''),'£',''),',','')) myDecimalColumn, *
from (values ('42,180.00'),
('$123.44'),
('4,555.99'),
('£100,000'),
('$1,393.21')) T(C)
October 18, 2018 at 10:57 am
Jonathan AC Roberts - Thursday, October 18, 2018 10:17 AMGrassHopper - Thursday, October 18, 2018 10:09 AMI have a varchar column that has amounts that are mixed ie
42,180.00
$123.44
4,555.99
£100,000
$1,393.21I want to create a new column with those amounts above as a decimal/numeric type column. How can i do this?
select CONVERT(decimal(10,2),REPLACE(REPLACE(REPLACE(T.C,'$',''),'£',''),',','')) myDecimalColumn, *
from (values ('42,180.00'),
('$123.44'),
('4,555.99'),
('£100,000'),
('$1,393.21')) T(C)
Haven't tried this particular set of data but it might be a little faster to simply convert them to the MONEY datatype and then to decimal. The MONEY datatype isn't bothered by commas and valid currency markers.
Of bigger concern might be the fact that once these are converted to decimal, the currency indications will be gone unless other measures are taken.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 18, 2018 at 11:06 am
Jonathan AC Roberts - Thursday, October 18, 2018 10:17 AMGrassHopper - Thursday, October 18, 2018 10:09 AMI have a varchar column that has amounts that are mixed ie
42,180.00
$123.44
4,555.99
£100,000
$1,393.21I want to create a new column with those amounts above as a decimal/numeric type column. How can i do this?
select CONVERT(decimal(10,2),REPLACE(REPLACE(REPLACE(T.C,'$',''),'£',''),',','')) myDecimalColumn, *
from (values ('42,180.00'),
('$123.44'),
('4,555.99'),
('£100,000'),
('$1,393.21')) T(C)
This works great when all rows are populated with a value. There are rows that have blank values or just a "-" (negative sign). How do i get around that?
October 18, 2018 at 11:21 am
GrassHopper - Thursday, October 18, 2018 11:06 AMJonathan AC Roberts - Thursday, October 18, 2018 10:17 AMGrassHopper - Thursday, October 18, 2018 10:09 AMI have a varchar column that has amounts that are mixed ie
42,180.00
$123.44
4,555.99
£100,000
$1,393.21I want to create a new column with those amounts above as a decimal/numeric type column. How can i do this?
select CONVERT(decimal(10,2),REPLACE(REPLACE(REPLACE(T.C,'$',''),'£',''),',','')) myDecimalColumn, *
from (values ('42,180.00'),
('$123.44'),
('4,555.99'),
('£100,000'),
('$1,393.21')) T(C)This works great when all rows are populated with a value. There are rows that have blank values or just a "-" (negative sign). How do i get around that?
Use Jeff's solution:select convert(decimal(9,2),convert(money,T.C)) myDecimalColumn,*
from (values ('42,180.00'),
('$123.44'),
('4,555.99'),
('£100,000'),
('-'),
(''),
('$1,393.21')) T(C)
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply