wafw1971
wafw1971

Reputation: 361

Understanding what the query is and does?

This is going to be a strange request, can someone explain in plain English what the query below is doing especially the <>0 bit.

Select S.[Silks_Skey], MC.[MajorColour_Skey], MC.[MajorColour]
from [dbo].[Silks] S 
inner join [dbo].[SubColour] SC on CHARINDEX(SC.[SubColour],S.[SilksName]) <> 0
inner join [dbo].[MajorColour] MC on SC.[MajorColour] = MC.[MajorColour]

Thanks

W

Upvotes: 1

Views: 47

Answers (2)

gareththegeek
gareththegeek

Reputation: 2418

The silk name contains the sub colour name related to it. Sub colours are related to Major colours. Return silks and their related major colours if the silk name contains the sub colour name.

Upvotes: 1

Andomar
Andomar

Reputation: 238086

The charindex(expressionToFind, expressionToSearch) function searches for the occurance of a string in another string. If the string is not found, it returns 0, otherwise it returns the position of the first string in the second.

inner join [dbo].[SubColour] SC on CHARINDEX(SC.[SubColour],S.[SilksName]) <> 0

So the join looks for all SubColours that are contained in the silk's name. For example, if the silk was called "high-quality blue and green silk", this would join in the blue and green subcolours.

Upvotes: 1

Related Questions