如何 append/concat 逗号分隔值上逗号后的字符串?
How to append/concat a string after a comma on a comma-separated value?
我有一列存储用逗号分隔的值。是这样的:
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| breakfast_id | breakfast_english_menu |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | Canned Orange Juice- Can of 5 to 7 oz, Oatmeal- ½ T, Almonds - (12 almonds), Milk with Coffee or Milk with Chocolate (1T.) (Only one) |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
所以我想在 'new value' 开头的每个逗号后添加一个字符串。就像
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| breakfast_id | breakfast_english_menu |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | Canned Orange Juice- Can of 5 to 7 oz, [HI]Oatmeal- ½ T, [HI]Almonds - (12 almonds), [HI]Milk with Coffee or Milk with Chocolate (1T.) (Only one) |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
欢迎任何帮助或提示。
因为你只有一个简单的模式,所以使用 REPLACE
SELECT REPLACE('ned Orange Juice- Can of 5 to 7 oz, Oatmeal- ½ T, Almonds - (12 almonds), Milk with Coffee or Milk with Chocolate (1T.) (Only one)',', ',', [HI]')
| REPLACE('ned Orange Juice- Can of 5 to 7 oz, Oatmeal- ½ T, Almonds - (12 almonds), Milk with Coffee or Milk with Chocolate (1T.) (Only one)',', ',', [HI]') |
| :----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ned Orange Juice- Can of 5 to 7 oz, [HI]Oatmeal- ½ T, [HI]Almonds - (12 almonds), [HI]Milk with Coffee or Milk with Chocolate (1T.) (Only one) |
db<>fiddle here
对于更复杂的模式,您可以使用正则表达式来查找和替换。
如果需要,您可以将 REPLACE
命令嵌套在另一个命令中以替换多个 strintg
如果您想更改数据库中的值,您可以使用此查询:
UPDATE [MyTable] SET
[breakfast_english_menu]=Replace([breakfast_english_menu],',',', [HI]')
Where [breakfast_id]=1
我有一列存储用逗号分隔的值。是这样的:
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| breakfast_id | breakfast_english_menu |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | Canned Orange Juice- Can of 5 to 7 oz, Oatmeal- ½ T, Almonds - (12 almonds), Milk with Coffee or Milk with Chocolate (1T.) (Only one) |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
所以我想在 'new value' 开头的每个逗号后添加一个字符串。就像
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| breakfast_id | breakfast_english_menu |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | Canned Orange Juice- Can of 5 to 7 oz, [HI]Oatmeal- ½ T, [HI]Almonds - (12 almonds), [HI]Milk with Coffee or Milk with Chocolate (1T.) (Only one) |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
欢迎任何帮助或提示。
因为你只有一个简单的模式,所以使用 REPLACE
SELECT REPLACE('ned Orange Juice- Can of 5 to 7 oz, Oatmeal- ½ T, Almonds - (12 almonds), Milk with Coffee or Milk with Chocolate (1T.) (Only one)',', ',', [HI]')
| REPLACE('ned Orange Juice- Can of 5 to 7 oz, Oatmeal- ½ T, Almonds - (12 almonds), Milk with Coffee or Milk with Chocolate (1T.) (Only one)',', ',', [HI]') | | :----------------------------------------------------------------------------------------------------------------------------------------------------------- | | ned Orange Juice- Can of 5 to 7 oz, [HI]Oatmeal- ½ T, [HI]Almonds - (12 almonds), [HI]Milk with Coffee or Milk with Chocolate (1T.) (Only one) |
db<>fiddle here
对于更复杂的模式,您可以使用正则表达式来查找和替换。
如果需要,您可以将 REPLACE
命令嵌套在另一个命令中以替换多个 strintg
如果您想更改数据库中的值,您可以使用此查询:
UPDATE [MyTable] SET
[breakfast_english_menu]=Replace([breakfast_english_menu],',',', [HI]')
Where [breakfast_id]=1