简体   繁体   English

从 SQL Server 表中的带连字符的字符串列中提取第 1 个第 2 个第 3 个值直到第 n 个值

[英]Extract 1st 2nd 3rd values till nth value from hyphenated string column in SQL Server table

Table has values like 12-43-5765-234-56-24-6456-234-678-9-0-0 with hyphens表具有像12-43-5765-234-56-24-6456-234-678-9-0-0这样的12-43-5765-234-56-24-6456-234-678-9-0-0带连字符

I need to take the first value as a separate column and 2nd value as separate column and so on.我需要将第一个值作为单独的列,将第二个值作为单独的列,依此类推。

I tried like this.我是这样试的。

DECLARE @S VARCHAR(MAX);
SET @S = '12-43-5765-234-56-24-6456-234-678-9-0-0'

SELECT SUBSTRING(@S, 0, CHARINDEX('-', @S)) AS first

This above query selects only first value, but how can 4th and fifth value can be extracted from string?上述查询仅选择第一个值,但如何从字符串中提取第 4 个和第 5 个值? like喜欢

12 as first,
43 as second,
5765 as third,
234 as fourth,
56 as fifth,
24 as sixth and so on..

If you have a maximum (or known) number of positions, and DON'T want to go dynamic如果你有一个最大的(或已知的)位置的数目,并且不想去动态

Example例子

DECLARE @S VARCHAR(MAX);
SET @S='12-43-5765-234-56-24-6456-234-678-9-0-0'


Select Pos1 = xDim.value('/x[1]','varchar(max)') --could change to desired datatype (int ?)
      ,Pos2 = xDim.value('/x[2]','varchar(max)') 
      ,Pos3 = xDim.value('/x[3]','varchar(max)')
      ,Pos4 = xDim.value('/x[4]','varchar(max)')
      ,Pos5 = xDim.value('/x[5]','varchar(max)')
      ,Pos6 = xDim.value('/x[6]','varchar(max)')
      ,Pos7 = xDim.value('/x[7]','varchar(max)')
      ,Pos8 = xDim.value('/x[8]','varchar(max)')
      ,Pos9 = xDim.value('/x[9]','varchar(max)')
      ,Pos10= xDim.value('/x[10]','varchar(max)')
      ,Pos11= xDim.value('/x[11]','varchar(max)')
      ,Pos12= xDim.value('/x[12]','varchar(max)')
      ,Pos13= xDim.value('/x[13]','varchar(max)')
 From  (Select Cast('<x>' + replace(@S,'-','</x><x>')+'</x>' as xml) as xDim) as A 

Returns退货

在此处输入图片说明

暂无
暂无

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

相关问题 在SQL中,我需要生成一个排名(第1,第2,第3)列,并停留在“联系”上 - In SQL, I need to generate a ranking (1st, 2nd, 3rd) column, getting stuck on “ties” 如何连接两个SQL表,将第二个表中的值放入第一个表中的列? - How can I join two SQL tables, putting a value from the 2nd table into a column in the 1st? SQL:如何从表中选择第 1、3、11 和第 n 行? - SQL: How to select 1st, 3rd, 11th and nth row from a table? SQL Server挑战性问题-寻找第一买家,第二/第三买家,第四或更多买家 - SQL Server Challenging questions - look for 1st buyer, 2nd/3rd buyers, 4th or more buyers 我如何 Select 不同列中的第一个、第二个和第三个值 - 访问女士 - How can I Select 1st, 2nd and 3rd values in different columns - Ms Access SQL-在列表中为第1次,第2次,第3次创建计数 - SQL - Creating counts for 1st, 2nd, 3rd occasion in a list 如何使用SQL从格式化的字母数字字符串中提取第一和第二个数字? - How to extract 1st and 2nd number from a formatted alphanumeric string using SQL? Mysql - 如何根据第一个表的 2 列的值显示第二个表中的列? - Mysql - How to display column from 2nd table based on values from 2 columns of 1st table? 在 sql 中查找运行总计 - 如果第一列没有值,则从第二列计算 - Find Running Total In sql - if 1st column has no value then calculate from 2nd column SQL:从第二个和第三个破折号 (-) 之间提取一个字符串,如果它们存在的话。 其他不适用 - SQL: Extract a string from between the 2nd and 3rd dashes (-), if they exist. Else N/A
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM