Question

I have a table with a string field where than can be one number or mulitple numbers (delimited by a comma). I need to find the difference between the values (when converted to an integer) and an unspecified value. For simplicity sake for this question, I'll just say the value to be compared is a static value of 10.

Example Table:

iId vchStringNumbers vchSubtractedStringNumbers 1 20, 30, 40 2 50 3 20

Desired Results:

iId vchStringNumbers vchSubtractedStringNumbers 1 20, 30, 40 10, 20, 30 2 50 40 3 20 10

Is there a way to accomplish this in SQL? If it would be eaiser in excel or something like that, feel free to answer as well.

Was it helpful?

Solution 2

Place you CS data in an Excel column, Select the cells and run this tiny VBA macro:

Sub SubtractCSV()
    Dim r As Range
    For Each r In Selection
        ary = Split(r.Value, ",")
        For i = LBound(ary) To UBound(ary)
            ary(i) = CLng(Trim(ary(i))) - 10
        Next i
        r.Offset(0, 1).Value = Join(ary, ",")
    Next r
End Sub

OTHER TIPS

If one has access to Excel 2019 or Excel O365, one could also use (in B2):

=TEXTJOIN(", ",,FILTERXML("<t><s>"&SUBSTITUTE(B2,",","</s><s>")&"</s></t>","//s")-10)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top