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
Related
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:-
I am not a SQL expert so please forgive me if this is SQL 101 :).
In a select statement there are 2 replace functions. They look for a Servername and it's admin share d$ by it's UNC path. Example '\SERVERNAME\d$'
It then replaces '\SERVERNAME\d$' with 'D:'.
Here is the query currently:
select Replace(p.Path,'\\SERVERNAME\d$','D:') as searchpath
,p.path as fullpath
,s.ShareName
,s.SharePath
,p.Member
,p.Access
From Paths As p
Left Outer Join Shares as s on
Replace(p.Path,'\\SERVERNAME\d$','D:') Like s.SharePath + '\%'
Up until now it has always been d$.
Today my needs have changed and I need the query to find ANY servername UNC path admin share regardless of share letter (c$, d$, e$, f$...etc) and replace it with it's respective drive letter (D:, E:, F:... etc).
My thought is replace function could find the $ and look one character to the left of it to get the proper share letter, then use that for the replace. The issue I have, not being a SQL professional, is that I know SQL can likley do what I need it to do...I just don't know how to get there. I've googled and found some examples, but haven't had any luck in getting them to work.
Any help would be greatly appreciated.
You can use a combination of STUFF, PATINDEX, LEN to get what you want.
Sample Query
DECLARE #ReplaceChar VARCHAR(100) = '[prefixcharacters]\\SERVERNAME\d$[postcharacter]'
DECLARE #SearchString VARCHAR(100) = '\\SERVERNAME\_$'
SELECT
STUFF(#ReplaceChar,PATINDEX('%' + #SearchString + '%',#ReplaceChar),LEN(#SearchString),
UPPER(SUBSTRING(#ReplaceChar,PATINDEX('%' + #SearchString + '%',#ReplaceChar) + LEN(#SearchString) - 2,1)) + ':') as searchpath
WHERE PATINDEX('%' + #SearchString + '%',#ReplaceChar) > 0
Output
[prefixcharacters]D:[postcharacter]
Alternate Query
You can shorten the query if you want to get the previous character before $ as per your title. Something like this
DECLARE #ReplaceChar VARCHAR(100) = '[prefixcharacters]\\SERVERNAME\d$[postcharacter]'
DECLARE #SearchString VARCHAR(100) = '\\SERVERNAME\_$'
SELECT
STUFF(#ReplaceChar,
PATINDEX('%'+#SearchString+'%',#ReplaceChar),
LEN(#SearchString),
UPPER(SUBSTRING(#ReplaceChar,CHARINDEX('$',#ReplaceChar) -1,1)) + ':')
WHERE PATINDEX('%'+#SearchString+'%',#ReplaceChar) > 0
In this query
STUFF replaces your pattern with with the character before $ + ':'
Start of pattern is identified by PATINDEX('%'+#SearchString+'%',#ReplaceChar)
D is identified by getting the charindex of '$' and then getting the previous character using SUBSTRING
What about ΒΈ
select Replace(SUBSTRING(p.path, 14, Len(#spath)-14),'$',':') as searchpath
,p.path as fullpath
,s.ShareName
,s.SharePath
,p.Member
,p.Access
From Paths As p
Left Outer Join Shares as s on
Replace(SUBSTRING(p.path, 14, Len(#spath)-14),'$',':') Like s.SharePath + '\%
select as searchpath
DECLARE #str nvarchar (100)
SET #str = '\\SERVERNAME\d$'
IF #str LIKE '\\SERVERNAME\_$'
SET #str = UPPER(SUBSTRING(#str, 14, 1)) + ':'
SELECT #str
Starting from previous, something like
select UPPER(SUBSTRING(p.path, 14, 1)) + ':' as searchpath
,p.path as fullpath
,s.ShareName
,s.SharePath
,p.Member
,p.Access
From Paths As p
Left Outer Join Shares as s on
SUBSTRING(p.path, 14, 1) + ':' Like s.SharePath + '\%'
I am no mysql expert either :)
Based on the logic you mentioned in the last part of the question, I have used concat and substring to get to the drive letter in the column.
Hope this helps
select replace(path, concat(substring(path, 1, locate('$', path) - 2), substring(path, locate('$', path) - 1, 1) , '$'), concat(substring(path, locate('$', path) - 1, 1) , ':')) as searchpath ...
The remaining part of the query would be the same.
I've a strings such as:
Games/Maps/MapsLevel1/Level 1.swf
Games/AnimalWorld/Animal1.1/Level 1.1.swf
Games/patterns and spatial understanding/Level 13.5/Level 13.5.swf
I want to get only file name without its extension(String After last Slash and before Last dot), i.e Level 1 and Level 1.1 and Level 13.5, Even I want to remove all the white spaces and the final string should be in lower case i.e the final output should be
level1
level1.1
level13.5 and so on..
I tried following query but i got Level 1.swf, How do i change this Query?
SELECT SUBSTRING(vchServerPath, LEN(vchServerPath) - CHARINDEX('/', REVERSE(vchServerPath)) + 2, LEN(vchServerPath)) FROM Games
SELECT (left((Path), LEN(Path) - charindex('.', reverse(Path))))
FROM
(
SELECT SUBSTRING(vchServerPath,
LEN(vchServerPath) - CHARINDEX('/', REVERSE(vchServerPath)) + 2,
LEN(vchServerPath)) Path
FROM Games
) A
This would work, I kept your inner substring which got you part way and I added the stripping of the dot.
I have included a sql fiddle link for you to see it in action sql fiddle
Edited:
Following will remove the white space and returns lower case...
SELECT REPLACE(LOWER((left((Path), LEN(Path) - charindex('.', reverse(Path))))), ' ', '')
FROM
(
SELECT SUBSTRING(vchServerPath,
LEN(vchServerPath) - CHARINDEX('/', REVERSE(vchServerPath)) + 2,
LEN(vchServerPath)) Path
FROM Games
) A
Try this:
select
case
when vchServerPath is not null
then reverse(replace(substring(reverse(vchServerPath),charindex('.',reverse(vchServerPath))+1, charindex('/',reverse(vchServerPath))-(charindex('.',reverse(vchServerPath))+1)),' ',''))
else ''
end
This should work fine; with extension removed.
select
REVERSE(
SUBSTRING(
reverse('Games/patterns and spatial understanding/Level 13.5/Level 13.5.swf'),
5,
(charindex('/',
reverse('Games/patterns and spatial understanding/Level 13.5/Level 13.5.swf')) - 5)
))
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.
Inside a varchar2 column I have text values like :
aaaaaa. fgdfg.
bbbbbbbbbbbbbb ccccccccc
dddddd ddd dddddddddddd,
asdasdasdll
sssss
if i do select column from table where id=... i get the whole text in a single row, normally.
But i would like to get the result in multiple rows, 5 for the example above.
I have to use just one select statement, and the delimiters will be new line or carriage return (chr(10), chr(13) in oracle)
Thank you!
Like this, maybe (but it all depends on the version of oracle you are using):
WITH yourtable AS (SELECT REPLACE('aaaaaa. fgdfg.' ||chr(10)||
'bbbbbbbbbbbbbb ccccccccc ' ||chr(13)||
'dddddd ddd dddddddddddd,' ||chr(10)||
'asdasdasdll ' ||chr(13)||
'sssss '||chr(10),chr(13),chr(10)) AS astr FROM DUAL)
SELECT REGEXP_SUBSTR ( astr, '[^' ||chr(10)||']+', 1, LEVEL) data FROM yourtable
CONNECT BY LEVEL <= LENGTH(astr) - LENGTH(REPLACE(astr, chr(10))) + 1
see: Comma Separated values in Oracle
The answer by Kevin Burton contains a bug if your data contains empty lines.
The adaptation below, based on the solution invented here, works. Check that post for an explanation on the issue and the solution.
WITH yourtable AS (SELECT REPLACE('aaaaaa. fgdfg.' ||chr(10)||
'bbbbbbbbbbbbbb ccccccccc ' ||chr(13)||
chr(13)||
'dddddd ddd dddddddddddd,' ||chr(10)||
'asdasdasdll ' ||chr(13)||
'sssss '||chr(10),chr(13),chr(10)) AS astr FROM DUAL)
SELECT REGEXP_SUBSTR ( astr, '([^' ||chr(10)||']*)('||chr(10)||'|$)', 1, LEVEL, null, 1) data FROM yourtable
CONNECT BY LEVEL <= LENGTH(astr) - LENGTH(REPLACE(astr, chr(10))) + 1;