SQL选择以获取两个空格之间的字符串 [英] SQL select to get a string between two spaces

查看:26
本文介绍了SQL选择以获取两个空格之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字段的名称格式为 DOE JOHN HOWARD 或 DOE JOHN H.

I have a field with names in the format DOE JOHN HOWARD or DOE JOHN H.

我需要一个查询来获取两个空格之间的字符串(本例中为 JOHN).

I need a query to get the string between the two spaces (JOHN in this case).

这里的答案 显示了当所需的子字符串位于两个 不同的 字符串之间时如何执行此操作,但是当所需的子字符串位于同一字符串的两个实例之间时(在这种情况).

The SO answer here shows how to do that when the desired substring is between two different strings, but I don't see how to apply something similar when the desired substring is between two instances of the same string (a space in this case).

我该怎么做?

推荐答案

一种方式:

select 
left(substring(fld, 
    charindex(' ', fld) + 1, len(fld)), 
    charindex(' ', substring(fld, charindex(' ', fld) + 2, len(fld)))) 

这篇关于SQL选择以获取两个空格之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆