?
Solved

T-SQL query explaination

Posted on 2011-03-06
5
Medium Priority
?
450 Views
Last Modified: 2012-05-11
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
Comment
Question by:Itudk_2010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35047325
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
 

Author Comment

by:Itudk_2010
ID: 35047784
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
 
LVL 11

Assisted Solution

by:JoeNuvo
JoeNuvo earned 1000 total points
ID: 35052954
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
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 1000 total points
ID: 35063302

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
 

Author Closing Comment

by:Itudk_2010
ID: 35068689
good
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In this article, we’ll look at how to deploy ProxySQL.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question