1

I need to copy part of one column into another column. The delimiter is "-". I don't want to remove that part from the first column.

Example:

ItemDesc            Part#
Glowear_black-1234

So it needs to look like this:

ItemDesc            Part#
Glowear_black-1234  1234

The only SQL query I can find cuts the information from the ItemDesc column and pastes it into Part#. I still need the "1234" in the first column. Also not all of the ItemDesc have a "-" (which is fine).

4

2 回答 2

0

对于 ItemDesc 缺少连字符作为分隔符的情况,目前尚不清楚该怎么做,但这是我的建议。使用 substring 函数获取连字符右侧的所有内容,其中右侧的所有内容都由连字符的索引加一和字符串的长度限制:

update user.table
set Part# = substring(ItemDesc, (CHARINDEX('-', ItemDesc)+1), LEN(ItemDesc) )

从评论中获取您的陈述:

UPDATE tblIOT1 
SET Part#= Trim(Mid(ItemDesc, InStr(ItemDesc, '-') +1)) 
where ItemDesc like "*-*"

请注意,我使用的是星号,而另一位受访者使用的是百分号,两者都似乎对 MS Access 有效,如此处所指定

于 2013-10-17T17:50:13.870 回答
0

在 Access SQL 中,您可以使用InStr()来查找“-”在您的字段中的位置。然后您可以使用Mid()返回在该位置之后开始一个字符的子字符串。

请注意,由于其名称中的字符,您必须Part#在 SQL 语句中使用括号。#

UPDATE tblIOT1
SET [Part#] =
    Mid(ItemDesc, InStr(1, ItemDesc, "-") + 1)

如果ItemDesc可以为 Null 或包含不带“-”的字符串,则添加一个WHERE子句以忽略UPDATE.

WHERE ItemDesc ALike '%-%'
于 2013-10-17T19:17:50.807 回答