split string in sql server 2008 - sql

In my table, I have Experience field like
Experience
0to0Years
2to0Years
7to12Years
Here I want to split into
Yearfrom - 0
Yearto- 0
How to split strings here. I search so many articles. I can't find the correct solution. Is there any way to do here?

Here is a working solution
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: http://www.sqlfiddle.com/#!3/d41d8/12483
For working on your column replace '0to0Years' with column name
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>

Try with following Steps...
--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

Please try:
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

Try this using Parsename function
Select parsename(replace(Experience,'to','.'),2) ,
substring(parsename(replace(Experience,'to','.'),1),0,
charindex('Y',parsename(replace(Experience,'to','.'),1)))
from YourTable
Demo in SQLFiddle

Related

SQL select the last numbers from the right

I am trying to select only the numbers from a string, starting from the right. So from the following examples:
1ECCA15
ECCB9
I would only like to select the numbers from the right. So the result would be:
15
9
I've tried this, but this does not select the numbers from the right.
substring(col, PatIndex('%[0-9]%', col), len(col))
Try this:
DECLARE #t TABLE(Col VARCHAR(255));
INSERT #t SELECT '1ECCA15';
INSERT #t SELECT 'ECCB9';
SELECT REVERSE(LEFT(REVERSE(col), PATINDEX('%[a-z]%', REVERSE(col)) - 1)) FROM #t;
There you go, make use of REVERSE
CREATE TABLE #temp(col varchar(20))
INSERT INTO #temp values('1ECCA15'),('ECCB9')
SELECT REVERSE(SUBSTRING(REVERSE(Col), 0, PATINDEX('%[^0-9]%',REVERSE(col))))
FROM #temp
How about this?
select right(col, patindex('%[^0-9]%',reverse(col))-1) from your_table;

SQL Server - Get substring form a string

I have a string coming like '1234_XXXX_RHL_PQR' and in output I want to pull character coming after 'XXXX_' that is 'RHL'.
I would always have 'XXXX' in string and my task is to get string after 'XXXX_'
Try This
DECLARE #Table AS TABLE (Data Nvarchar(100))
INSERT INTO #Table
SELECT '1234_XXXX_RHL_PQR'
SELECT Data,CAST('<S>'+REPLACE(Data,'_','</S><S>')+'</S>' AS XML).value('/S[3]','nvarchar(1000)') AS ReqData
FROM #Table
Result
Data ReqData
----------------------------
1234_XXXX_RHL_PQR RHL
DEMO : http://rextester.com/PXHSDZ80426
Try the following:
select
right(yourfield,len(yourfield)-3-patindex('%XXXX%',yourfield))
from yourtable
I would use substring() with charindex() function
select col, substring(col, charindex('XXXX_', col)+5, len(col)) as Ncol
from table t;
First Create Table-Value-Function
CREATE FUNCTION [dbo].[Udf_GetReqString](#IputData Varchar(200))
RETURNS #OutTable TABLE
(
ActualData varchar(200),
ReqData varchar(200)
)
AS
BEGIN
DECLARE #Table AS TABLE (Data Nvarchar(100))
INSERT INTO #Table
SELECT #IputData
INSERT INTO #OutTable
SELECT Data,SUBSTRING(DatReq,-1,CHARINDEX('_',Data )) AS DatReq
FROM
(
SELECT Data,SUBSTRING(Data,CHARINDEX('X_',Data )+2,LEN(Data) ) AS DatReq
FROM #Table
)dt
RETURN
END
Sample And Call Function
DECLARE #Table AS TABLE (Data Nvarchar(100))
INSERT INTO #Table
SELECT '1234_XXXX_SHL_PQR' UNION ALL
SELECT '1234_XXXX_RHL_PQR' UNION ALL
SELECT '1234_DDHDFD_XXXX_PHL_PQR' UNION ALL
SELECT '1234_ABAD_DADADA_XXXX_GHL_PQR'
SELECT t.Data,
udf.ReqData
FROM #Table t
CROSS APPLY [dbo].[Udf_GetReqString] (t.Data) As udf
Result
Data ReqData
-------------------------------------------
1234_XXXX_SHL_PQR SHL
1234_XXXX_RHL_PQR RHL
1234_DDHDFD_XXXX_PHL_PQR PHL
1234_ABAD_DADADA_XXXX_GHL_PQR GHL
DEMO
http://sqlfiddle.com/#!18/2bcfa/1

Get a specific string

It's my data and every ThroughRouteSid record has the same pattern.
six number and five comma. then I just want to get three and five
number into two record to template Table and get the same Count()
value to these two record.
For example: First record in the picture.
ThroughRouteSid(3730,2428,2428,3935,3935,3938,) Count(32).
I want a result like this:
2428 32 3935 32
I get What number I want.become two record and both have same Count value into template table
you can use XML to get your result, please refer below sample code -
create table #t1( ThroughRouteSid varchar(500) , Cnt int)
insert into #t1
select '3730,2428,2428,3935,3935,3938,' , len('3730,2428,2428,3935,3935,3938,')
union all select '1111,2222,3333,4444,5555,6666,' , len('1111,2222,3333,4444,5555,6666,')
select cast( '<xml><td>' + REPLACE( SUBSTRING(ThroughRouteSid ,1 , len(ThroughRouteSid)-1),',','</td><td>') + '</td></xml>' as xml) XmlData , Cnt
into #t2 from #t1
select XmlData.value('(xml/td)[3]' ,'int' ), Cnt ,XmlData.value('(xml/td)[5]' ,'int' ), Cnt
from #t2
First create the function referring How to Split a string by delimited char in SQL Server. Then try Querying the following
select (SELECT CONVERT(varchar,splitdata) + ' '+ Convert(varchar, [Count])+' ' FROM (select splitdata, ROW_NUMBER() over (ORDER BY (SELECT 100)) row_no
from [dbo].[fnSplitString](ThroughRouteSid,',')
where splitdata != '') as temp where row_no in (2,5)
for xml path('')) as col1 from [yourtable]
If you are using SQL Server 2016 you can do something like this:
create table #temp (ThroughRouteSid varchar(1024),[Count] int)
insert into #temp values
('3730,2428,2428,3935,3935,3938,',32),
('730,428,428,335,935,938,',28)
select
spt.value,
t.[Count]
from #temp t
cross apply (
select value from STRING_SPLIT(t.ThroughRouteSid,',') where LEN(value) > 0
)spt

how to substring of a string in SQL

My data will be like that
IPDocument/XR/test_20120409_clearGAC.txt
IPDocument/XR/test_20120409_clearGAC_05.txt
IPDocument/XR/test_20120409_clearGAC_01.txt
I would like to extract clearGAC.txt from above.
How can I use substring in SQL ?
I assume that you want the _05 and _01 - the question is not clear.
DECLARE #var VARCHAR(250)
DECLARE #table TABLE ( val VARCHAR(250) )
INSERT INTO #table
SELECT 'IPDocument/XR/test_20120409_clearGAC_01.txt'
UNION ALL
SELECT 'IPDocument/XR/test_20120409_clearGAC_05.txt'
UNION ALL
SELECT 'IPDocument/XR/test_20120409_clearGAC.txt'
SELECT SUBSTRING(val, CHARINDEX('clearGAC', val), 50),
SUBSTRING(SUBSTRING(val, CHARINDEX('_', val)+1, 50), CHARINDEX('_', SUBSTRING(val, CHARINDEX('_', val)+1, 50))+1, 50)
FROM #table
Returns this:
clearGAC_01.txt
clearGAC_05.txt
clearGAC.txt
The third parameter in the CHARDINDEX can be larger than the length.
EDIT: I added logic if you don't know if clearGAC is the name
After reading your comment, you may just want this:
I am going to pass ClearGAC.txt and see if it's in the DB. this
ClearGAC.txt may be named like above codes.
DECLARE #var VARCHAR(250)
DECLARE #table TABLE ( val VARCHAR(250) )
INSERT INTO #table
SELECT 'IPDocument/XR/test_20120409_clearGAC_01.txt'
UNION ALL
SELECT 'IPDocument/XR/test_20120409_clearGAC_05.txt'
UNION ALL
SELECT 'IPDocument/XR/test_20120409_clearGAC.txt'
SELECT *
FROM #table
WHERE val LIKE '%clearGAC%.txt'
SELECT SUBSTRING('IPDocument/XR/test_20120409_clearGAC.txt', CHARINDEX('09_','IPDocument/XR/test_20120409_clearGAC.txt')+3, 100)
result :
clearGAC.txt

How to Split string value in sqlserver

I have a following string
90-PMR-450
90-PMRA-340
I want to get part 3 of string. example 450 or 340.
plese help me. thanks
declare #T table
(
Value varchar(15)
)
insert into #T values
('90-PMR-450'),
('90-PMRA-340')
select stuff(Value, 1, 1+len(Value)-charindex('-', reverse(Value)), '')
from #t
DECLARE #x TABLE(v VARCHAR(32));
INSERT #x SELECT '90-PMR-450'
UNION ALL SELECT '90-PMRA-340';
SELECT Part3 = PARSENAME(REPLACE(v, '-', '.'), 1) FROM #x;
i think you will find this user defined function to split the string helpful:
http://www.codeproject.com/Articles/7938/SQL-User-Defined-Function-to-Parse-a-Delimited-Str