Función O procedimiento de SQL Server para cortar registros de la tabla por parámetros de otra tabla
Frecuentes
Visto 127 equipos
1
I have two tables i should make a comparing between those two tables, the first table have one column this column is the full URL
and the other table have two columns first column is URLCategory
y el otro es the number
of how many /
i should cut before in the other table column URL
the first table is
URL
http://10.6.2.26/ERP/HRServices/WorkflowService.asmx
http://195.170.180.170/SADAD/PaymentNotificationService.asmx
http://10.6.2.26/ERP/HRServices/WorkflowService.asmx
http://10.6.2.26/ERP/HRServices/WorkflowService.asmx
http://10.6.2.26/ERP/HRServices/WorkflowService.asmx
http://217.146.8.6/din.aspx?s=11575802&client=DynGate&p=10002926
http://195.170.180.170/SADAD/PaymentNotificationService.asmx
http://10.6.2.26/ERP/HRServices/WorkflowService.asmx
http://195.170.180.170/SADAD/PaymentNotificationService.asmx
http://www.google.com/
the Second table which is hould compare with
URL CUT_BEFORE
http://10.6.2.26 3
http://217.146.8.6 1
http://195.170.180.170 2
I should compare between second table with first column to be like that
URL
http://10.6.2.26/ERP/HRServices
http://195.170.180.170/SADAD
http://10.6.2.26/ERP/HRServices
http://10.6.2.26/ERP/HRServices
http://10.6.2.26/ERP/HRServices
http://217.146.8.6
http://195.170.180.170/SADAD
http://10.6.2.26/ERP/HRServices
http://195.170.180.170/SADAD
http://www.google.com/
What's the function script to do something like that in SQLServer
OR can we make it in Stored procedure with while loop because when i tried to execute the last function below i used this query
declare @table table
( main_url NVARCHAR(MAX),URL NVARCHAR(MAX), count int)
insert @TABLE
select
Main_URL,T2.Url,T2.[Count]
from
(select
URL as Main_URL,LEFT(URL1, CHARINDEX('/', URL1) - 1) as URL1
from
(select URL,replace(stuff(URL1, 1,patindex('%://%', URL1 + '0'), ''),'//','') as URL1
from (select URL, convert(nvarchar(max),[Url]) Url1 from [dbo].[InternetUsage_nn] )T1)T)T1
left outer join [dbo].[InternetUsage_URL_List] T2
on T1.URL1=convert(nvarchar(max),T2.URL) where T2.URL is not null
select dbo.FindAbsolutePath('/',Main_url,count) from @Table
esperando tus respuestas
Muchas Gracias
2 Respuestas
1
The following does what you requre, with the aid of a Split
función:
CREATE FUNCTION dbo.Split(@StringToSplit NVARCHAR(MAX), @Delimiter NCHAR(1))
RETURNS TABLE
AS
RETURN
(
SELECT ID = ROW_NUMBER() OVER(ORDER BY n.Number),
Position = Number,
Value = SUBSTRING(@StringToSplit, Number, CHARINDEX(@Delimiter, @StringToSplit + @Delimiter, Number) - Number)
FROM ( SELECT TOP (LEN(@StringToSplit) + 1) Number = ROW_NUMBER() OVER(ORDER BY a.object_id)
FROM sys.all_objects a
) n
WHERE SUBSTRING(@Delimiter + @StringToSplit + @Delimiter, n.Number, 1) = @Delimiter
);
Once you have this function your code becomes relatively concise:
DECLARE @T TABLE (URL VARCHAR(1000));
DECLARE @T2 TABLE (URL VARCHAR(1000), Cut_Before INT);
-- POPULATE TABLES HERE (NOT INCLUDED TO SAVE SPACE)
WITH CTE AS
( SELECT FullURL = t.URL,
BaseURLLength = LEN(ISNULL(t2.URL, t.URL)),
Remainder = ISNULL(REPLACE(t.URL, t2.URL, ''), ''),
Cut_Before = ISNULL(t2.Cut_Before, 1)
FROM @T AS t
LEFT JOIN @T2 AS t2
ON t.URL LIKE t2.URL + '/%'
)
SELECT t.FullURL,
Cut = SUBSTRING(t.FullURL, 1, BaseURLLength + LEN(s.Value) + s.Position - 1)
FROM CTE t
OUTER APPLY dbo.Split(t.Remainder, '/') AS s
WHERE s.ID = t.Cut_Before;
The premise is, the first part inside the CTE identifies the part URL for each full url by joining using LIKE
. Utilizando http://10.6.2.26/ERP/HRServices/WorkflowService.asmx
as an example this will show the following:
FullURL: http://10.6.2.26/ERP/HRServices/WorkflowService.asmx
BaseURLLength: 16
Remainder: /ERP/HRServices/WorkflowService.asmx
Cut_Before: 3
Where remainder is what is left of the full url after you have removed the part URL. The split function will then split the remainder into a new row for each of it's component parts:
SELECT *
FROM dbo.Split('/ERP/HRServices/WorkflowService.asmx', '/');
Regresará:
ID Position Value
1 1
2 2 ERP
3 6 HRServices
4 17 WorkflowService.asmx
This is then limited to only the row that matches the Cut_Before
value. This row can then be used to establish the position to "Cut" the full URL (the starting position + the length of the value at that position).
contestado el 22 de mayo de 14 a las 15:05
0
i modified my code. this code block will resolve your problem.
CREATE Function FindAbsolutePath(
@TargetStr varchar(8000),
@SearchedStr varchar(8000),
@Occurrence int
)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @Result varchar(8000);
if CHARINDEX('http://',@SearchedStr)>0 --fix http://
BEGIN
set @Occurrence=@Occurrence+2;
END
;WITH Occurrences AS (
SELECT
Number,
ROW_NUMBER() OVER(ORDER BY Number) AS Occurrence
FROM master.dbo.spt_values
WHERE
Number BETWEEN 1
AND LEN(@SearchedStr)
AND type='P'
AND SUBSTRING(@SearchedStr,Number,LEN(@TargetStr))=@TargetStr
)
SELECT @Result= SUBSTRING(@SearchedStr,0,Number)
FROM Occurrences
WHERE Occurrence=@Occurrence
return @Result
END
--select dbo.FindAbsolutePath('/','http://10.6.2.26/ERP/HRServices/WorkflowService.asmx',3)
contestado el 22 de mayo de 14 a las 14:05
Also this function can use get n th index of spesific character in the string. - cankaya07
Thanks Bro. but when i tried to make it with full table it's give me an error i think we should add a loop in the function isn't it or i'm wrong :) - user3143565
stackoverflow.com/users/1382335/cankaya07 I'd like to know how i can execute it between two tables to compare all of the records - user3143565
i couldn't understand you. Compare all of records? What is your intend? this code should work. - cankaya07
stackoverflow.com/users/1382335/cankaya07 can we make it in stored procedure with while loop - user3143565
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas sql sql-server function stored-procedures or haz tu propia pregunta.
Interesting solution. I'd suggest changing the
JOIN
condition in your CTE tot.URL LIKE t2.URL + '/%'
, so that something likehttp://1.2.3.4
int2
won't match something likehttp://1.2.3.40/...
int
. - joe farrell@JoeFarrell I am inclined to agree with you. I have made the change to the JOIN. - garethd