November 16, 2016 at 8:20 pm
Comments posted to this topic are about the item Find the brackets
November 16, 2016 at 8:39 pm
IMHO, cleaner code if you take the shortcut.
SELECT mychar FROM mydata WHERE mychar LIKE '[[]%'
--Jeff Moden
Change is inevitable... Change for the better is not.
November 17, 2016 at 12:20 am
I tried this against a SQL Server 2008 R2 database and both 1 and 5 brought back the bracketed records. Luckily I choose answer 1.
November 17, 2016 at 1:15 am
TimCarrett (11/17/2016)
I tried this against a SQL Server 2008 R2 database and both 1 and 5 brought back the bracketed records. Luckily I choose answer 1.
Query 5 will bring back records that have characters before the bracket too. The question was for rows that begin with a bracket. Although in the example posted it brings back the same rows, if you run the queries against the table below the outcome will be different.
CREATE TABLE #MyData
(
mychar VARCHAR(50)
);
GO
INSERT #MyData
(mychar)
VALUES
('This is a string'),
('"A Quoted String"'),
('''Single quoted string'''),
('''more single quotes'''),
('AA[My bracketed string]'),
(''),
('[Can I find this string]')
;
GO
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
November 17, 2016 at 2:19 am
Wasn't aware of ESCAPE as I would have used '[[]%'
- Damian
November 17, 2016 at 2:40 am
Hi Steve,
it seems to me that there are a couple of minor mistakes in the solution: "The only query that will return the two rows is the one that uses the ESCAPE option ".
The rows returned are three, not two. Then I think that it would be better to say that the query n.5 returns the three rows as well, but the logic is not correct: in a bigger table it would return also strings like "xyq[My...", "1[Can..." because of the "%".
Grasshoppers like me could be confused
November 17, 2016 at 3:44 am
I'm assuming there was a typo in one of the options and it was meant to have a character before the initial bracket, so the correct answer would have returned 2 rows and the final option would have been clearly wrong as it returned 3.
Personally I'd use one of the alternatives suggested in the explanation as I think it's clearer
SELECT mychar FROM mydata WHERE mychar LIKE '![%' ESCAPE '!'
November 17, 2016 at 4:49 am
This was removed by the editor as SPAM
November 17, 2016 at 5:21 am
Nice question on one of the basics of strings. Thanks, Steve.
November 17, 2016 at 5:52 am
This would also work and keep the code simple...
SELECT *
FROM dbo.MyData
WHERE Left(mychar,1)='[';
November 17, 2016 at 5:58 am
Laurie Dunn (11/17/2016)
This would also work and keep the code simple...SELECT *
FROM dbo.MyData
WHERE Left(mychar,1)='[';
Yeah I got
select * from MyData where CHARINDEX('[',mychar,0) = 1
which works fine, then looked at the options and realised today's lesson was about ESCAPE :hehe:
November 17, 2016 at 6:36 am
Jeff Moden (11/16/2016)
IMHO, cleaner code if you take the shortcut.
SELECT mychar FROM mydata WHERE mychar LIKE '[[]%'
That's how I went about doing it and then had to take a closer look at the possible answers
Cheers
November 17, 2016 at 7:03 am
funbi (11/17/2016)
Laurie Dunn (11/17/2016)
This would also work and keep the code simple...SELECT *
FROM dbo.MyData
WHERE Left(mychar,1)='[';
Yeah I got
select * from MyData where CHARINDEX('[',mychar,0) = 1
which works fine, then looked at the options and realised today's lesson was about ESCAPE :hehe:
Both of these queries are non SARGable, which would result in an index scan instead of an index seek.
November 17, 2016 at 7:32 am
Luis Cazares (11/17/2016)
funbi (11/17/2016)
Laurie Dunn (11/17/2016)
This would also work and keep the code simple...SELECT *
FROM dbo.MyData
WHERE Left(mychar,1)='[';
Yeah I got
select * from MyData where CHARINDEX('[',mychar,0) = 1
which works fine, then looked at the options and realised today's lesson was about ESCAPE :hehe:
Both of these queries are non SARGable, which would result in an index scan instead of an index seek.
The table has 7 rows.
November 17, 2016 at 7:42 am
This was removed by the editor as SPAM
Viewing 15 posts - 1 through 15 (of 40 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy