Split string and replace - sql-server-2005

I have a varchar column with Url's with data that looks like this:
http://google.mews.......http://www.somesite.com
I want to get rid of the first http and keep the second one so the row above would result in:
http://www.somesite.com
I've tried using split() but can't get it to work
Thanks

If you are trying to do this using T-SQL, you can try something in the lines of:
-- assume #v is the variable holding the URL
SELECT SUBSTRING(#v, PATINDEX('%_http://%', #v) + 1, LEN(#v))
This will return the start position of the first http:// that has before it at least one character (hence the '%_' before it and the + 1 offset).

If the first URL always starts right from the beginning of the string, you can use SUBSTRING() & CHARINDEX():
SELECT SUBSTRING(column, CHARINDEX('http://', column, 2), LEN(column))
FROM table
CHARINDEX simply searches a string for a substring and returns the substring's starting position within the string. Its third argument is optional and, if set, specifies the search starting position, in this case it's 2 so it didn't hit the first http://.

Related

Getting everything after last '/'

I have a path like this in my TBL_Documents table:
Uploads/Documents/6093/12/695-Graco-SW_5-15-19.pdf
I need to compare it to a file being uploaded now that will look like this:
695-Graco-SW_5-15-19.pdf
I want to compare the path in my table with the uploaded file name. I tried using substring() on the first right / but I don't really get how substring is really working. For example, I tried to do this:
select substring(right(path,1),1,1) as path from TBL_DOCUMENT
but it is only giving me the very first character from the right. I expected to see everything after the last / character.
How can I do this?
I would use an approach of finding how many characters you need to use from the right. I would do this by first reversing the string and then searching for the '/'. This will tell you how many characters from the right this '/' is. I would then use this in the RIGHT function:
SQL Fiddle
MS SQL Server 2017 Schema Setup:
Query 1:
DECLARE #documentName varchar(100) = 'Uploads/Documents/6093/12/695-Graco-SW_5-15-19.pdf'
SELECT RIGHT(#documentName, CHARINDEX('/',REVERSE(#documentName))-1)
Results:
| |
|--------------------------|
| 695-Graco-SW_5-15-19.pdf |
RIGHT(path,1) means you want [1] character from the right of the path string, or 'f'.
You then wrap 'f' in a substring, asking for [1] character starting at the [1]st position of the string. Since the expression passed to substring returns 'f', your substring also returns 'f'.
You want to use a combination of charindex and reverse to handle this appropriately.
SUBSTRING(path,len(path) - charindex('/',reverse(path))). That will not parse but it should get you on the right track.
In normal speak, this returns the string, starting with the right most '/' of the path, to the end of string.

How to extract a number part of a field using regex_substr function?

I need to extract the numerical part of values in a column (varchar) if there exists a number in the value.
ColumnA has values like ABC, M365, J344, MCT etc.
I would like to check the entire value from second position and if is a number I would like to extract it, for instance,
a. M365, from 2nd position 365 is a number so I would like to return this substring.
b. M3AB, from 2nd position 3AB is not a number so I would not want to return this substring.
I tried regex_substr('M365', '[0-9]', 2) but this is not how I want and it only returns what is there in the second position but not the entire substring.
This seems to do what you want:
select regexp_substr(substr(x, 2), '^\d+$')
This starts matching the pattern at the second position in the string, requiring that a number start there.
[0-9] only searches for one number. You want to know if they are all numbers, so you need the '+' operator. For more info, visit:
https://www.techonthenet.com/oracle/functions/regexp_substr.php
The following code should work for you.
regex_substr('M365', '[0-9]+', 2)

SQL Server substring without upper bound

What is the best way in SQL Server to do
SELECT
SUBSTRING('CATCH ME IF YOU CAN', 2, 10000)
-- ATCH ME IF YOU CAN
with no upper bound?
Use STUFF instead (STUFF (Transact-SQL)):
SELECT STUFF('CATCH ME IF YOU CAN',1,1,'');
Here, STUFF replaces 1 character, from position 1 with the string ''. The 2nd parameter is the start position, and the 3rd is the number of characters (from that position) to replace. The 4th is the replacement string.
So, as a further example, you could do something like this:
SELECT STUFF('2019-07-09 11:38:00',11,1,'T');
This replaces 1 character from position 11 with the character 'T', which returns '2019-07-09T11:38:00', changing the above value to the ISO8601 format. As you can see, the length of the string to replace does not need to be the same length as the replacement string as well (in fact, the 3rd parameter can have a value of 0, meaning that no characters are replaced and the "replacement" string is simply injected into the existing value).
Using LEN() with variable
DECLARE #Val AS VARCHAR (MAX) = 'CATCH ME IF YOU CAN';
SELECT SUBSTRING(#Val, 2, LEN(#Val));
or directly with LEN()
SELECT SUBSTRING('CATCH ME IF YOU CAN', 2, LEN('CATCH ME IF YOU CAN'));
You can use RIGHT() function, combined with LEN().
All you have to do is subtract from LEN() the number of chars that you want to exclude from the start of the string:
SELECT RIGHT('CATCH ME IF YOU CAN', LEN('CATCH ME IF YOU CAN') - 1)

Parsing a string and comparing values to existing column

I have the below table with the string marked "Remark" that needs to be parsed. The highlighted fares need to be compared from the columns TotalBookedFare and Remark. The only issue is that the value I need to compare under the Remark column is in the middle of a string. I've tried to parse the string but I cannot figure it out. I am using SQL Server 2008. As you can see the first row is not a match while the other three are matching.
Ideally I would like to convert the one string "Remark" to the 5 columns listed below so I can compare the TotalBookedFare to the "New" column.dionbennett
I think this should work
select substring(
remark, --string base
charindex ('/', 'xyz/57.77usd/zyx') + 1,
--starting position is location one to the right of first instance of / character (5)
charindex ('u', 'xyz/57.77usd/zyx', charindex ('/', 'xyz/57.77usd/zyx')) - charindex ('/', 'xyz/57.77usd/zyx') - 1
--length is the location of the first instance of the u character
--starting from the location of first instance of the / character (10)
--then subtracted by the location of the first instance of the / character (4)
--and then an additional 1 resulting in the length of the string to be extracted (5)
)
The string I put in there is just a more concrete example, if you replace it with Remark, it should extract the substring for each row. You could even modify it with some copy/pasting to get each of those columns you were looking for.

split string with character

using SQL 2008; I have the following string:
EMCo: 1 WorkOrder: 12770 WOItem: 10
I am trying to get the WorkOrder #.
When the string did not have the WOItem on end of it, I was able to use the following statement to get WorkOrder #.
[WorkOrder] = LTRIM(RTRIM(RIGHT(HQMA.KeyString,CHARINDEX(':',REVERSE(HQMA.KeyString))-1)))
This statement moves and may have double digits for the Co#, and it does not always have WOItem #. Was hoping to find something that would split after the ":" and just take 2nd group.
Any suggestions?
The patindex suggestion above will work beautifully, now if you still want to use your current statement, substring will pull the same values, and replace will take out WOItem. Not very elegant, it works whether you have WOItem or not:
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
How about using patindex()? Assuming the work order always has five characters:
select substring(HQMA.KeyString,
patindex('%WorkOrder: %', HQMA.KeyString) + 11,
5) as WorkOrder