0

I'm using sql server managment studio 2012

What I'm trying to do is check if string value is a bit if it is then I want to check if this value is greater than 1. here is my code:

(TRY_CONVERT(bit, '2') IS NULL OR TRY_CONVERT(bit, '2') > 1) 

For this sample purpose I hardcoded there a string with value.

Anyone can tell me why this doesn't work I mean second condition.

Romesh
  • 2,291
  • 3
  • 24
  • 47
Harry89pl
  • 2,415
  • 12
  • 42
  • 63

1 Answers1

2

bit fields in SQL Server can only have one of three values: 1, 0 or NULL therefore checking for values greater than 1 is invalid.

So to resolve your issue I would consider this method

SELECT Convert(bit, CASE WHEN your_field IN ('1', '0') THEN your_field END) As valid_bit
gvee
  • 16,732
  • 35
  • 50