简体   繁体   English

使用SQL和C#检查数字是否在范围之间

[英]Check if a number is between a range using SQL & C#

I have a table with the following info: 我有一个包含以下信息的表:

ID   Name                  Range          Checked
1    Jennifer             000-100           0
2    Louis                101-200           0
3    Michael              201-300           0

The range are the numbers of the tickets they have, and the checked column are the number of tickets that have been used. 范围是他们拥有的票证数量,选中的列是已使用的票证数量。

What I want to do is to add +1 to the column Checked when a ticket is used, so I want to check where the ticket belongs. 我想做的是在使用票证时在“已检查”列中添加+1,因此我想检查票证所属的位置。 I mean, if I use ticket number 103, I want to add 1 to the column checked in the row number 2. And so on if I use more tickets. 我的意思是,如果我使用票证编号103,我想在行号2的检查列中添加1。以此类推,如果我使用更多票证,则依此类推。

ID   Name                  Range          Checked
1    Jennifer             000-100           0
2    Louis                101-200           1
3    Michael              201-300           0

So, is there a way to check if the ticket I have submitted is between one of the ranges? 那么,有没有办法检查我提交的票证是否在上述范围之一之间?

PD.: I know how to check if a number is between two numbers in SQL, and I do also know how to get info from specific rows using C#, what I don't know how to do is to check the entire table to see if the number is between the ranges column. PD .:我知道如何在SQL中检查数字是否在两个数字之间,而且我也知道如何使用C#从特定行中获取信息,我不知道怎么做是检查整个表以查看如果数字在ranges列之间。

If the Range Values are 3 digits, left() and right() would do the trick without having to find the dash. 如果范围值是3位数字,则left()和right()可以完成技巧而不必找到破折号。

Example

Update YourTable 
   Set Checked=Checked+1
 Where 103 between left(Range,3) and Right(Range,3)

Select * from YourTable

Results 结果

ID  Name        Range     Checked
1   Jennifer    000-100   0
2   Louis       101-200   1
3   Michael     201-300   0

EDIT - CharIndex() option For Variable Ranges 编辑-可变范围的CharIndex()选项

Update @YourTable 
   Set Checked=Checked+1
 Where 103 between left(Range,charindex('-',Range+'-')-1) and Right(Range,len(Range)-charindex('-',Range+'-'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM