I running an UPDATE statement, based on a JOIN from another table to identify and add the correct 'Destination' code
Below is my code.
UPDATE [BILLING]
SET [BILLING].[BILLDESTINATION] = [Orbisrates].[dbo].[RATES].[DESTINATION]
FROM [OrbisRates].[dbo].[RATES]
INNER JOIN [OrbisBilling].[dbo].[BILLING]
ON [OrbisBilling].[dbo].[BILLING].[TO] LIKE
[RATES].[DESTINATION] + ''%''
I largely works much the matching is not correct.
Here are the example codes in my destination list
1
100
1001
The trouble is that telephone number 1001999 returns 100 as the Destination, where as I need it to match the longest correct match, so I want it to return 1001.
What would be the correct syntax to match the longest/best match?
Any help greatly appreciated.
EDIT - FULL CODE HERE
DECLARE @tablevalue NVARCHAR(MAX), @sql NVARCHAR(MAX);
DECLARE table_value_cursor CURSOR FOR
SELECT DISTINCT [Tariff Lookup]
FROM [OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325]; OPEN table_value_cursor
FETCH NEXT FROM table_value_cursor INTO @tablevalue WHILE @@FETCH_STATUS = 0
BEGIN SELECT @sql = N'
UPDATE [masked_2014-06-30-2014-06-01-customer325]
SET [masked_2014-06-30-2014-06-01-customer325].[Sell Price] =
(CASE WHEN (DATEPART(hh,[OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325].[ConnectTime]) >= 8 AND DATEPART(hh,[OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325].[ConnectTime]) <= 17)
THEN ROUND ([Orbisrates].[dbo].[Orbis_Import_June2014].[Peakperminute]/60*[OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325].[ChargedTimeSecs]+[Orbisrates].[dbo].[Orbis_Import_June2014].[Peakconnect],4)
ELSE ROUND ([Orbisrates].[dbo].[Orbis_Import_June2014].[OffPeakperminute]/60*[OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325].[ChargedTimeSecs]+[Orbisrates].[dbo].[Orbis_Import_June2014].[OffPeakconnect],4)
END)
FROM [OrbisRates].[dbo].'+ @tablevalue +'
INNER JOIN [OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325] on [OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325].[To] LIKE '+ @tablevalue +'.[Destination]+ ''%''
WHERE [OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325].[tariff lookup] = '''+ @tablevalue +'''';
EXEC sp_executesql @sql;
FETCH NEXT FROM table_value_cursor
INTO @tablevalue;
END CLOSE table_value_cursor
DEALLOCATE table_value_cursor;
Here is your query rewritten with aliases so it is easier to read (at least for me):
UPDATE b
SET BillDestination = r.Destination
FROM OrbisRates.dbo.Rates r INNER JOIN
OrbisBilling.dbo.Billing b
on b.[To] LIKE r.Destination + '%';
The problem that you have is multiple matches. One way to solve this is with a subquery:
UPDATE b
SET BillDestination = (SELECT TOP 1 r.Destination
FROM OrbisRates.dbo.Rates r
WHERE b.[To] LIKE r.Destination + '%'
ORDER BY LEN(r.Destination) DESC
)
FROM OrbisBilling.dbo.Billing b;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.