我在SQL Server中有这个表
[ArticleID] - number
[ArticleName] - varchar
[Created_dtm] - date
[Update_dtm] - date
我想获取Created_dtm在Update_dtm之前0,1 - 1.5秒的所有行。
我试过
Select *
from [Article]
Where ([Created_dtm] + 0.1 < [Update_dtm]) or ([Created_dtm] + 1.5 < [Update_dtm])
但它不起作用。我失去了
答案 0 :(得分:2)
当您使用public class DisplayListView extends AppCompatActivity {
String response;
JSONObject jsonObject;
JSONArray jsonArray;
DrinksAdapter drinksAdapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_listview_layout);
listView = (ListView) findViewById(R.id.listview);
drinksAdapter = new DrinksAdapter(this,R.layout.row_layout);
listView.setAdapter(drinksAdapter);
response = getIntent().getExtras().getString("json_data");
try {
jsonObject = new JSONObject(response);
jsonArray = new JSONObject(response).getJSONArray("Menu");
//jsonArray = jsonObject.getJSONArray("Menu");
int count = 0;
String ime,kolicina,cijena;
while(count < jsonArray.length()) {
JSONObject JO = jsonArray.getJSONObject(count);
ime = JO.getString("ime");
kolicina = JO.getString("kolicina");
cijena = JO.getString("cijena");
Drinks drinks = new Drinks(ime,kolicina,cijena);
drinksAdapter.add(drinks);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(position) {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Drink drink = adapterView.getItem(i);
}
});
}
}
时,特别是在毫秒范围内,您必须非常认识到datetime
字段的精确度。在MS SQL中,datetime
数据类型仅精确到1/300秒。这意味着高精度将导致一些意外的舍入。这是微软建议使用datetime
代替datetime2
的主要原因之一。
设置
datetime
在我的例子中,我选择了许多显示舍入问题的边缘日期。这应该IF OBJECT_ID('tempdb..#times') IS NOT NULL
DROP TABLE #times ;
CREATE TABLE #times ( pickMe varchar(5), Created_dtm datetime, Update_dtm datetime ) ;
INSERT INTO #times ( pickMe, Created_dtm, Update_dtm )
VALUES
( 'YES', '9/6/2017 12:00:00.000' , '9/6/2017 12:00:01.250' ) -- 1.25
, ( 'YES', '9/6/2017 12:00:00.000' , '9/6/2017 12:00:00.100' ) -- 0.1
, ( 'YES', '9/6/2017 12:00:00.000' , '9/6/2017 12:00:01.500' ) -- 1.5
, ( 'NO' , '9/6/2017 12:00:00.000' , '9/6/2017 12:00:00.099' ) -- 0.099
, ( 'NO' , '9/6/2017 12:00:00.000' , '9/6/2017 12:00:01.501' ) -- 1.501
, ( 'NO' , '9/6/2017 12:00:00.000' , '9/6/2017 14:00:00.000' ) -- WAY OUT
, ( 'YES', '9/6/2017 11:59:59.500' , '9/6/2017 12:00:00.000' ) -- 0.5
, ( 'YES', '9/6/2017 11:59:59.900' , '9/6/2017 12:00:00.000' ) -- 0.1
, ( 'YES', '9/6/2017 11:59:58.500' , '9/6/2017 12:00:00.000' ) -- 1.5
, ( 'NO' , '9/6/2017 11:59:59.901' , '9/6/2017 12:00:00.000' ) -- 0.099
, ( 'NO' , '9/6/2017 11:59:58.499' , '9/6/2017 12:00:00.000' ) -- 1.501
, ( 'NO' , '9/6/2017 10:00:00.000' , '9/6/2017 12:00:00.000' ) -- WAY OUT
;
记录的SELECT
在Created_dtm
之前的.1到1.5秒之间,只有6个。
<强> QUERY 强>
Modified_dtm
返回
SELECT
DATEDIFF(millisecond,Created_dtm,Update_dtm) AS diff
, pickMe
, Created_dtm
, Update_dtm
FROM #times
WHERE DATEADD(millisecond, 100, Created_dtm) <= update_dtm
AND DATEADD(millisecond, 1500, Created_dtm) >= Update_dtm
任何包含| diff | pickMe | Created_dtm | Update_dtm |
-------------------------------------------------------------------------
| 1250 | YES | 2017-09-06 12:00:00.000 | 2017-09-06 12:00:01.250 |
| 100 | YES | 2017-09-06 12:00:00.000 | 2017-09-06 12:00:00.100 |
| 1500 | YES | 2017-09-06 12:00:00.000 | 2017-09-06 12:00:01.500 |
| 100 | NO | 2017-09-06 12:00:00.000 | **2017-09-06 12:00:00.100** |
| 1500 | NO | 2017-09-06 12:00:00.000 | **2017-09-06 12:00:01.500** |
| 500 | YES | 2017-09-06 11:59:59.500 | 2017-09-06 12:00:00.000 |
| 100 | YES | 2017-09-06 11:59:59.900 | 2017-09-06 12:00:00.000 |
| 1500 | YES | 2017-09-06 11:59:58.500 | 2017-09-06 12:00:00.000 |
| 100 | NO | **2017-09-06 11:59:59.900** | 2017-09-06 12:00:00.000 |
| 1500 | NO | **2017-09-06 11:59:58.500** | 2017-09-06 12:00:00.000 |
= PickMe
的内容都不应包括在内。另请注意,显示的日期已四舍五入,导致'NO'
计算错误。
注意:我讨厌约会。
答案 1 :(得分:1)
您可以使用毫秒来执行此操作:
Select *
from [Article]
Where (dateadd(ms, 100, Created_dtm) > [Update_dtm]) and
(dateadd(ms, 1500, Created_dtm) < [Update_dtm])
或写出时间部分的全名:
Select *
from [Article]
Where dateadd(millisecond, 100, Created_dtm) > [Update_dtm] and
dateadd(millisecond, 1500, Created_dtm) < [Update_dtm]
答案 2 :(得分:1)
另一种选择......
SELECT
*,
SecsDiff = DATEDIFF(ms, td.Created_dtm, td.Update_dtm) / 1000.0
FROM
#TestData td
WHERE
DATEDIFF(ms, td.Created_dtm, td.Update_dtm) BETWEEN 100 AND 1500;