如何将一个列值int拆分为三列? [英] How to split one column value int to the three columns?

查看:72
本文介绍了如何将一个列值int拆分为三列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个表和一列,我想将一个列值拆分为三列(逗号分隔值)

我有一个表格数据。

插入adddetails值(''abc,123213,Delhi'')

我正在为分割值编写查询但是如何分成三列。

我的查询是: -

 选择 SUBSTRING (地址, 1 ,CHARINDEX(' ,',address)-1) as  Address1,
SUBSTRING(地址,CHARINDEX(' ,',address)+ 1,LEN(地址)) as Address2
来自 adddetails





它是两列的查询,但我需要三列如



地址1地址2地址3

abc 123213德里



请帮帮我。



Ankit Agarwal

软件工程师

解决方案

容易!! [ ^ ]谷歌是你的朋友,只要你被卡住就会使用他!



祝你好运,

OI

Hello Ankit,



非常简单:)



你''我设法找到CHARINDEX的第一个逗号,

你的问题是找到其他人(在你的情况下是第二个)。



你可以做的是在第一次出现后开始搜索起始索引。



参见位于sqlandme的Vishal'帖子


  DECLARE   @ string   VARCHAR  128 
SET @ string = ' CHARINDEX搜索' +
' 首次出现指定字符/字符串'
SELECT CHARINDEX(' Char' @ string AS ' 位置'
CHARINDEX(' Char',< span class =code-sdkkeyword> @ string
2 AS ' 职位'



结果集:



职位空缺

---- ----

1 - 75



(1行受影响)



在上面的例子中,第一列返回1作为字符串以''CHAR ...''开头,而在第二列中搜索是从''HARINDEX ...''开始的,导致返回第二次''CHAR''。



搜索基于数据的整理。由于我当前的排序规则设置为Latin1_General_CI_AI,因此CHARINDEX()执行了不区分大小写的搜索。要在此处执行区分大小写的搜索,我们可以将Latin1_General_CS_AI排序规则应用于@string:



  DECLARE   @ string   VARCHAR  128 
SET @ string = ' CHARINDEX搜索' +
' 第一次出现指定的字符/字符串'
SELECT CHARINDEX(' char' @ string COLLATE Latin1_General_CS_AI)
AS ' 职位



结果集:



职位

----

75


Hello,

I have one table and a column, i want to split one column value into three column (comma separated value)
I have a table data.
insert into adddetails values(''abc,123213,Delhi'')
I am writing a query for split values but how to break into three column.
my query is:-

select SUBSTRING(address,1,CHARINDEX(',',address)-1) as Address1,
SUBSTRING(address,CHARINDEX(',',address)+1,LEN(address)) as Address2
from adddetails



It''s query of two column but i need three columns like

Address1 Address2 Address3
abc 123213 Delhi

Please help me.

Ankit Agarwal
Software Engineer

解决方案

Easy!![^] Google is your friend use him whenever you are stuck!

Good luck,
OI


Hello Ankit,

Quite simple :)

You''ve managed to find the first comma by the CHARINDEX,
and your problem is to find the others (in your case the second).

What you can do is start the search for a starting index, after the first appearance.

See this section of Vishal''s post on sqlandme:

DECLARE @string VARCHAR(128)
SET @string =     'CHARINDEX searches for the string for the ' +
                  'first occurrence of a specified character/string'
SELECT      CHARINDEX('Char', @string)    AS 'Position',
            CHARINDEX('Char', @string, 2) AS 'Position'


Result Set:

Position Position
———– ———–
1 - 75

(1 row(s) affected)

In the above example, the first columns returns 1 as the string started with ''CHAR…'', while in the second column the search was started from ''HARINDEX…'', which resulted in returning the second occurrence of ''CHAR''.

Searches are based on the collation of the data. CHARINDEX() has performed a case-insensitive search as my current collation is set to Latin1_General_CI_AI. To perform a case-sensitive search here we can apply Latin1_General_CS_AI collation to @string:

DECLARE @string VARCHAR(128)
SET @string =     'CHARINDEX searches for the string for the ' +
                  'first occurrence of a specified character/string'
SELECT      CHARINDEX('char', @string COLLATE Latin1_General_CS_AI)
            AS 'Position'


Result Set:

Position
———–
75


这篇关于如何将一个列值int拆分为三列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆