Itudk_2010
asked on
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,CHARI NDEX(' ',RecipeName) + 1,
case
when charindex(' ', SUBSTRING(RecipeName,CHARI NDEX(' ',RecipeName) + 1, LEN(RecipeName))) = 0 then LEN(RecipeName)
else charindex(' ', SUBSTRING(RecipeName,CHARI NDEX(' ',RecipeName) + 1, LEN(RecipeName)))
end)
ELSE
'No Second Word'
END Second_Word
FROM Recipe
Thanks
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,CHARI
case
when charindex(' ', SUBSTRING(RecipeName,CHARI
else charindex(' ', SUBSTRING(RecipeName,CHARI
end)
ELSE
'No Second Word'
END Second_Word
FROM Recipe
Thanks
ASKER
Hi ewangoya,
Thanks for the explaination. I still don't understand the following part?
when charindex(' ', SUBSTRING(RecipeName,CHARI NDEX(' ',RecipeName) + 1, LEN(RecipeName))) = 0 then LEN(RecipeName)
else charindex(' ', SUBSTRING(RecipeName,CHARI NDEX(' ',RecipeName) + 1, LEN(RecipeName)))
Looking forward to your reply.
Thanks for the explaination. I still don't understand the following part?
when charindex(' ', SUBSTRING(RecipeName,CHARI
else charindex(' ', SUBSTRING(RecipeName,CHARI
Looking forward to your reply.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
good
SUBSTRING copies a specified number of characters from a string starting from a given index
WHEN CHARINDEX(' ',RecipeName) <> 0 THEN
SUBSTRING(RecipeName,CHARI
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,CHARI
else charindex(' ', SUBSTRING(RecipeName,CHARI
Now we look for the second space character and get the string
eg "sugar"