How to use between clause on a nvarchar?

纵然是瞬间 提交于 2019-12-12 02:13:37

问题


I have a table which is attached in image here sample table values I want that this conditions will return one row

   where Vendor_Value_Table.Feature_ID in (17,19) and value_text like 'Dhol Wala$Shahnai Wala' and value_text between 0 and 100`

because this represent to single vendor_id but the problem is datatype of Value_Text Column is nvarchar(max) how to do that kindly suggest.


回答1:


Assuming you wont have ANY NEGATIVE NUMBER You must cast nvarchar to Int for your between clause and set a default for the case its not cast-able like below:

 SELECT *
  From Vendor_Value_Table
WHERE (Vendor_Value_Table.Feature_ID in (17,19)) 
AND(
value_text like 'Dhol Wala$Shahnai Wala' 
OR 
 (SELECT CASE WHEN ISNUMERIC(value_text) = 1 THEN CAST(value_text AS INT) ELSE -1 END) between 0 and 100
 )

We chose -1 as default becuase if its not cast able to number the between clause needs to be false always.

(IT WILL ONLY RESPONSE FOR THE NUMBERS LESS THAN INT RANGE)



来源:https://stackoverflow.com/questions/40049777/how-to-use-between-clause-on-a-nvarchar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!