SQL Server DATEDIFF一直忽略日期的秒部分

时间:2015-05-07 05:08:10

标签: sql sql-server tsql sql-server-2005

我在执行以下语句时得到0

SELECT DATEDIFF(mi,'1970-01-01 00:00:00','1970-01-01 00:00:01') * CONVERT(BIGINT,60)*1000  as BidTicks    

我执行此操作时6000

SELECT DATEDIFF(mi,'1970-01-01 00:00:00','1970-01-01 00:01:01') * CONVERT(BIGINT,60)*1000  as BidTicks    

我有什么选择?

3 个答案:

答案 0 :(得分:3)

您需要使用s或ss作为预期输出。

select DATEDIFF(s,'1970-01-01 00:00:00','1970-01-01 00:00:01') * CONVERT(BIGINT,60)*1000  as BidTicks 

它会生效:

60000

答案 1 :(得分:1)

要以秒为单位返回差异作为查询中的第一个参数,您需要使用秒作为参数,即s:

select DATEDIFF(s,'1970-01-01 00:00:00','1970-01-01 00:01:01')

基本上,您的查询要求两者之间的分钟数。您的第一个查询返回0,第二个返回1.

因此0 * 60 * 1000 = 01 * 60 * 1000 = 60000

尝试以下方法:

select DATEDIFF(s,'1970-01-01 00:00:00','1970-01-01 00:01:01') 
-- * convert(BIGINT,60) wasn't sure if this was necessary still
* 1000  as BidTicks   

答案 2 :(得分:0)

使用public static void parseFile(String s) throws IOException { File file = new File("data.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { final String lineFromFile = scanner.nextLine(); if (lineFromFile.contains(s)) { System.out.println(scanner.nextLine()); // code hangs right here. } } } s代替ss

mi
select DATEDIFF(s,'1970-01-01 00:00:00','1970-01-01 00:00:01') * CONVERT(BIGINT,60)*1000  as BidTicks