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 ;
Related
select
case when concat('ACC','-',NVL(trim(bvmo.booking),''),'-',NVL(trim(bvmo.org),''),'-',NVL(trim(bvma.sparm),''),'-',NVL(trim(bvmo.id),'') like 'ACC--%' then
NULL ELSE concatconcat('ACC','-',NVL(trim(bvmo.booking),''),'-',NVL(trim(bvmo.org),''),'-',NVL(trim(bvma.sparm),''),'-',NVL(trim(bvmo.id),'') END AS Parent_id
from bvmo
output:
Parent-id
:----------:
ACC-1123-1344--
ACC-4567-6528--
ACC-7890-9827--
ACC-1143-8079--
ACC-1883-8944--
am expecting as the below output where i can remove the "-" in above case condition in which the values are not appearing in respective columns
Expected output:
Parent-id
:----------:
ACC-1123-1344
ACC-4567-6528
ACC-7890-9827
ACC-1143-8079
ACC-1883-8944
Use concat_ws. It should take care of nulls in the way you wanted.
select
concat_ws('-', 'ACC', trim(bvmo.booking), trim(bvmo.org), trim(bvma.sparm), trim(bvmo.id)) AS Parent_id
from bvmo
I have a query which returns a bunch of different data, however I want to have it replace all the values upon a certain condition.
What I have written below kind of gives me the result I want but not really. It creates a new column instead of replacing the other one:
SELECT
CASE
WHEN T4.[U_DestType] = '6'
THEN (SELECT
'Company Limited' AS [ShipToCode]
)
END AS [ShipToCode],
T2.[ShipToCode],
T6.[StreetS],
T6.[StreetNoS],
T6.[CityS],
T6.[ZipCodeS],
T6.[CountryS],
T5.[LicTradNum],
T2.[CardCode],
T4.[Phone1],
T4.[E_Mail],
T4.[U_DestType],
CASE
WHEN T4.[Country] = 'GB'
THEN 'EN'
ELSE T4.[Country]
END AS [Country],
T4.[U_ShortName]
FROM[...]
The end goal is to replace all of the columns with some preset values instead of just ShipToCode as above.
I tried putting an EXIST subquery after FROM too but that didn't work either.
Is this possible? I'm probably missing something very obvious.
Many thanks!
You can use an ELSE in your CASE expression to combine the two "columns":
CASE
WHEN T4.[U_DestType] = '6'
THEN (SELECT
'Company Limited' AS [ShipToCode]
)
ELSE T2.[ShipToCode]
END AS [ShipToCode],
And by the way, you didn't need to use a Sub-Select. This would work just as well and is easier to read:
CASE
WHEN T4.[U_DestType] = '6' THEN 'Company Limited'
ELSE T2.[ShipToCode]
END AS [ShipToCode],
SELECT DISTINCT ISNULL(a.[BPOAGE], 0) AS BPOAGE, a.[BPOAttic]
FROM [Legacy].[dbo].[MyTables] as a
Result :
SELECT DISTINCT ISNULL(a.[BPOAGE], 0) AS BPOAGE, a.[BPOAttic]
FROM [Legacy].[dbo].[MyTables] as a
where (a.[BPOAGE] not in ('New'))
Result :
Q : Can you tell me why 0 values are not shown when I put this condition a.[BPOAGE] not in ('New')?
Sql works on Three valued logic.It considers NULL as unknown,since it is unknown it will not got selected in the condition you write.If you want to include null rewrite the code as
SELECT DISTINCT ISNULL(a.[BPOAGE], 0) AS BPOAGE,a.[BPOAttic] FROM [Legacy].
[dbo].[MyTables] as a where (a.[BPOAGE] not in ('New') or a.[BPOAGE] is null)
I'm trying to get the last record from this query but i don't know how to do it. I used ROW_NUMBER but my program (Protheus ADVPL) don't have resources to get the last line from a query
SELECT ROW_NUMBER() OVER (ORDER BY B1_MASTER, B1_COD) AS ID,
B1_COD,
B1_DESC,
B1_CATEG,
B1_MASTER,
A2_COMPRAD,
ISNULL((SELECT Sum(C6_QTDVEN * C6_PRCVEN)
FROM SC6010 SC6,
SF4010 SF4,
SC5010 SC5
WHERE C6_FILIAL = '01'
AND C6_PRODUTO = B1_COD
AND SC6.D_E_L_E_T_ <> '*'
AND C5_FILIAL = C6_FILIAL
AND C5_NUM = C6_NUM
AND C5_EMISSAO BETWEEN '20160401' AND '20160404'
AND C5_TIPO = 'N'
AND C5_MODAL = '2'
AND SC5.D_E_L_E_T_ <> '*'
(query have 106 lines so i ll not put everything)
I need the total records in a column, like this:
Tabela
What can i do?
Tks
You can use MAX(field) too.
But, you're using ADVPL, so you could use dbSeek instead to find the last RECNO.
So, using "work area" you can find the last record with this:
TRB->(RECCOUNT())
I changed ROW_NUMBER to ##ROWCOUNT and it works! Tks all
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]