我有这样的数据:
+--+----------+--------+------+
|Id|class_name|class_id|medals|
+--+----------+--------+------+
|1 |7IPA1 |7 |3 |
|2 |7IPA2 |7 |2 |
|3 |7IPA3 |7 |5 |
|4 |8IPA1 |8 |1 |
|5 |8IPA2 |8 |7 |
|6 |8IPA3 |8 |3 |
+--+----------+--------+------+
我希望class_id
上的数据为7IPA& 8IPA(class_name中的第4个字符)。
答案 0 :(得分:2)
您必须使用substring功能:
UPDATE MYTABLE SET CLASS_ID=SUBSTRING(CLASS_NAME,1,4)
答案 1 :(得分:2)
另一种方法是使用LEFT
字符串函数
Select LEFT(CLASS_NAME,4) from yourtable
看起来你想要一个新列而不是更新现有列,我建议你创建一个计算列
alter table yourtable add new_class_id as (left(class_name,4)) persisted