SQL Server 2008 : I have a table with three columns Ship Qty, Size, Qty.
We need to update the column Qty by multiplying two other columns.
At present this is the data in the table
Ship Qty Size Qty
2 1*100 EA NULL
3 3*20 ML
Expected result after query
Ship Qty Size Qty
2 1*100 EA 200
3 3*20 ML 180
- Split Size to Size1 (Before X) and Size2 (Behind X) (e.g 1X100 EA => Size1 = 1, SIze2=100)
- Multiply Ship Qty * Size1 * Size2
- Update Field qty with the result where qty is null or empty
I have no idea, how can I do it inside SQL Server? I am sure a stored procedure would be helpful !
Here is one try. It makes a lot of assumptions, e.g. that no space will occur until AFTER Size2 and that both sides of the * (or X from your description – which is it?) will be convertible to INT.
I strongly recommend storing this data better. Instead of storing “1*100 EA” store 1 in a column, 100 in a column, and EA in a column. This is going to be a nightmare to maintain, never mind to enforce that the size column gets consistent data (and that your formula will work against all future implementations).
I also suggest not having column names with special characters (e.g. spaces). Nobody likes having to type square brackets when they can be avoided.
Finally, if you can enforce the data on input, you should consider a computed column or view. You shouldn’t have to constantly go back and update the table after the fact.