Replace start of string in SQL - sql

SELECT REPLACE([strUrl], '/myoldurl', '/mynewurl') FROM [UrlRewrite]
If strUrl is /myoldurl/myoldurl_something_else, it returns /mynewurl/mynewurl_something_else. I want it to be /mynewurl/myoldurl_something_else.
How do I just replace from start of string in SQL?

SELECT REPLACE([strUrl], '/myoldurl/', '/mynewurl/') FROM [UrlRewrite]
to address Martin's point:
SELECT CASE WHEN [strUrl] = '/myoldurl' THEN '/mynewurl' ELSE REPLACE([strUrl], '/myoldurl/', '/mynewurl/') END FROM [UrlRewrite]
or
SELECT
CASE
WHEN LEFT([strUrl], LEN('/myoldurl')) = '/myoldurl'
THEN '/mynewurl' + SUBSTRING([strUrl], LEN('/myoldurl')+1, LEN([strUrl]))
ELSE [strUrl]
END
FROM [UrlRewrite]

Hi you can check this simple solution using STUFF function.
DECLARE #url varchar(255)='/myoldurl/myoldurl_something_else'
SELECT STUFF(#url,CHARINDEX('old',#url,1),3,'new')

How about
SELECT '/mynewurl' + SUBSTRING([strUrl], LEN('/myoldurl')+1,8000) FROM [UrlRewrite]
WHERE [strUrl] LIKE '/myoldurl%'

To replace from the start of the string, you could either filter using LIKE or if that isn't appropriate to your query, use CASE, SUBSTRING and CHARINDEX to conditionally do the replacement

In your example, justo do that:
SELECT REPLACE([strUrl], '/myoldurl/', '/mynewurl/') FROM [UrlRewrite]

Related

Change some fields in a query

I'm using the following query to get a lis of server:
select
DISTINCT upper("Local_Hostname_U")
from
"Services_Inventory_610"
where
CASE
--WHEN upper("Local_Hostname_U")='EAP-WEBSERVICES.WEB.INPS' THEN upper("Local_Hostname_U")='WEBINPS59'
WHEN "TMZDIFF"=-3600 THEN ("Interval_Begin_Time">=1230110230000000 and "Interval_Begin_Time"<1230111230000000)
WHEN "TMZDIFF"=-7200 THEN ("Interval_Begin_Time">=12301100000000 and "Interval_Begin_Time"<1230110230000000)
END
order by upper("Local_Hostname_U")
I get the following list:
CUSTMON03.SERVIZI.INPS
EAP-WEBSERVICES.WEB.INPS
JAVAZAPPWS-P01
JAVAZAPPWS-P02
JAVAZAPPWS-P03
JAVAZAPPWS-P04
JAVAZAPPWS-P05
JAVAZAPPWS-P06
JAVAZAPPWS-P07
JAVAZAPPWS-P08
LHEWBSWAS01
LHEWBSWAS02
LHEWBSWAS03
LHEWBSWAS04
LHEWBSWAS05
LHEWBSWAS06
LHEWBSWAS07
LHEWBSWAS08
LHEWBSWAS09
LHEWBSWAS10
LHEWBSWAS11
LHEWBSWAS12
WEBINPS120.SERVIZI.INPS
WEBINPS52
WEBINPS53
WEBINPS54
WEBINPS55
WEBINPS56.SERVIZI.INPS
WEBINPS58
WEBINPS62.SERVIZI.INPS
Is there a way to substitute EAP-WEBSERVICES.WEB.INPS with WEBINPS59 and to delete the string ".SERVIZI.INPS" from the list where it is in the same query?
Try this:
select DISTINCT REPLACE(REPLACE(upper("Local_Hostname_U"), 'EAP-WEBSERVICES.WEB.INPS', 'WEBINPS59 '), '.SERVIZI.INPS', '')
from "Services_Inventory_610"
where
CASE
--WHEN upper("Local_Hostname_U")='EAP-WEBSERVICES.WEB.INPS' THEN upper("Local_Hostname_U")='WEBINPS59'
WHEN "TMZDIFF"=-3600 THEN ("Interval_Begin_Time">=1230110230000000 and "Interval_Begin_Time"<1230111230000000)
WHEN "TMZDIFF"=-7200 THEN ("Interval_Begin_Time">=12301100000000 and "Interval_Begin_Time"<1230110230000000)
END
order by upper("Local_Hostname_U")
try with split_part if you use postgreSQL:
select DISTINCT upper(case when "Local_Hostname_U" LIKE '%-%' then split_part(split_part("Local_Hostname_U",'-',2),'.',1)
else split_part("Local_Hostname_U",'.',1) end)
from
"Services_Inventory_610"
where
CASE
--WHEN upper("Local_Hostname_U")='EAP-WEBSERVICES.WEB.INPS' THEN upper("Local_Hostname_U")='WEBINPS59'
WHEN "TMZDIFF"=-3600 THEN ("Interval_Begin_Time">=1230110230000000 and "Interval_Begin_Time"<1230111230000000)
WHEN "TMZDIFF"=-7200 THEN ("Interval_Begin_Time">=12301100000000 and "Interval_Begin_Time"<1230110230000000)
END
order by upper("Local_Hostname_U")
This split_part fuction show only what is before .
select split_part(name,'.',1) from hostname ;

Returning values from a database with a Substring

Let's say I've got 3 product description fields with the values AC-120 XXX, AC-120,CCC and AC-120 BBB.
How would I get that information from a table using only AC-120 as my search argument?
I've tried using the subStr function but that won't return any values either
SELECT TbArtikel.Artikel_Merk, TbArtikel.Artikel_Groep, TbArtikel.Artikel_Categorie_ID, TussenMAATenARTKEL.VoorraadNummer, TbArtikel.Artikel_ID, TussenMAATenARTKEL.ArtikelDetail_ID, TbArtikel.Artikel_Prijs_Advies, TbArtikel.Artikel_Prijs_Bees, TbArtikel.Artikel_Omschrijving
FROM TbArtikel INNER JOIN TussenMAATenARTKEL ON TbArtikel.Artikel_ID = TussenMAATenARTKEL.Artikel_ID
WHERE (((TbArtikel.Artikel_Merk)="Yonex") AND ((TbArtikel.Artikel_Omschrijving)="%AC-102%"));
LIKE is very appropriate for this comparison. However, MS Access uses different wildcards from standard SQL. So you want:
TbArtikel.Artikel_Omschrijving) LIKE "*AC-102*"
In standard SQL, this would be:
TbArtikel.Artikel_Omschrijving) LIKE "%AC-102%"
Try '((TbArtikel.Artikel_Omschrijving) LIKE "%AC-102%"' instead of '((TbArtikel.Artikel_Omschrijving)="%AC-102%"'
Use MID() function as
MID(TbArtikel.Artikel_Omschrijving, 1, 6) = 'AC-120'
Instead of
TbArtikel.Artikel_Omschrijving ="%AC-102%"

I need help in filtering the content of this SQL column

I need help in filtering the content of this SQL column. I have unfortunately been unsuccessful so far. I will be happy for any assistance.
My goal is for all the unc paths to bear the same format.
All should look like: \\ps9\wa033242. Meaning all should begin with the "\\" replacing the "///"
I tried truncating it but because of the different string length, I have problems.
I tried truncating and UPDATING
SELECT
cw_platz.nummer,
cw_platz.nwaddress,
cw_platz.bezeichnung,
os_cw.cw_ldzuplatz.ldruckernr,
os_cw.cw_ldzuplatz.papierschacht,
os_cw.cw_ldzuplatz.treibername,
cw_logischerdrucker.bezeichnung
FROM
cw_platz,
os_cw.cw_ldzuplatz,
cw_logischerdrucker
WHERE
cw_platz.nummer = os_cw.cw_ldzuplatz.platznr and
cw_logischerdrucker.nummer = os_cw.cw_ldzuplatz.ldruckernr and
cw_platz.bezeichnung in cw_platz.bezeichnung
This is my result:
My first thought is to simply use something like REPLACE(yourstringhere, '/','\').
Is it something you already tried?
Reference: https://learn.microsoft.com/en-us/sql/t-sql/functions/replace-transact-sql?view=sql-server-2017
you could use replace
select replace('client/ps9///wa033242//', '/' ,'\');.
and for update
update your_table
set your_column = replace(your_column, '/' ,'\')
try avoid .. the where like
UPDATE os_cw.cw_ldzuplatz
SET os_cw.cw_ldzuplatz.treibername = REPLACE(os_cw.cw_ldzuplatz.treibername, '/' ,'\')
FROM
cw_platz,
os_cw.cw_ldzuplatz,
cw_logischerdrucker
WHERE
cw_platz.nummer = os_cw.cw_ldzuplatz.platznr and
cw_logischerdrucker.nummer = os_cw.cw_ldzuplatz.ldruckernr and
cw_platz.bezeichnung = cw_platz.bezeichnung
Try this it will help you.
UPDATE os_cw.cw_ldzuplatz
SET os_cw.cw_ldzuplatz.treibername = REPLACE(os_cw.cw_ldzuplatz.treibername, '\\','///')
FROM
cw_platz,
os_cw.cw_ldzuplatz,
cw_logischerdrucker
WHERE
cw_platz.nummer = os_cw.cw_ldzuplatz.platznr and
cw_logischerdrucker.nummer = os_cw.cw_ldzuplatz.ldruckernr and
cw_platz.bezeichnung = cw_platz.bezeichnung
and TREIBERNAME like '\\%'

Is there an advanced like function where i can get results even if one word matches

hello I am trying to achieve something like this
should give results if
where 'Robert Doug' like '%Robert'
or
where 'Robert Doug' like '%Doug Robert'
if users enters first name or last name and if it matches in that whole string it should return the record.
Any help is appreciated.
Try like this
SELECT * FROM [YOUR TABLE_NAME] WHERE [COLUMN_NAME] LIKE ('%'+[USER PROMPT]+'%')
Or try this..
SELECT PATINDEX('%Robert%',NAME_COLUMN)
FROM [YOUR TABLE_NAME]
Or..
SELECT NAME_COLUMN
FROM [YOUR TABLE_NAME]
WHERE PATINDEX('%Robert%',NAME_COLUMN)<>'0'
Thank you all i tried something like this
WHERE
( d_searchJobTitle.term like '%'+case when CHARINDEX(' ','implementation analyst') >0 then SUBSTRING('implementation analyst',1,CHARINDEX(' ','implementation analyst')) else 'implementation analyst' end+'%'
and
d_searchJobTitle.term like '%'+ case when CHARINDEX(' ','implementation analyst') >0 then SUBSTRING('implementation analyst',CHARINDEX(' ','implementation analyst'),len('implementation analyst'))
else 'implementation analyst' end
+'%' )

sql select with one value of two where

This is the only place that I get all answer ;)
I want to select :
SELECT
RTRIM(LTRIM(il.Num_bloc)) AS Bloc,
RTRIM(LTRIM(il.num_colis)) AS Colis,
cd.transporteur AS Coursier,
cd.origine AS Origine,
cd.destination AS Destinataire,
cd.adresse AS [Adresse Destinataire],
cd.poids AS Poids,
il.Signataire, il.num_cin AS CIN, il.date_livraison AS [Date Livraison]
FROM
dbo.cd
INNER JOIN
dbo.il ON cd.bloc = il.Num_bloc AND dbo.cd.colis = dbo.il.num_colis
WHERE
(il.Num_bloc = RTRIM(LTRIM(#ParamBloc)))
AND (il.num_colis = RTRIM(LTRIM(#ParamColis)))
In the way of getting result if the user put ether #ParamBloc or #ParamColis
Try using IsNull() function.
A simple query would go like this
Select * from yourTable
Where
Num_bloc = ISNULL(#ParamBloc, Num_block) AND
num_colis = ISNULL(#ParamColis, num_colis)
The second parameter would make the expression to true if the #parameter Bloc or Colis is null. This query would be useful for all 4 possible combination of these two parameter.