Other DIFFERENCE SQL function


1. Usage of DIFFERENCE to show Soundex Difference

SQL Server Query 1

            
 SELECT TOP 10 c1.CompanyName AS CompanyName1, c2.CompanyName AS CompanyName2,   
DIFFERENCE(c1.CompanyName, c2.CompanyName) AS SoundexDifference
FROM Customers c1 CROSS JOIN Customers c2
WHERE c1.CustomerID < c2.CustomerID /* Avoid comparing a company to itself twice */
ORDER BY SoundexDifference DESC, CompanyName1, CompanyName2;

Create SQL query with SqlQueryBuilder 1

            
 var (sql, parameters) = new SqlQueryBuilder()  
.Select().Top(10)
.Column("c1.CompanyName", "CompanyName1")
.Column("c2.CompanyName", "CompanyName2")
.Column(new DIFFERENCE(new Column("c1.CompanyName"), new Column("c2.CompanyName")), "SoundexDifference")
.From("Customers", "c1")
.Join(new List<IJoin>()
{
new CROSSJOIN().TableName(new Table("Customers", "c2"))
})
.Where(new Where(new Column("c1.CustomerID").LessThan(new Column("c2.CustomerID"))))
.OrderBy(new OrderBy().SetColumnDescending("SoundexDifference").SetColumnAscending("CompanyName1"))
.Build();

Query build by SqlQueryBuilder 1

            
SELECT TOP 10 c1.CompanyName AS CompanyName1,
              c2.CompanyName AS CompanyName2,
              DIFFERENCE(c1.CompanyName, c2.CompanyName) AS SoundexDifference
FROM Customers AS c1 CROSS JOIN Customers AS c2
WHERE c1.CustomerID < c2.CustomerID
ORDER BY SoundexDifference DESC, CompanyName1 ASC;


            
        

Parameters (If used)

Name Value

Query Results 1:

  CompanyName1 CompanyName2 SoundexDifference
1 Du monde entier Die Wandernde Kuh 4
2 Folies gourmandes Folk och fä HB 4
3 France restauration Franchi S.p.A. 4
4 Frankenversand France restauration 4
5 Frankenversand Franchi S.p.A. 4
6 Hungry Coyote Import Store Hungry Owl All-Night Grocers 4
7 La corne d'abondance La maison d'Asie 4
8 Lehmanns Marktstand Lonesome Pine Restaurant 4
9 Magazzini Alimentari Riuniti Maison Dewey 4
10 The Big Cheese The Cracker Box 4


2. Usage of DIFFERENCE to show name difference

SQL Server Query 2

            
 SELECT FirstName, LastName,   
DIFFERENCE(FirstName, LastName) AS NameDifference
FROM Employees
ORDER BY NameDifference DESC, FirstName, LastName;

Create SQL query with SqlQueryBuilder 2

            
 var (sql2, parameters2) = new SqlQueryBuilder()  
.Select()
.Column("FirstName", "FirstName")
.Column("LastName", "LastName")
.Column(new DIFFERENCE(new Column("FirstName"), new Column("LastName")), "NameDifference")
.From("Employees")
.OrderBy(new OrderBy().SetColumnDescending("NameDifference").SetColumnAscending("FirstName").SetColumnDescending("LastName"))
.Build();

Query build by SqlQueryBuilder 2

            
SELECT FirstName AS FirstName,
       LastName AS LastName,
       DIFFERENCE(FirstName, LastName) AS NameDifference
FROM Employees
ORDER BY NameDifference DESC, FirstName ASC, LastName DESC;


            
        

Parameters (If used)

Name Value

Query Results 2:

  LastName FirstName NameDifference
1 Callahan Laura 2
2 Fuller Andrew 1
3 Peacock Margaret 1
4 Suyama Michael 1
5 Davolio Nancy 1
6 Buchanan Steven 1
7 Dodsworth Anne 0
8 Leverling Janet 0
9 King Robert 0


3. Usage of DIFFERENCE to show Product Soundex Difference

SQL Server Query 3

            
 SELECT TOP 15 p1.ProductName AS ProductName1, p2.ProductName AS ProductName2,   
DIFFERENCE(p1.ProductName, p2.ProductName) AS ProductSoundexDifference
FROM Products p1
CROSS JOIN Products p2
WHERE p1.ProductID < p2.ProductID
ORDER BY ProductSoundexDifference DESC, ProductName1, ProductName2;

Create SQL query with SqlQueryBuilder 3

            
 var (sql3, parameters3) = new SqlQueryBuilder()  
.Select().Top(15)
.Column("p1.ProductName", "ProductName1")
.Column("p2.ProductName", "ProductName2")
.Column(new DIFFERENCE(new Column("p1.ProductName"), new Column("p2.ProductName")), "ProductSoundexDifference")
.From("Products", "p1")
.Join(new List<IJoin>()
{
new CROSSJOIN().TableName("Products", "p2")
})
.Where(new Where(new Column("p1.ProductID"), SQLComparisonOperators.LESS_THAN, new Column("p2.ProductID")))
.OrderBy(new OrderBy().SetColumnDescending("ProductSoundexDifference")
.SetColumnAscending("ProductName1")
.SetColumnAscending("ProductName2"))
.Build();

Query build by SqlQueryBuilder 3

            
SELECT TOP 15 p1.ProductName AS ProductName1,
              p2.ProductName AS ProductName2,
              DIFFERENCE(p1.ProductName, p2.ProductName) AS ProductSoundexDifference
FROM Products AS p1 CROSS JOIN Products AS p2
WHERE p1.ProductID < p2.ProductID
ORDER BY ProductSoundexDifference DESC, ProductName1 ASC, ProductName2 ASC;


            
        

Parameters (If used)

Name Value

Query Results 3:

  ProductName1 ProductName2 ProductSoundexDifference
1 Chef Anton's Cajun Seasoning Chef Anton's Gumbo Mix 4
2 Genen Shouyu Gnocchi di nonna Alice 4
3 Louisiana Fiery Hot Pepper Sauce Louisiana Hot Spiced Okra 4
4 Queso Cabrales Queso Manchego La Pastora 4
5 Sasquatch Ale Scottish Longbreads 4
6 Sir Rodney's Marmalade Sir Rodney's Scones 4
7 Camembert Pierrot Rhönbräu Klosterbier 3
8 Carnarvon Tigers Chartreuse verte 3
9 Chai Chang 3
10 Chai Chef Anton's Cajun Seasoning 3
11 Chai Chef Anton's Gumbo Mix 3
12 Chai Côte de Blaye 3
13 Chang Gnocchi di nonna Alice 3
14 Chang NuNuCa Nuß-Nougat-Creme 3
15 Chang Zaanse koeken 3


4. Usage of DIFFERENCE to identify difference

SQL Server Query 4

            
 SELECT DIFFERENCE('Smith', 'Smyth') AS SmithSmyth,       /*-- Output: 4 (very similar)*/   
DIFFERENCE('Johnson', 'Johnsen') AS JohnsonJohnsen, /*-- Output: 3 (similar)*/
DIFFERENCE('Apple', 'Ape') AS AppleApe, /*-- Output: 3 (similar)*/
DIFFERENCE('Cat', 'Dog') AS CatDog; /*-- Output: 0 (not similar)*/

Create SQL query with SqlQueryBuilder 4

            
 var (sql4, parameters4) = new SqlQueryBuilder()  
.Select().Column(new DIFFERENCE("Smith", "Smyth"), "SmithSmyth")
.Column(new DIFFERENCE("Johnson", "Johnsen"), "JohnsonJohnsen")
.Column(new DIFFERENCE("Apple", "Ape"), "AppleApe")
.Column(new DIFFERENCE("Cat", "Dog"), "CatDog")
.Build();

Query build by SqlQueryBuilder 4

            
SELECT DIFFERENCE(@pMAIN_2507200210130394980, @pMAIN_2507200210130394981) AS SmithSmyth,
       DIFFERENCE(@pMAIN_2507200210130394982, @pMAIN_2507200210130394983) AS JohnsonJohnsen,
       DIFFERENCE(@pMAIN_2507200210130394984, @pMAIN_2507200210130394985) AS AppleApe,
       DIFFERENCE(@pMAIN_2507200210130394986, @pMAIN_2507200210130394987) AS CatDog;


            
        

Parameters (If used)

Name Value
@pMAIN_2507200210130394980 Smith
@pMAIN_2507200210130394981 Smyth
@pMAIN_2507200210130394982 Johnson
@pMAIN_2507200210130394983 Johnsen
@pMAIN_2507200210130394984 Apple
@pMAIN_2507200210130394985 Ape
@pMAIN_2507200210130394986 Cat
@pMAIN_2507200210130394987 Dog

Query Results 4:

  SmithSmyth JohnsonJohnsen AppleApe CatDog
1 4 4 3 2