how to substring of a string in SQL - 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

Related

Select first element of a comma-separated list

I have a field column that contains values like
4
12,3
8,5,6,7
I'm going to write a SELECT statement, whose result would be:
4
12
8
How can I do it?
Try this
I'm hoping you are looking for this kind of solution.
DECLARE #table TABLE
(
ID INT IDENTITY(1,1),
Value NVARCHAR(MAX)
)
INSERT INTO #table VALUES('4'),('12,3'),('8,5,6,7')
select *,
IIF(CHARINDEX(',', [Value]) = 0, [Value],SUBSTRING([Value],1,(CHARINDEX(',',[Value])-1))) AS [OutPut]
from #table
Output
Try this
For SQL Server:
select if(charindex(',', numbers) = 0, numbers, LEFT(numbers, charindex(',', numbers) - 1))
from table;
For MySQL:
select if(instr(numbers, ',') = 0, numbers, LEFT(numbers, instr(numbers, ',') - 1))
from table;
Try this for SQL server
DECLARE #CSV_Split TABLE
(
Id INT IDENTITY(1,1),
CSVData NVARCHAR(MAX)
)
INSERT INTO #CSV_Split VALUES('4'),('12,3'),('8,5,6,7')
select *,LEFT(CSVData, CHARINDEX(',', CSVData + ',') - 1)
from #CSV_Split
The output is something like this:

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

split string in sql server 2008

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

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