0

以下示例数据的 SQL 命令是什么:

Table1  
ID|Description  
1 |Logon Failed. User tempUser1 is unknown
2 |Logon Failed. User Carl is unknown  


Expected result to get only the username after the word "User" and before the word "is"  
ID|Description  
1 |tempUser1  
2 |Carl

谢谢,
里尔

4

2 回答 2

1

像这样的东西应该工作:

SELECT 
  ID, 
  SUBSTRING(
    description, 
    CHARINDEX('User', description) + 5, 
    CHARINDEX('is unknown', description) - CHARINDEX('User', description) - 6
  ) AS Description
FROM table1
ORDER BY ID

substring 函数的奇怪格式只是为了将所有内容都放在一个页面上(以删除滚动条)。

编辑:

如果字符串始终以 开头,这也应该有效,并且效率更高Logon Failed. User

SELECT 
  ID, 
  SUBSTRING(Description, 20, CHARINDEX('is unknown', Description) - 21) AS Description
FROM table1
ORDER BY ID
于 2013-10-22T14:46:20.307 回答
0

您可以尝试使用以下查询,并根据您的要求替换文本或设置长度:

select substring(column_name,patindex('%tempUser%',column_name),9) from Table_Name where column_name like '%tempUser%'
于 2013-10-22T14:36:07.737 回答