Link to home
Start Free TrialLog in
Avatar of cheryl9063
cheryl9063Flag for United States of America

asked on

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..
SOLUTION
Avatar of sshah254
sshah254

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brendt Hess
This handles most data types and sizes without any problems

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


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

SELECT *
FROM MyTable
WHERE Cast(Column2 as varchar(32)) LIKE '%09'
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
[Using % also requires that the column is an integer (of any size).]