繁体   English   中英

从字符串DateTime转换日期和/或时间时转换失败

[英]Conversion failed when converting date and/or time from character string DateTime

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];
}

我有一个页面,可以按城市进行报告,现在我在其上添加了一个下拉列表,以按日期(即星期,今天,月份,年份等)过滤结果

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

PS这是我的存储过程

ALTER PROCEDURE [dbo]。[sp_admin_reports_city_filter_select]-在此处添加存储过程的参数-@ id INT = NULL @start_date DateTime,@end_date DateTime AS BEGIN-声明@startdate datetime set @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

结束

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

我在德国工作,欧洲日期格式与美国日期格式不同,后者通常是SQL Server安装中的默认格式。 问题在于有时您不会注意到。

2013年2月11日是德国的11月2日。 以美国格式,它是2月11日。 除了您得到的日期错误之外,其他所有操作都没问题。 但是,如果您尝试保存2013年10月31日,则美国日期会被解释为它不理解的第31个月。

如评论中所述,问题出在您的SP中,而不是您发布的代码中。 检查日期解析或SQL字符串建立以及.exec。

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

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

您不能使用类型为datetime的变量,然后将其与varchar进行比较。 引擎将尝试将“每周”转换为日期格式,并为您显示错误。

暂无
暂无

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

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