May 12, 2015 at 9:31 am
I am trying to access records which start with numbers 75
Looks like LIke doesn't work.
How can I do so for
Case when Numbers like '75%'
May 12, 2015 at 9:43 am
sharonsql2013 (5/12/2015)
I am trying to access records which start with numbers 75Looks like LIke doesn't work.
How can I do so for
Case when Numbers like '75%'
You could try something like:
CASE WHEN CAST(Numbers AS VARCHAR) LIKE '75%'
Or
CASE WHEN LEFT(CAST(Numbers AS VARCHAR), 2) = '75'
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
May 12, 2015 at 9:51 am
Thanks
May 12, 2015 at 10:23 am
I'm not sure how Alvin's query solved the problem as the LIKE operator will implicitly convert values into strings.
DECLARE @test-2 TABLE(
Numbers int)
INSERT INTO @test-2 VALUES(12),(75924),(75),(53756),(2354)
SELECT Numbers
,Case when Numbers like '75%' THEN 'True' ELSE 'False' END
,CASE WHEN CAST(Numbers AS VARCHAR) LIKE '75%' THEN 'True' ELSE 'False' END
,CASE WHEN LEFT(CAST(Numbers AS VARCHAR), 2) = '75' THEN 'True' ELSE 'False' END
FROM @test-2
You can change the Numbers data type to any type that can be implicitly converted and you'll get the same result for the different options. You'll have more problems with the input values than with the LIKE operator.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply