简体   繁体   English

sql server:我的日期数据出了什么问题?

[英]sql server: what's wrong with my date data?

i have a column with dates, but it is a varchar: 我有一个日期列,但它是一个varchar:

8/31/2010 9:48
8/31/2010 9:49
8/31/2010 9:51
8/31/2010 9:52
8/31/2010 9:55
8/31/2010 9:59
8/31/2010 10:11
8/31/2010 10:13
8/31/2010 10:16
8/31/2010 10:37
8/31/2010 10:42

i made sure that none of these will be a BAD date: 我确保这些都不是一个糟糕的日期:

SELECT *
FROM qcvalues.dbo.batchinfo
WHERE ISDATE(reporttime) <> 1

this returned 0 results 这返回0结果

question: i need to return dates between a certain range: 问题:我需要在某个范围之间返回日期:

select rowid from qcvalues.dbo.batchinfo where CONVERT(DATE, Substring( reporttime, 1, LEN(reporttime)), 103)
    between cast('2010-08-01' as datetime) and CAST('2010-08-31' as datetime)

and i am getting this error; 我收到了这个错误;

Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.

what is wrong with my conversion? 我的转换有什么问题?

If you need to store dates then use a datetime column in the future 如果您需要存储日期,请在将来使用日期时间列

does this work? 这有用吗?

WHERE CONVERT(DATE,RTRIM(reporttime))
BETWEEN '2010-08-01' and '2010-08-31' 

If not use SET DATEFORMAT MDY before running it 如果在运行之前不使用SET DATEFORMAT MDY

And if you have to store it in a varchar column then use YYYYMMDD format...that way you can do 如果你必须将它存储在varchar列中,那么使用YYYYMMDD格式......你可以这样做

WHERE reporttime like '201008%' if you want August 2010 如果您想要2010年8月, WHERE reporttime like '201008%'

在查询之前放置SET DATEFORMAT MDY。

This will solve your problem: 这将解决您的问题:

select rowid
from qcvalues.dbo.batchinfo
where
   CONVERT(DATE, reporttime, 101) >= '20100801'
   -- style 101, not 103
   -- also notice date conversion invariant format YYYYMMDD with no separators
   AND CONVERT(DATE, reporttime, 101) < '20100901'
   -- using BETWEEN with an end date of '8/31/2010' will skip
   --   times between '8/31/2010 00:00:00.003' and '8/31/2010 23:59:59.997'

Try this to see what the problem is: 试试看看问题是什么:

select convert(datetime, '8/31/2010 9:48', 103)
select convert(datetime, '8/31/2010 9:48', 101)

Remember, this CAST('2010-08-31' as datetime) will have its time portion as 00:00 . 请记住,此CAST('2010-08-31' as datetime)时间CAST('2010-08-31' as datetime)的时间部分为00:00

Consider casting your varchar data as smalldatetime, and being specific about the boundaries of times in your between. 考虑将varchar数据转换为smalldatetime,并具体说明您之间的时间边界。 No need to be converting, substring, etc. Just one CAST will do. 无需转换,子串等等。只需一个CAST

Consider this as a potential solution: 将此视为潜在的解决方案:

SELECT rowid from qcvalues.dbo.batchinfo 
WHERE CAST(reporttime as smalldatetime)
BETWEEN '2010-08-01' AND '2010-08-31 23:59'

This will strip out the time portion too 这也将剥离时间部分

select rowid 
    from qcvalues.dbo.batchinfo 

    Where cast(floor(cast(cast(reportTime as datetime)as float))as datetime)

     between    cast('2010-08-01' as datetime)
     and        cast('2010-08-31' as datetime)

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

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