October 16, 2022 at 12:55 pm
A community member called Jeff Morden helped with a query that removes characters from a field value, see https://www.sqlservercentral.com/forums/topic/how-to-remove-character-and-carriage-return-from-fields#post-4099382
Can someone help modify the code to include www. when it doesn't exists. For example, the query
SELECT tt.homepage_url
,websiteurl = LEFT(v1.RightString,COALESCE(NULLIF(CHARINDEX('/',v1.RightString)-1,-1),150))
FROM #tmpTable tt
CROSS APPLY (VALUES(SUBSTRING(homepage_url,CHARINDEX('//',homepage_url)+2,150)))v1(RightString)
;
Will produce the following results:
You notice that http://navico.com and https://zeb-consulting.com is not prepended with www, as a result the code removes http://. However, as well as removing http:// I would like www prepended (or is the word prefaced? 🙂 with www.
So, can someone help modify the code such to add www where it doesn't exists?
Sample Data
CREATE TABLE tmpTable (
homepage_url nvarchar(150),
websiteurl varchar(100))
INSERT tmpTable VALUES
(N'http://www.opisnet.com','www.opisnet.com'),
(N'http://www.ebookers.com','www.ebookers.com'),
(N'http://navico.com','navico.com'),
(N'https://zeb-consulting.com','www.zeb.de'),
(N'http://www.hobbycraft.co.uk','www.hobbycraft.co.uk'),
(N'http://www.parliament.uk/lords','www.parliament.uk/lords'),
(N'https://www.intermed.de/','www.intermed.de/'),
(N'http://www.iac.com','www.iac.com'),
(N'https://www.esteve.es','www.esteve.com/'),
(N'http://www.kornferry.com/','www.kornferry.com')
October 16, 2022 at 1:41 pm
You could nest the current formula inside an additional CONCAT function and test for the existence of the lead characters
SELECT tt.homepage_url
,websiteurl = concat(iif(left(v1.RightString, 4)=N'www.', null, N'www.'),
LEFT(v1.RightString,COALESCE(NULLIF(CHARINDEX(N'/',v1.RightString)-1,-1),150)))
FROM #tmpTable tt
CROSS APPLY (VALUES(SUBSTRING(homepage_url,CHARINDEX(N'//',homepage_url)+2,150))) v1(RightString)
;
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
October 16, 2022 at 4:43 pm
One of my suggestions in one of the many other threads you have created on this subject was to fix the data. You can write all the code you want but bad data will always win out...
Assuming that every site must be preceded with 'www' is also incorrect. A good example is Microsoft's online documentation which is now located at https://learn.microsoft.com. How about one of the other sites where we can get help https://forums.sqlteam.com.
To answer your question - how do you determine if 'www.' exists in a string - and if it doesn't, add it?
SELECT someColumn = IIF(somestring NOT LIKE 'www.%', CONCAT('www.', someString), someString)
-- Or using CASE
SELECT someColumn = CASE WHEN someColumn NOT LIKE 'www.%' THEN CONCAT('www.', someString) ELSE someString END
To simplify your code - here is a hint: Add another CROSS APPLY using the results from v1(RightString) to calculate the value for websiteurl - then use another CROSS APPLY to prepend the 'www.' to the results from websiteurl. At that point you can then use the final result in any other expressions as needed.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
October 17, 2022 at 12:14 am
I can also tell you for sure that the "community member"s name was not Jeff Morden. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply