[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

T-SQL query explaination

Hi all,

I have some problems understanding the following query. Do you know what the following query does?

SELECT
      CASE
      WHEN CHARINDEX(' ',RecipeName) <> 0 THEN
      SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1,
      case
      when charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName))) = 0 then LEN(RecipeName)
      else charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName)))
     
  end)

ELSE
    'No Second Word'
     END Second_Word
FROM Recipe


Thanks
0
Itudk_2010
Asked:
Itudk_2010
  • 2
  • 2
2 Solutions
 
Ephraim WangoyaCommented:
CHARINDEX serchers for a specified substring within a string and returns the position of the substring
SUBSTRING copies a specified number of characters from a string starting from a given index

 WHEN CHARINDEX(' ',RecipeName) <> 0 THEN
      SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1,

If there is a space in the recipe, then copy all characters up to the space
eg if its "salt sugar pepper"
then you end up with "Salt"

when charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName))) = 0 then LEN(RecipeName)
      else charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName)))

Now we look for the second space character and get the string
eg "sugar"

0
 
Itudk_2010Author Commented:
Hi ewangoya,

Thanks for the explaination. I still don't understand the following part?

when charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName))) = 0 then LEN(RecipeName)
      else charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName)))

Looking forward to your reply.
0
 
JoeNuvoCommented:
if RecipeName = "Salt Sugar"

   SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName)) will give value of "Sugar"
   charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName))) will equal to zero
   LEN(RecipeName) will given to the outside substring function

so, 2nd word, which is last word will be return.


if RecipeName = "Salt Sugar Pepper"
   SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName)) will give value of "Sugar Pepper"
   charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName))) will become the length of 2nd  word (+1 for the space)
   which is not zero, and will be return to previous SubString function

so, 2nd word will be return with space at the end  (for ex.  "Sugar ")

extra note.
to return without space
code should be

else charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName))) - 1
0
 
Ephraim WangoyaCommented:

when charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName))) = 0 then LEN(RecipeName)
      else charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName)))

This is getting the index of the second space character in the string

Lets break it down
Assume the string to be "salt sugar pepper"
1. charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName)))
    part in bold will get the index of the first space,  in this case 5 then add 1

Now the statement is equvalent to
2.  charindex(' ', SUBSTRING(RecipeName, 6, LEN(RecipeName)))

3. The substring part then extracts the string from index 6 to the rest of the string  (LEN(RecipeName))
     this would be
    => charindex(' ', SUBSTRING(RecipeName, 6, 17)))
    => charindex(' ', 'sugar pepper'))
    => 5
   
4. In this case the result is 5, the general statement has equated to
   
    when 5 = 0 then
       LEN(RecipeName)  --this is false
     else
        charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName))) --we return this value which is 5
    end
 
0
 
Itudk_2010Author Commented:
good
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now