尝试在两个日期之间执行 sql 语句时出错

发布于 2024-12-28 17:09:23 字数 529 浏览 0 评论 0原文

SQL:

dim sql
sSql = "SELECT * FROM [dbo].[table] WHERE [sent] = 1 and datesent between '" & dStartDate & "' and '"  & dFinishDate & "'"
response.write(sSql)
set oRs = oConn.execute(sSql)       

当我在 sql server 2008 中执行此 sql 时,它工作正常。 但是,当我在应用程序中执行它时,我收到错误:

Microsoft OLE DB Provider for SQL Server error '80040e07'

The conversion of a char data type to a datetime data type resulted
in an out-of-range datetime value.

我必须在应用程序中做一些不同的事情吗?谢谢

The SQL:

dim sql
sSql = "SELECT * FROM [dbo].[table] WHERE [sent] = 1 and datesent between '" & dStartDate & "' and '"  & dFinishDate & "'"
response.write(sSql)
set oRs = oConn.execute(sSql)       

When i execute this sql in sql server 2008 it works fine.
However, when i execute it within my application i get error:

Microsoft OLE DB Provider for SQL Server error '80040e07'

The conversion of a char data type to a datetime data type resulted
in an out-of-range datetime value.

Is there something different i have to do in the application? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小瓶盖 2025-01-04 17:09:23

您尝试过使用 ISO 格式的日期吗?

SELECT * FROM [dbo].[Table]  
WHERE [sent] = 1 and datesent between '20120101' and '20120127' 

(严格来说,sql server 的ISO 8601 日期yyyy-mm-ddThh:mm :ss[.mmm])

旁注:当输出到文本(或有文字)时,始终使用 ISO 格式日期;这样它们就可以在所有系统上明确地读取)。

SQL Server ISO 8601 格式日期

< a href="http://msdn.microsoft.com/en-us/library/ms190977(v=sql.90).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms190977(v=sql.90).aspx

Have you tried using ISO format dates?

SELECT * FROM [dbo].[Table]  
WHERE [sent] = 1 and datesent between '20120101' and '20120127' 

(Strictly speaking an ISO 8601 date for sql server is yyyy-mm-ddThh:mm:ss[.mmm])

Side Note: always use ISO format dates when you output to text (or have literals); that way they can be read unambiguously on all systems).

SQL Server ISO 8601 Format Dates

http://msdn.microsoft.com/en-us/library/ms190977(v=sql.90).aspx

花开柳相依 2025-01-04 17:09:23
SELECT * 
FROM [dbo].[Table] 
WHERE 
    [sent] = 1 
and datesent between CONVERT(datetime, '01/01/2012', 101) and CONVERT(datetime, '01/27/2012', 101)

OR

您的列 datesent 不是 DATETIME 数据类型

SELECT * 
FROM [dbo].[Table] 
WHERE 
    [sent] = 1 
and datesent between CONVERT(datetime, '01/01/2012', 101) and CONVERT(datetime, '01/27/2012', 101)

OR

Your column datesent is not DATETIME datatype

似狗非友 2025-01-04 17:09:23

检查您的日期时间设置。

set dateformat mdy
select CAST('01/27/2012' as datetime)
set dateformat dmy
select CAST('01/27/2012' as datetime) --> Exception

您可以使用以下命令显示当前设置:

DBCC USEROPTIONS

Check your datetime setting.

set dateformat mdy
select CAST('01/27/2012' as datetime)
set dateformat dmy
select CAST('01/27/2012' as datetime) --> Exception

You can display the current setting with:

DBCC USEROPTIONS
提赋 2025-01-04 17:09:23

您应该始终对日期使用 ISO 格式“YYYYMMDD”,因为无论本地化设置如何,SQL Server 始终会正确解析该格式。

You should always use the ISO-formats 'YYYYMMDD' for your dates, as this will always be parsed correctly by SQL Server, regardless of localization setting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文