Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

T-SQL query explaination

Posted on 2011-03-06
5
Medium Priority
?
452 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

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!

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

650 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