Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql server 2008中的拆分字符串_Sql_Sql Server_Sql Server 2008_Split - Fatal编程技术网

sql server 2008中的拆分字符串

sql server 2008中的拆分字符串,sql,sql-server,sql-server-2008,split,Sql,Sql Server,Sql Server 2008,Split,在我的表格中,我有类似于这个领域的经验 体验 0到0年 20到00年 7至12年 在这里,我想分成两部分 年-0 年至-0 如何在这里分割字符串。我搜索了这么多的文章。我找不到正确的解决办法。这里有什么办法吗?这里有一个可行的解决方案 declare @yearfrom varchar(2),@yearto varchar(2) select @yearfrom=substring('0to0Years',0,patindex('%to%','0to0Years')), @yeart

在我的表格中,我有类似于这个领域的经验

体验

  • 0到0年
  • 20到00年
  • 7至12年
在这里,我想分成两部分

  • 年-0
  • 年至-0

如何在这里分割字符串。我搜索了这么多的文章。我找不到正确的解决办法。这里有什么办法吗?

这里有一个可行的解决方案

declare @yearfrom varchar(2),@yearto varchar(2)

select @yearfrom=substring('0to0Years',0,patindex('%to%','0to0Years')),
 @yearto=substring('0to0Years',patindex('%to%','0to0Years')+2,patindex('%Years%','0to0Years')-patindex('%to%','0to0Years')-2)
SqlFiddle:

若要处理列,请将“0到0年”替换为列名

    declare @yearfrom varchar(2),@yearto varchar(2)

        select @yearfrom=substring(col_name,0,patindex('%to%',col_name)),
         @yearto=substring(,patindex('%to%',col_name)+2,patindex('%Years%',col_name)-patindex('%to%',col_name)-2)
from table_name where <condition>
声明@yearfrom varchar(2),@yearto varchar(2)
选择@yearfrom=substring(列名称,0,patindex(“%to%”,列名称)),
@yearto=子字符串(,patindex(“%to%”,列名称)+2,patindex(“%Years%”,列名称)-patindex(“%to%”,列名称)-2)
从表_name where
尝试使用此函数


在演示中尝试以下步骤

                            --Create Table :
                            Create Table #Table
                            (
                            Name Varchar(50),
                            Experience Varchar(20)
                            )
                            Go

                            -- Insert Values :
                            Insert into #Table Values('Tom','0to0Years')
                            Insert into #Table Values('Victor','0to1Years')
                            Insert into #Table Values('Mark','11to12Years')
                            Go

                            --View Data
                            Select  * from #Table

                            --Using CharIndex  and Substring :
                            Select Name,
                            Substring(Experience,1,CharIndex('to',Experience)-1) as Yearfrom,
                            Substring(Experience,(CharIndex('to',Experience)+2),Len(Replace(Experience,'Years','')) - (CharIndex('to',Experience)+1)) as YearTo
                            from #Table
请尝试:

select 'YearFrom - '+substring(Data, 0, PatIndex('%[to]%', Data)) YearFrom, 
    'YearTo - '+replace(stuff(Data, 1, PatIndex('%[to]%', Data)+1, ''), 'Years', '') YearTo
from
(
    Select '0to0Years' Data union
    Select '2to0Years' Data union
    Select '756to12Years' Data
)x

如何将其保存在DB中或仅显示?我现在想使用select语句。所以它只是显示的意思是ok。然后我执行另一个表。ok,好的,等一会儿。您将必须使用子字符串函数,我也正在为这个查询处理它。ok。我使用子字符串(exp,start,end)你在那里发现一个问题了吗?执行此查询时,结果为12年。所以它将给出0 12y。如果我把那个地方搬走,意味着0到0年后就会有问题。“0”不会出现。这一个很好。但是当我使用select insert查询时,我遇到了一些问题,比如,select top(5)的insert jobs()值('+子字符串(经验,0,patindex('%to%',经验))*365*24*60*60,子字符串(经验,patindex('%to%',经验)+2,patindex('%Years%',Experience)-patindex('%to%',Experience)-2+)“从requirementsdetailsfororganization and getting error”将varchar值“insert jobs()值(”转换为数据类型int时,转换失败
select 'YearFrom - '+substring(Data, 0, PatIndex('%[to]%', Data)) YearFrom, 
    'YearTo - '+replace(stuff(Data, 1, PatIndex('%[to]%', Data)+1, ''), 'Years', '') YearTo
from
(
    Select '0to0Years' Data union
    Select '2to0Years' Data union
    Select '756to12Years' Data
)x