简体   繁体   English

在T-SQL中分割值

[英]Split a value in T-SQL

I have a value in one column like this: PD 10256-P1 . 我在这样的一列中有一个值: PD 10256-P1

I want to split this information & only need the number 10256 . 我想拆分此信息,只需要输入10256

How to achieve this? 如何实现呢?

You can easily do it by 您可以轻松地做到这一点

SUBSTRING('PD 10256-P1', 4, 5)

it cuts your input to 5 character string starting from 4th character. 它将您的输入剪切为从第4个字符开始的5个字符串。

result is 结果是

10256

Read more 阅读更多

Here is generalised Format for you 这是适合您的通用格式

SELECT SUBSTRING('PD 10256-P1', CHARINDEX(' ', 'PD 10256-P1'), CHARINDEX('-', SUBSTRING('PD 10256-P1', CHARINDEX(' ', 'PD 10256-P1'), LEN('PD 10256-P1')))-1) AS RequiredString;

You can replace 'PD 10256-P1' by your column name. 您可以将“ PD 10256-P1”替换为您的列名。

you can use it : 您可以使用它:

declare @var varchar(50)= 'PD 10256-P1'
select substring(@var,charindex(' ',@var,1), charindex('-p',@var,1)-charindex(' ',@var,1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM