I have a string column in SQL Server that contains data of payment. The column contains two data separated by comma, but may sometimes contain only one. I need to be able to split this single column into two new columns and convert this to money and perform a sum of the two.
Sample Payment Column
+---------+
| Payment |
+---------+
| 1000 |
+---------+
| 500, 100|
+---------+
| 10,20 |
+---------+
desired output
+---------+---------+--------+-------+
| Payment | split1 | split2 | total |
+---------+---------+--------+-------+
| 1000 | 1000 | 0 | 1000 |
+---------+---------+--------+-------+
| 500, 100| 500 | 100 | 600 |
+---------+---------+--------+-------+
| 10,20 | 10 | 20 | 30 |
+---------+---------+--------+-------+
I'd like to query this in a single select statement without using a function.
Solution
I have formulated the solution based on the links provided by the commentators. Since I wanted not to use cursors or functions, here is the complete select statement that answers my question:
SELECT
cast(substring('1000,789',1,CHARINDEX(',', '1000,789')-1) as decimal(10,2)) AS Payment1,
cast(substring('1000,789',CHARINDEX(',', '1000,789')+1,len('1000,789')) as decimal(10,2))
as Payment2,
cast(substring('1000,789',1,CHARINDEX(',', '1000,789')-1) as decimal(10,2)) +
cast(substring('1000,789',CHARINDEX(',', '1000,789')+1,len('1000,789')) as decimal(10,2))
as TotalPayment from payment;