I have string value which contains a long text. And there is last name of person write like this:
'S M I T H'.
So I have a long text in one row:
text text text 'S M I T H' John text text.
I find all these rows by this code:
Select *
from [dbo].[my_table]
where [TEXT] like ('% [A-Z][ ][A-Z][ ][A-Z][ ][A-Z]%')
But I don't know, how to remove the space between letters in the last name, so I want this:
text text text 'SMITH' John text text.
``
Use LEFT, RIGHT, SUBSTRING, REPLACE and CHARINDEX to do so...
An example :
WITH
T AS (SELECT 'text text text ''S M I T H'' John text text.' AS LONG_TEXT)
SELECT LEFT(LONG_TEXT, CHARINDEX('''', LONG_TEXT) - 1) +
REPLACE(SUBSTRING(LONG_TEXT, CHARINDEX('''', LONG_TEXT) , CHARINDEX('''', LONG_TEXT, CHARINDEX('''', LONG_TEXT) + 1) - CHARINDEX('''', LONG_TEXT) + 1), ' ', '') +
RIGHT(LONG_TEXT, CHARINDEX('''', LONG_TEXT))
FROM T;
Related
I have multiple strings:
address_1 = '226'
address_2 = 'Virginia Ave'
address_city = 'Trenton'
address_state = 'NJ'
address_country = 'US'
address_postal_code = '08610'
I am trying to combine them into a single string, separated from one another with comma, as follows:
"226 Virginia Ave, Trenton, NJ, 08610, US"
How can I combine my variables in a string, separate them using a comma, and in case any of the variable is missing, then do not display the extra comma?
I did this:
(address_1.to_s + ' ' + address_2.to_s + ' ' + address_city.to_s + ' ' + address_state.to_s + ' ' + address_postal_code.to_s + ' ' + address_country.to_s).squish
This gives output like this:
"226 Virginia Ave Trenton NJ 08610 US"
This happens because I am adding space + ' ' +. If I do + ', ' +, in case any of the address_ is nil or empty, it still displays the extra ,, and the address ends up looking like this:
"226 Virginia Ave, , , 08610, US"
address_1 = '226'
address_2 = 'Virginia Ave'
address_city = 'Trenton'
address_state = 'NJ'
address_country = 'US'
address_postal_code = '08610'
["#{address_1} #{address_2}", address_city, address_state, address_country, address_postal_code].map{|line| line.to_s.strip}.select{|line| !line.empty?}.join(', ')
This puts all the address elements in an array, maps to change all lines to a stripped string, and then selects lines that are not empty, and then joins using a comma and space.
The first element is address_1 and address 2 combined so as to avoid a comma after house number.
Try this using array compact. This may solve your problem.
address_arr = [address_1, address_2, address_city, address_state, address_country, address_postal_code]
no_empty_address = address_arr.reject{ |c| c.empty? }
no_empty_address.compact.join(', ')
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:-
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
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)
))
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;