简体   繁体   English

SQL子字符串右字符索引

[英]SQL Sub string Right char index

        input 
       -----------
        '12345-123'
        '3456-67'


       output
      ----------- 
      column 1    column 2
        12345       123
         3456        67

In SQL For Column 1 : 在SQL中,对于列1:

  SELECT  Substring('12345-123',1,CHARINDEX('-','12345-123')-1)

For column 2 : unable to achieve ? 对于第2列:无法实现? can any one help me 谁能帮我

Well, you can use string manipulation: 好了,您可以使用字符串操作:

select left(input, charindex('-', input) - 1) as col1,
       stuff(input, 1, charindex('-', input), '') as col2
from t;

Here is a db<>fiddle. 是一个db <>小提琴。

使用reverse()完成两列的简单方法使逻辑保持一致。

SELECT  Substring('12345-123',1,CHARINDEX('-','12345-123')-1) col1, reverse(Substring(reverse('12345-123'),1,CHARINDEX('-',reverse('12345-123'))-1)) col2

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

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