Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Substring comprehension Expand / Collapse
Author
Message
Posted Saturday, October 31, 2009 1:20 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, April 08, 2011 8:55 AM
Points: 274, Visits: 149
Comments posted to this topic are about the item Substring comprehension
Post #812043
Posted Sunday, November 01, 2009 3:46 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, September 05, 2013 6:50 AM
Points: 315, Visits: 78
Refer this link
http://www.ianywhere.com/developer/product_manuals/sqlanywhere/0902/en/html/dbrfen9/00000293.htm
Post #812090
Posted Monday, November 02, 2009 12:31 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, April 01, 2014 2:57 PM
Points: 443, Visits: 822
Took a guess and got it wrong, but...
Ran this on 2005 and A is not actually NULL, its an empty string. Maybe its NULL on 2008?
Post #812209
Posted Monday, November 02, 2009 1:07 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, April 10, 2014 7:08 AM
Points: 3,448, Visits: 4,406
The correct answer is: A is '' (empty string), B is 'Chris'.
Try this code:
declare @name varchar(12)
select @name = 'Christopher'
select case when substring(@name,15,12) is null then 'NULL' else 'NOT NULL' end as A, substring(@name,-6,12) as B

and you will see the result:
A        B
-------- ------------
NOT NULL Chris

(1 row(s) affected)

Despite what is shown in 2008 BOL

Maybe the author of the question uses some "secret special" BOL
I use the following link: http://msdn.microsoft.com/en-us/library/ms187748.aspx, and what I see there:
SUBSTRING ( value_expression ,start_expression , length_expression )
...
If length_expression is negative, an error is generated and the statement is terminated.

So, length_expression cannot be negative, but the "-6" from the question is start_expression.

the code ... does not return the whole value expression when the sum of the start and length values are greater than the length of the variable

Just a little detail from BOL:
the whole value expression beginning at start_expression is returned
Post #812213
Posted Monday, November 02, 2009 5:14 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, March 28, 2014 1:51 AM
Points: 1,298, Visits: 1,305
Just a little mistake, empty string and null.
But to tell the truth, I was confused, because I know the first expression returns empty string and not a null...




See, understand, learn, try, use efficient
© Dr.Plch
Post #812313
Posted Monday, November 02, 2009 7:15 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Friday, November 09, 2012 2:48 PM
Points: 493, Visits: 636
Even though A is empty string versus Null the answer is still obvious given that it is multiple choice. In other words it was obvious that the author had just made a mistake between empty/null since options A and C were so far off.
Post #812361
Posted Tuesday, November 03, 2009 12:30 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, November 05, 2013 6:08 AM
Points: 1,076, Visits: 591
I chose the one closest to the answer and I seem to have got it right. Hihahhhhh

What you don't know won't hurt you but what you know will make you plan to know better
Post #812769
Posted Tuesday, November 03, 2009 7:09 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, October 28, 2013 3:12 PM
Points: 313, Visits: 253
Can some one explains how it returns 'chris' when the start value is -6 and length is 12.
Post #812904
Posted Tuesday, November 03, 2009 8:04 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, July 23, 2013 6:34 AM
Points: 654, Visits: 265
sorry, wrong post..
Post #812941
Posted Tuesday, November 03, 2009 10:00 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, February 12, 2010 8:31 AM
Points: 29, Visits: 37
If you think of it sort of as a number line, the name "Christopher" would occupy spots 1 thru 11 of the line. The expression is askiing you to look at spots -6 thru 5. This is 12 spots because of the number zero. The first 7 spots (-6 thru 0) we're asked to look at don't correspond to any characters in the name and don't return any characters. But the last 5 spots correspond to spots 1 thru 5 of the number line and those spots are occupied by the characters "Chris".

This is perhaps a clumsy example, but it seems to work.

Post #813058
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse