最多选择一个空格的数据? [英] select data up to a space?

查看:24
本文介绍了最多选择一个空格的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下示例的 MSSQL 数据库字段:

I have an MSSQL database field that looks like the examples below:

u129  james
u300  chris
u300a jim
u202  jane
u5    brian
u5z   brian2

有没有办法选择第一组字符?基本上选择所有字符直到第一行空格?

Is there a way to select the first set of characters? Basically select all the characters up until the first line space?

我尝试使用 LEFT、RIGHT、LEN,但找不到像我的示例中那样使用可变字符串长度的方法.

I tried messing around with LEFT, RIGHT, LEN, but couldn't figure out a way to do it with variable string lengths like in my example.

谢谢!

推荐答案

您可以使用 LEFTCHARINDEX 的组合来查找第一个空格的索引,并且然后抓住它左边的所有东西.

You can use a combiation of LEFT and CHARINDEX to find the index of the first space, and then grab everything to the left of that.

 SELECT LEFT(YourColumn, charindex(' ', YourColumn) - 1) 

如果您的任何列中没有空格:

And in case any of your columns don't have a space in them:

SELECT LEFT(YourColumn, CASE WHEN charindex(' ', YourColumn) = 0 THEN 
    LEN(YourColumn) ELSE charindex(' ', YourColumn) - 1 END)

这篇关于最多选择一个空格的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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