Solved

T-SQL query explaination

Posted on 2011-03-06
5
432 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
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:ewangoya
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 250 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:
ewangoya earned 250 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…
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…

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now