4

I would like to know if the last character of a string is an asterisk *

I have got the following which gives me the last character:

select RIGHT('hello*',1)

but how would I use it as a condition in an if statement by matching it? the following doesn't work as it only returns the last char.

select '*' =  RIGHT('hello*',1)

can I use regex?

3 Answers 3

9

You're just about there. By using:

select '*' =  RIGHT('hello*',1)

Sql evaluates the RIGHT but thinks you want to alias it as a column named *

You can use the expression conditionally:

if RIGHT('hello*',1) = '*'
    print 'Ends in *'
else
    print 'Does not end in *'

You can filter like so on a table:

select * 
from MyTable
where RIGHT(MyColumn, 1) = '*';

Although the performance won't be stellar. Edit : See Karl Kieninger's answer for ideas on how to greatly improve the performance of this query

2

StuartLC mentioned performance. You can performance gains at the cost of an indexed persisted computed column. This tactic has been mentioned several in other places, such as:

I created a sample to see it in action, but the execution plan showed it was never actually picking up the index I expected for index seeks on the computed columns. So now I'm not sure what is wrong with my test. If someone can point out my failure, I'll correct the answer.

CREATE TABLE Test(
  TestData VARCHAR(10)
 ,TestData_Reverse AS REVERSE(TestData) PERSISTED
 ,TestData_Right1 AS RIGHT(TestData,1) PERSISTED
)
INSERT INTO Test(TestData) VALUES ('Bob'),('Joe'),('Ed*')

CREATE INDEX IX_Test_TestData ON Test (TestData)
CREATE INDEX IX_Test_TestData_Reverse ON Test (TestData_Reverse)
CREATE INDEX IX_Test_TestData_Right1 ON Test (TestData_Right1)

GO

SELECT * FROM Test WHERE TestData LIKE '%*' --Index Scan
SELECT * FROM Test WHERE RIGHT(TestData,1) = '*' --Table Scan
SELECT * FROM Test WHERE TestData LIKE '*%' --Index Seek
SELECT * FROM Test WHERE TestData_Reverse LIKE '*%' --Table Scan
SELECT * FROM Test WHERE LEFT(TestData_Reverse,1) = '*' --Index Scan
SELECT * FROM Test WHERE TestData_Right1 = '*' --Table Scan

DROP TABLE Test

Edit - temporary edit

This really is excellent - You just need more data, so that:

  1. the selectivity of a few '*' rows improves to the point where select returns less than a few % of total table rows to warrant seeks
  2. the number of pages used to store the data is non-trivial so that relative costs are more accurate
  3. We need to be careful of SELECT * as this will trigger RID / Bookmark lookups (which are possibly more expensive than a cluster since we could carry at least one of the columns in a Clustered Index)

Here's some more data, tests done on Sql Express 2014:

INSERT INTO Test(TestData)
    SELECT o1.name + o2.name from sys.objects o1, sys.objects o2;

UPDATE STATISTICS Test;

SELECT TestData FROM Test WHERE TestData LIKE '%*' --Index Scan IX_Test_TestData *1 .038
SELECT TestData FROM Test WHERE RIGHT(TestData,1) = '*' --Index Seek (IX_Test_TestData_Right1) *2 .003
SELECT TestData FROM Test WHERE TestData LIKE '*%' --Index Seek IX_Test_TestData *3 .003
SELECT TestData FROM Test WHERE TestData_Reverse LIKE '*%' --Index Scan IX_Test_TestData *4 .038
SELECT TestData FROM Test WHERE LEFT(TestData_Reverse,1) = '*' --Index Scan IX_Test_TestData *5 .038
SELECT TestData FROM Test WHERE LEFT(TestData_Right1,1) = '*' --Index Scan IX_Test_TestData_Right1 *5 .031
SELECT TestData FROM Test WITH (INDEX = IX_Test_TestData_Reverse) WHERE TestData_Reverse LIKE '*%' --Index Scan IX_Test_TestData *6 .05
SELECT TestData FROM Test WHERE TestData_Right1 = '*' --Index Seek IX_Test_TestData_Right1 *7 .003
SELECT * FROM Test WHERE REVERSE(TestData) = '*dE' --Index Seek (IX_Test_TestData_Reverse) *8 .006

The great news IMO is that Sql Server was able to "grok" that RIGHT(TestData,1) could be substituted for the computed column and used IX_Test_TestData_Right1 (and the same for REVERSE *8). This has implication (admittedly for very specific queries) that the persisted computed columns can be hidden away from the world, behind the scenes like a plain index, and means that the lack of sargability of functions CAN actually be mitigated in limited cases.

w.r.t. the disappointing scans for e.g. *4, it also worth noting that the test table is a heap, and one of the possible reasons why Sql isn't using seeks for the LIKE operator is the perception that the RID lookup into the cluster will outweigh the benefit of using IX_Test_TestData_Right / IX_Test_TestData_Reverse over TestData. I believe an optimal strategy in real tables would be to use a covering index to INCLUDE the original unreversed column.

CREATE INDEX IX_Test_TestData_Reverse ON Test (TestData_Reverse) INCLUDE (TestData)

SELECT TestData FROM Test WHERE TestData_Reverse LIKE '*%' --Index Scan IX_Test_TestData .003
2
  • 1
    This is really great, and I was unaware that Sql was able to substitute a function with the equivalent persisted computed column (meaning that you could also give the illusion of SARGable, non-monotonic functions). This substitution also occurs on deterministic scalar (WITH SCHEMABINDING) user defined functions as well.
    – StuartLC
    Jan 10, 2015 at 8:40
  • 1
    Oh, right. Too small a table an SQL Server just decides brute force is faster than trying to use anything else. I knew that. Thanks for helping. I'm tempted to leave the temp edit in place, but will certainly do so until (unless) I get a chance to fully digest and incorporate. Jan 10, 2015 at 13:12
0

Try this:

DECLARE @x NVARCHAR(32) = 'bbbbbbb*aaaaaaa';

IF(LEN(@x)=(SELECT LEN(@x) - CHARINDEX('*', REVERSE(@x)) + 1))
PRINT 'End with *'
ELSE
PRINT 'No end with *'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.