TSQL Help2

I need a query that will pull all the rows where column2 has a number that ends in 09.. How?

For example if the number is 1309 I want it or 9909 etc..
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

select * from table where right(convert(int, column2), 2) = "09"

should work

convert(int ... is done if it's not an int so there is no problem with decimal precision
but right(....,2)  = "09" is a char-function  
so an exctra convert(varchar...)  is needed to be able to take the last 2 positions and compare it with "09"

select * from table where right(convert(varchar,convert(int, column2)), 2) = "09"
This will work in both condition if column is int or nvarchar

select * from TableName where <ColumnName> like '%09'

Open in new window

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

The like '%09' is a more readable version of  the Right() = '09'  (I worked further on the given solution including the ""-error where it must be '')

But using different data-types without explicitly converting them is fooling the one who has to maintain that  sql in future. If you read that you directly assume that column is a varchar or nvarchar.  
Brendt HessSenior DBACommented:
This handles most data types and sizes without any problems

FROM MyTable
WHERE Cast(Column2 as varchar(max)) LIKE '%09'

If we know that the value is numeric, this can be changed to

FROM MyTable
WHERE Cast(Column2 as varchar(32)) LIKE '%09'
Brendt HessSenior DBACommented:
Now - one more case.  If you also must check if the column is a number - that is, if the column is textual and may contain numbers - a validation check will need to be added to the process.

This may be what the Convert(int, column2) suggested by sshah254 was intended to do, but this will generate errors if the data is not numeric, bigger than an Int, etc.  Instead, a LIKE and IsNumeric() check will validate for this.

    SELECT *
    FROM MyTable m
    WHERE m.Column2 NOT LIKE '%![0-9. ]%'   -- If the column does not contain anything but digits, decimal, and space
        AND m.Column2 LIKE '%09'  -- the column matches the pattern needed
     ) src
WHERE IsNumeric(Src.Column2)=1  -- the column is actually numeric

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony PerkinsCommented:
Assuming numeric than:
FROM YourTableName
WHERE column2 % 100 = 9
Scott PletcherSenior DBACommented:
[Using % also requires that the column is an integer (of any size).]
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.