3

Consider the following snippet of a table: enter image description here

There are 14 columns in this particular table and all of the columns not shown are OK, but starting with the strength column all of the values have been shifted over one ordinal position to the right. So 100MG should be strength and not days_sup, quantity should go in the days_sup column, drug_class should be filled with the data that's currently in H4B. At the end, everything before the comma in std_cost should go one column to the left and everything to the right of the column should stay in std_cost. So a snippet of what they table should look like would be (shortened for brevity's sake): (SQL Server 2008)

strength   days_sup   drug_class   dispfee   std_cost
100MG      6          H4B          1.5       5.06

This error in the table only happens in about .05% of the entire table's rows, and happens with the condition `where std_cost like '%,%'. Is there anyway I can update all of these columns at once? I was thinking it might be possible using a sequence table and the ordinal index of the column.

4

2 回答 2

2

由于您只有大约 9 列有问题,因此我会手动进行。像这样,

UPDATE YourTable
SET strength =  days_sup 
  , days_sup = quantity
  .....
  ,  dispfee  = LEFT( STD_COST, CHARINDEX(',',STD_COST) -1 )
  ,   STD_COST = RIGHT (STD_COST, LEN(STD_COST) - CHARINDEX(',',STD_COST))
where std_cost like '%,%'
于 2013-04-29T13:59:15.887 回答
2

如果我做对了 - 像这样:

update t 
SET strength=days_sup,
    days_sup=quantity,
    quantity=drug_class,
    drug_class=fst_fill,
    fst_fill=rfl_nbr,
    rfl_nbr=prc_typ,
    prc_typ=dispfee,
    dispfee=SUBSTRING(std_cost,1,CHARINDEX(',',std_cost,1)-1)
    std_cost=SUBSTRING(std_cost,CHARINDEX(',',std_cost,1)+1,100)

where std_cost like '%,%';
于 2013-04-29T13:58:05.460 回答