How do I check the last two digits?

SUBBIS
SUBB1D
SUBBD3
SUBB12

      

In the above values, how can I check the last two digits (IS, 1D, D3, 12) are numbers using sql code?

+3


source to share


3 answers


Do you want to get these values? You can do it with like

:

where column like '%[0-9][0-9]'

      



If you need to make sure the values ​​always end in numbers 2

, you can do so with a similar check constraint.

+7


source


To check the last two digits enter the digits in the column, you can use the following script.

... WHERE ISNUMERIC(RIGHT(your_column,2)) = 1

      

This RIGHT(your_column,2)

will return the last two digits from the string.



or

SELECT ISNUMERIC(RIGHT(your_column,2))

      

will return 1 (if its a number), otherwise 0

+1


source


You can do it like this:

SELECT MyId,
ISNUMERIC(RIGHT(MyColumn,2)) -- your column to check last 2 (if numeric)
FROM (
----- replace with your table
SELECT 1 MyId,'SUBBIS' MyColumn UNION SELECT 2,'SUBB1D' UNION 
SELECT 3,'SUBBD3' UNION  SELECT 4,'SUBB12' 
----- replace with your table
) A

      

Hope this helps. :)

0


source







All Articles