【问题标题】:Conversion failed when converting date and/or time from character string DateTime从字符串 DateTime 转换日期和/或时间时转换失败
【发布时间】:2013-12-05 21:36:57
【问题描述】:
public static DataTable ReportsCityFilter(DateTime dtStart, DateTime dtEnd)
{
        //Dictionary<DateTime.TryParse((string DateString, IFormatProvider provider, System.Globalization.DateTimeStyles styles, "ddMMyyyy"), object> objDic = new Dictionary<DateTime, object>();

        Dictionary<string, object> objDic = new Dictionary<string, object>();
        objDic.Add("@start_date", dtStart);
        objDic.Add("@end_date", dtEnd);

        return dal.execute(objDic, "sp_admin_reports_city_filter_select").Tables[0];
}

我有一个按城市报告的页面,现在我在其上添加了一个下拉列表以按日期过滤结果,即周、今天、月、年等

其他一切正常,但这段代码有问题,因为它给我错误“从字符串转换日期和/或时间时转换失败。”

附:这是我的存储过程

更改程序 [dbo].[sp_admin_reports_city_filter_select] -- 这里添加存储过程的参数 --@id INT = NULL @start_date 日期时间 , @end_date 日期时间 作为 开始 --declare @startdate 日期时间 设置@start_date = '2999-01-01'

if @start_date = 'today'
begin
    select jp.id, city.name[City]
    , row_number() over (order by city.name) [sr_no]
    , count(jp.id) over (partition by name) as no_of_posts 
    , COUNT(od.id) over (partition by name) as no_of_employers
    ,CONVERT(varchar(12), jp.posting_date, 103) [date_created]

    from rs_job_posting jp

    inner join rs_job_posting_location jpl on jpl.id = jp.id
    inner join rs_cor_city city on city.id = jpl.city_fk
    inner join rs_organization_detail od on od.id = jp.id

    where DAY(posting_date) = DAY(GETDATE())
    order by no_of_posts Desc 

    select jp.date_updated
    from rs_job_posting jp
END

else if @start_date = 'weekly'
begin
select jp.id, city.name[City]
    , row_number() over (order by city.name) [sr_no]
    , count(jp.id) over (partition by name) as no_of_posts 
    , COUNT(od.id) over (partition by name) as no_of_employers
    ,CONVERT(varchar(12), jp.posting_date, 103) [date_created]

    from rs_job_posting jp

    inner join rs_job_posting_location jpl on jpl.id = jp.id
    inner join rs_cor_city city on city.id = jpl.city_fk
    inner join rs_organization_detail od on od.id = jp.id

    where DAY(posting_date) = DAY(GETDATE())
    order by no_of_posts Desc 

    select jp.date_updated
    from rs_job_posting jp
end

else if @start_date = 'byweekly'
begin
select jp.id, city.name[City]
    , row_number() over (order by city.name) [sr_no]
    , count(jp.id) over (partition by name) as no_of_posts 
    , COUNT(od.id) over (partition by name) as no_of_employers
    ,CONVERT(varchar(12), jp.posting_date, 103) [date_created]

    from rs_job_posting jp

    inner join rs_job_posting_location jpl on jpl.id = jp.id
    inner join rs_cor_city city on city.id = jpl.city_fk
    inner join rs_organization_detail od on od.id = jp.id

    where DAY(posting_date) = DAY(GETDATE())
    order by no_of_posts Desc 

    select jp.date_updated
    from rs_job_posting jp
end

else if @start_date = 'monthly'
begin
select jp.id, city.name[City]
    , row_number() over (order by city.name) [sr_no]
    , count(jp.id) over (partition by name) as no_of_posts 
    , COUNT(od.id) over (partition by name) as no_of_employers
    ,CONVERT(varchar(12), jp.posting_date, 103) [date_created]

    from rs_job_posting jp

    inner join rs_job_posting_location jpl on jpl.id = jp.id
    inner join rs_cor_city city on city.id = jpl.city_fk
    inner join rs_organization_detail od on od.id = jp.id

    where DAY(posting_date) = DAY(GETDATE())
    order by no_of_posts Desc 

    select jp.date_updated
    from rs_job_posting jp
end

else if @start_date = 'yearly'
begin
select jp.id, city.name[City]
    , row_number() over (order by city.name) [sr_no]
    , count(jp.id) over (partition by name) as no_of_posts 
    , COUNT(od.id) over (partition by name) as no_of_employers
    ,CONVERT(varchar(12), jp.posting_date, 103) [date_created]

    from rs_job_posting jp

    inner join rs_job_posting_location jpl on jpl.id = jp.id
    inner join rs_cor_city city on city.id = jpl.city_fk
    inner join rs_organization_detail od on od.id = jp.id

    where DAY(posting_date) = DAY(GETDATE())
    order by no_of_posts Desc 

    select jp.date_updated
    from rs_job_posting jp
end

结束

【问题讨论】:

  • 如果您告诉我们错误的实际位置。谢谢
  • 我很确定这是您的过程 sp_admin_reports_city_filter_select 中发生的 SQL 错误。您可以发布程序定义吗?此错误是已选择 the wrong data type 的模式的特征,并且您将日期存储为字符串。这将继续导致问题,直到从源头得到纠正。即将日期存储为日期。

标签: c# sql sql-server


【解决方案1】:

当针对数据库运行 SQL 查询时,我通常会在查询分析器中遇到这种错误。在查询分析器中,它是一个正在被解释的字符串。

我在德国工作,欧洲的日期格式不同于美国的日期格式,这通常是 SQL Server 安装中的默认设置。问题是有时你没有注意到。

02-11-2013 是 11 月 2 日的德国日期。在美国格式中,它是 2 月 11 日。一切都很好,除了你会得到错误的保存日期。但是,如果您尝试保存 31-10-2013,则美国日期将被解释为它不理解的第 31 个月。

正如评论中提到的,问题出在您的 SP,而不是您发布的代码中。检查日期解析或 SQL 字符串构建和 .exec。

编辑:基于您的存储过程。

declare @startdate datetime
if @startdate = 'weekly'
begin
select 1
end

您不能将变量的类型设为 datetime,然后将其与 varchar 进行比较。引擎将尝试将“每周”转换为日期格式,并为您提供您所看到的错误。

【讨论】:

    猜你喜欢
    • 2012-04-17
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多