Finding string in given set of strings bt comma separated - sql-server

I need to get third sub string from a string below are the samples, using SQL Server 2008.
string1:-
('C20080703115333MCTTtettett','24','6101349328','Bundled Standard','4','2.00','Testing Insert for New SP',','PD2013021002584832540')
desired result:- 6101349328
string2:-
('C20080703115333MCTTetew','24','7101349328','Bundled Standard','4','2.00','Testing Insert for New SP',','PD2013021002584832540')
desired result:- 7101349328
string3:-
('C20080703115333MCTTteetew','24tt','8101349328','Bundled Standard','4','2.00','Testing Insert for New SP',','PD2013021002584832540')
desired result:- 8101349328
string4:-
('C20080703','24','111101349328','Bundled Standard','4','2.00','Testing Insert for New SP',','PD2013021002584832540')
desired result:- 111101349328
Thanks In advance.

Try something like this:
select val = substring
(
str
, charindex(',', str, charindex(',',str, 0) + 1) + 2
, charindex(',', str, charindex(',', str, charindex(',',str, 0) + 1) + 1)
- (charindex(',', str, charindex(',',str, 0) + 1) + 3)
)
from strings
Which gives:
6101349328
7101349328
8101349328
111101349328
SQL Fiddle example.

Related

Extract everything in between & -- & from a column in a table

I have the value for the column in a table and i need to exact some part of the string
baf93b64-c255-4dda-b9dc-3f7438b49335-mkttrg&utm_source=bing&utm_medium=cpc&utm_campaign=MO+-+Payday&utm_term=payday+loan&utm_content=Payday+Loans+(Phrase)
now I have to extract from the first & i.e.,
utm-source = bing
utm-medium = cpc
utc_campaign = MO+Payday
utm_term = 'payday+loan'
utm_content = Payday+loans+(Phrase).
can you please help me with the sub string function to extract these parts from the column mentioned value.
Thanks in advance.
Use substring with the start at the charindex, and number of character extract is the length of the string subtract the length of the two pieces from the beginning and the end. You will need to do some data validation so that the col is of the form you want.
substring(col, charindex('&', col) + 1, len(col) - charindex('&', reverse(col)) - charindex('&', col))
Test code:
DECLARE #col nvarchar(100) = '12&12545643&euwpo';
SELECT substring(#col, charindex('&', #col) + 1, len(#col) - charindex('&', reverse(#col)) - charindex('&', #col))
The main approach of solving this issue via using SplitString function.
Then depends on your comment:
in the string what i have mentioned in my question, it is not picking
the last part "utm_content=Payday+Loans+(Phrase)" as it doesnt have
the & at the end i guess!!
Use the following Code:-
SELECT top ((select count (*) FROM dbo.SplitString('baf93b64-c255-4dda-b9dc-3f7438b49335-mkttrg&utm_source=bing&utm_medium=cpc&utm_campaign=MO+-+Payday&utm_term=payday+loan&utm_content=Payday+Loans+(Phrase)', '&')
where item like '%=%' ) -1) item
FROM dbo.SplitString('baf93b64-c255-4dda-b9dc-3f7438b49335-mkttrg&utm_source=bing&utm_medium=cpc&utm_campaign=MO+-+Payday&utm_term=payday+loan&utm_content=Payday+Loans+(Phrase)', '&')
where item like '%=%'
Result:-

SQL how to check prefix in cell with two prefixes

I am learning SQL and I have a table where there are certain cells with two prefixes like this :
example1(cell) : R:8days; U:5$;
example2(cell) : R:8days;
example3(cell) : U:5$;
I want to check for that U:5$ after the first prefix, as I know how to check for prefix R:8days;. So I need to check for U:5$ and then make a new column in table.
My code looks like this:
;with cte as (
select
Employer, AmountPayd, AmountPayd as Payd
from data
where TipeOfTransaction like 'Offline Prepaid%' AND Note like '%R:8%' **HERE I WANT TO CHECK FOR PREFIX NR2. 'U:5$' AND MAKE NEW COLUMN FOR WHICH EMPLOYER HAS U:5$ NOTE.**
)
select
Employer,
[4.00] = ISNULL([4.00],0)
,[5.00] = ISNULL([5.00],0)
,[9.00] = ISNULL([9.00],0)
,[10.00] = ISNULL([10.00],0)
,[15.00] = ISNULL([15.00],0)
,[Sum] =ISNULL([4.00],0) + ISNULL([5.00],0) + ISNULL([9.00],0) + ISNULL([10.00],0) + ISNULL([15.00],0)
from cte
pivot (
sum(AmountPayd) for Payd in ([4.00],[5.00],[9.00], [10.00], [15.00], [20.00]))pvt;
This?
select
Employer, AmountPayd, AmountPayd as Payd,
CASE WHEN Note like '%R:8%;%U:5$%' THEN 'U:5' END U5Note
from data
where TipeOfTransaction like 'Offline Prepaid%' AND Note like '%R:8%'
(I've corrected some mistakes in column names and refactored the query.)
I believe, you are looking for something like this:
WITH CTE AS (
SELECT
Employer,
AmountPaid,
Paid
FROM Data
LEFT JOIN (VALUES (4, 5, 9, 10, 15, 20)) V(Paid)
ON Note LIKE '%U:' + CAST(Paid AS VARCHAR(10)) + '$;%'
WHERE TypeOfTransaction LIKE 'Offline Prepaid%'
AND Note LIKE '%R:8%'
)
SELECT
Employer,
ISNULL([4.00], 0) AS [4.00],
ISNULL([5.00], 0) AS [5.00],
ISNULL([9.00], 0) AS [9.00],
ISNULL([10.00], 0) AS [10.00],
ISNULL([15.00], 0) AS [15.00],
ISNULL([4.00] + [5.00] + [9.00] + [10.00] + [15.00], 0) AS Sum
FROM CTE
PIVOT( SUM(AmountPaid)
FOR Paid IN ([4.00],[5.00],[9.00], [10.00], [15.00], [20.00])) PVT;

SQL Server : get part of string between 'ServicePassword=' and '&' in a url string

I'm trying to get the service password in a url stored in a table.
Example:
Parameters=http://google.com?ServiceUN=testUN&ServicePW=testPW&RequestType=testrequestType&MemberCode=TestMemberCode
This is where I'm at so far, the 9 in the substring function is only to use as a test length, that's the part I'm having issues with getting.
SELECT
SUBSTRING(Parameters,
CHARINDEX('ServicePW=', Parameters) + LEN('ServicePW='), 9)
FROM Table
WHERE TestID = 8
SELECT
substring(
Parameters,
charindex('ServicePW=', Parameters) + 10, -- len('ServicePW=')
charindex('&', substring(Parameters, charindex('ServicePW=', Parameters), len(Parameters))) - 11 -- (- len('ServicePW=') - 1)
)
FROM
Table
WHERE
TestID = 8
SELECT SUBSTRING(SUBSTRING(Parameters, PATINDEX('%ServicePW=%', Parameters), LEN(Parameters)), 11, PATINDEX('%&%', SUBSTRING(Parameters, PATINDEX('%ServicePW=%', Parameters), LEN(Parameters))) - 11)
FROM Table where TestID = 8

How to cut substring between known characters in SQL Server

How can I cut substring between two known character sequences in SQL Server?
For example: This is my string (column in a table)
'DateTimeFormat=dd.MM.yyyy&ReportDate_FromDate=08/11/2014 00:00:00&ReportDate_ToDate=08/12/2014 23:59:00&Reports_Brand:isnull=true&Reports_Portal:isnull=true&Reports_Currency:isnull=true&ReportBy=WEEK&OrderBy:isnull=true&IncludeDataForLastHour:isnull=true&ServerName:isnull=true&User=pirman1&Reports_Export=False&Internal'
and I want to see only pirman1 which is between &User= and the following &.
I've tested this code, with a slight alteration to Mureinik's code, this one works fine.
SELECT REPLACE(REPLACE(SUBSTRING(string, start_pos, end_pos - start_pos
+1), '&User=', ''), '&', '')
FROM (SELECT string,
CHARINDEX('&User=', string) AS start_pos,
CHARINDEX('&', string, CHARINDEX('&User=', string)+1) AS end_pos
FROM dbo.TestTbl
) AS abc
A combination of substring and charindex should do the trick:
SELECT SUBSTR(str_col, start_pos, end_pos - start_pos + 1)
FROM (SELECT str_col,
CHARINDEX('&User=', str_col) AS start_pos,
CHARINDEX('&', str_col, CHARINDEX('&User=', str_col) + 1) AS end_pos
FROM my_table) t

SQL query to extract extension from middle of URL

How to write a single SQL query for extracting file extension from URLs if I have URL is like this :
1) www.xyz.com\hello\world\fileA.txt?ID=01
2) www.xyz.com\hello\world\fileA.txt
Please help if anybody knows it
If you are using MS T-SQL:
1) select SUBSTRING (URL, CHARINDEX('?', URL) - 3, 3)
2) select RIGHT(URL, 3)
so you could combine them as follows:
Select Case When CHARINDEX('?', URL) <> 0 Then SUBSTRING (URL, CHARINDEX('?', URL) - 3, 3)
ELSE RIGHT(URL, 3)
END as Extension
CharIndex
PatIndex
SELECT
substring('www.xyz.com\hello\world\fileA.txt?ID=01',38,10)
Will return you 10 characters which are after "ID="
Read more about substring here: http://technet.microsoft.com/en-US/eng-eng/library/ms187748.aspx
SELECT
CASE
WHEN len(URL) = 33 THEN right(URL,3)
ELSE SUBSTRING(URL,31,3)
END
FROM #temp1

Resources