3

How can I get the value from the sixth field in the following column? I am trying to get the 333 field:

ORGPATHTXT
2123/2322/12323/111/222/333/3822

I believe I have to use select substring, but am unsure how to format the query

4

6 回答 6

2

假设 SQL Server

我能想到的最简单的方法是创建一个基于“/”拆分的拆分函数,然后提取如下所示的第六项

declare @text varchar(50) = '2123/2322/12323/111/222/333/3822'

select txt_value from fn_ParseText2Table(@text, '/') t where t.Position = 6

我使用了这个 url中的函数。看到它在 SQLFiddle 工作

于 2013-01-31T22:10:16.037 回答
2

试试这个 - 一个字符串variable 或包装成一个function与选择查询一起使用(Sql-Demo

Declare @s varchar(50)='2123/2322/12323/111/222/333/3822'

Select @s = right(@s,len(@s)- case charindex('/',@s,1) when 0 then len(@s) 
                              else charindex('/',@s,1) end)
From ( values (1),(2),(3),(4),(5)) As t(num)

Select case when charindex('/',@s,1)>0 then left(@s,charindex('/',@s,1)-1) 
            else @s end

--Results
333
于 2013-01-31T22:30:10.780 回答
1

I'd like to offer a solution that uses CROSS APPLY to split up any delimited string in MSSQL and ROW_NUMBER() to return the 6th element. This assumes you have a table with ORGPATHTXT as a field (it can easily be converted to work without the table though):

SELECT ORGPATHTXT
FROM (
  SELECT 
    Split.a.value('.', 'VARCHAR(100)') AS ORGPATHTXT,
    ROW_NUMBER() OVER (PARTITION BY ID ORDER BY (SELECT 1)) RN
  FROM  
    (SELECT ID, CAST ('<M>' + REPLACE(ORGPATHTXT, '/', '</M><M>') + '</M>' AS XML) AS String  
     FROM  MyTable
  ) AS A 
  CROSS APPLY String.nodes ('/M') AS Split(a)
  ) t 
WHERE t.RN = 6;

Here is some sample Fiddle to go along with it.

Good luck.

于 2013-02-01T01:11:53.980 回答
1

对于 sql,您可以使用

declare @string varchar(65) = '2123/2322/12323/111/222/333/3822'

select substring(string,25,27) from table_name 
于 2013-02-01T04:26:07.303 回答
0

如果您使用的是 MySQL,那么您可以使用:

select substring_index(orgpathtxt, '/', 6)

我只想说,在大多数其他数据库中它不太方便。

于 2013-01-31T22:09:53.343 回答
0

您也可以使用带有动态管理功能sys.dm_fts_parser的选项

DECLARE @s nvarchar(50) = '2123/2322/12323/111/222/333/3822' 

SELECT display_term
FROM sys.dm_fts_parser('"'+ @s + '"', 1033, NULL, 0) 
WHERE display_term NOT LIKE 'nn%' AND occurrence = 6
于 2013-02-01T16:58:29.087 回答