title: What is Difference between using C1 > '42' and C1>42 in check constraint in sqlserver
body: <p>I am adding check <em>constraint</em> to column like below. I am getting different results with both ways of using it. Can anybody explain what makes the difference?</p>
  
  <pre><code>ALTER TABLE [dbo].[state] WITH CHECK ADD CONSTRAINT [chk_State_partition_42] CHECK ([Code]&gt;='42' AND [Code]&lt;'43')
  
  ALTER TABLE dbo.State WITH CHECK ADD CONSTRAINT [chk_state_partition_42] CHECK ([code]&gt;=42 AND [code]&lt;43)
  </code></pre>
  
  <p>--Actually above second query is result of following dynamic sql code
  DECLARE @code varchar(2) = '42'
      , @SQLStr varchar(max)
    --SET @SQLStr = 'ALTER TABLE dbo.state  WITH CHECK ADD  CONSTRAINT [chk_state_partition_'+@APIState+'] CHECK  ([code]>='+@APIState+' AND [code]&lt;'+CAST(CAST(@code as int)+1 as varchar(2))+')'
   Select @SQLStr</p>