【问题标题】:SQL Server String Manipulation, splitting column value into 2 seperate valuesSQL Server 字符串操作,将列值拆分为 2 个单独的值
【发布时间】:2017-03-09 02:17:23
【问题描述】:

我正在使用 SQL Server 2014 ....

Select testvalue from testtable 

返回

[000001][xXCEWkC+WDhe7EYo6feDmQ==]mnjQ3UkMjb1swK1wCTT75Q==

如何将此值拆分为 2 个不同的值?

  1. 第二个括号中的值
  2. 第二组括号后的值

【问题讨论】:

  • 您使用的是哪个 DBMS?

标签: sql sql-server string data-manipulation


【解决方案1】:

如果你使用 MySQL,你可以使用 substringlocate

SELECT SUBSTR(testvalue,
1,LOCATE("]",testvalue,LOCATE("]",testvalue)+1)) AS column1,
SUBSTR(testvalue,
LOCATE("]",testvalue,LOCATE("]",testvalue)+1)+1) AS column2 FROM testtable

【讨论】:

  • 谢谢,但您知道如何在 SQL Server 中完成此操作吗?
【解决方案2】:

SQL 服务器

select      right(testvalue,charindex(']',reverse(testvalue))-1)                                                            as col_1_option_a
           ,right(testvalue,len(testvalue)-patindex('%][^[]%',testvalue))                                                   as col_1_option_b
           ,right(left(testvalue,patindex('%][^[]%',testvalue)-1),patindex('%][^[]%',testvalue)-charindex(']',testvalue)-2) as col_2

from        testtable
;

MySQL

select  substring_index(substring_index(testvalue ,'[',-1),']',1)
       ,substring_index(testvalue ,']',-1)

from    testtable
;

【讨论】:

  • 谢谢,但是您知道如何在 SQL Server 中完成此操作吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
相关资源
最近更新 更多