简体   繁体   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];
}

i have a page on which i have report by city, now i added a dropdown on it to filter results by date ie week, today, month, year etc 我有一个页面,可以按城市进行报告,现在我在其上添加了一个下拉列表,以按日期(即星期,今天,月份,年份等)过滤结果

everything else is working, but there is something wrong with this piece of code becasue its giving me error "Conversion failed when converting date and/or time from character string." 一切正常,但是这段代码有问题,因为它给我一个错误“从字符串转换日期和/或时间时转换失败。”

PS this is my stored procedure PS这是我的存储过程

ALTER PROCEDURE [dbo].[sp_admin_reports_city_filter_select] -- Add the parameters for the stored procedure here --@id INT = NULL @start_date DateTime , @end_date DateTime AS BEGIN --declare @startdate datetime set @start_date = '2999-01-01' 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

end 结束

I usually get that kind of error in the query analyzer when a SQL query is running against the DB. 当针对数据库运行SQL查询时,我通常会在查询分析器中遇到这种错误。 From the query analyzer it is a string that is being interpreted. 在查询分析器中,它是一个正在解释的字符串。

I work in Germany and European date formats are different than the US ones that are often a default in a SQL Server install. 我在德国工作,欧洲日期格式与美国日期格式不同,后者通常是SQL Server安装中的默认格式。 The problem is that sometimes you don't notice. 问题在于有时您不会注意到。

02-11-2013 is a German date for the second of November. 2013年2月11日是德国的11月2日。 In the American format, it is the 11th of Feburary. 以美国格式,它是2月11日。 All fine other than you would get the wrong date saved. 除了您得到的日期错误之外,其他所有操作都没问题。 However if you try to save 31-10-2013 the US date is interpreted as the 31th month which it doesn't understand. 但是,如果您尝试保存2013年10月31日,则美国日期会被解释为它不理解的第31个月。

As mentioned in the comment, the problem is in your SP, not in the code you posted. 如评论中所述,问题出在您的SP中,而不是您发布的代码中。 Check for date parsing or SQL string build up and .exec. 检查日期解析或SQL字符串建立以及.exec。

Edit: Based on your stored procedure. 编辑:基于您的存储过程。

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

You cannot have a variable be of type datetime and then compare it with a varchar. 您不能使用类型为datetime的变量,然后将其与varchar进行比较。 The engine will attempt to convert 'weekly' to a date format and give you the error you are seeing. 引擎将尝试将“每周”转换为日期格式,并为您显示错误。

暂无
暂无

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

相关问题 从字符串转换日期和/或时间时,DateTime转换失败 - DateTime conversion failed when converting date and/or time from character string 将C#datetime发送到SQL Server-错误“从字符串转换日期和/或时间时转换失败” - Send C# datetime to SQL Server - error “Conversion failed when converting date and/or time from character string” 从字符串 SQL (c#) 转换日期或时间时,日期时间转换失败 - datetime conversion failed when converting date or time from character string SQL (c#) 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and /or time from character string 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and /or time from character string 从字符串错误4转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string error 4 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string 错误“从字符串转换日期和/或时间时转换失败” - Error “Conversion failed when converting date and/or time from character string” 问题:从字符串转换日期和/或时间时转换失败 - Problem: Conversion failed when converting date and/or time from character string 错误:从字符串转换日期和/或时间时转换失败 - Error: Conversion failed when converting date and/or time from character string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM