0
votes
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 i.e. 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."

P.S. this is my stored procedure

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'

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

1
if you please update us where actually is the error. ThxSaghir A. Khatri
I'm pretty sure this is an SQL error occurring inside your procedure sp_admin_reports_city_filter_select. Can you post the procedure definition? This error is characteristic of schemas where the wrong data type has been chosen, and you are storing dates as strings. This will continue to cause problems until it is corrected at source. i.e. store dates as dates.GarethD

1 Answers

0
votes

I usually get that kind of error in the query analyzer when a SQL query is running against the DB. 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. The problem is that sometimes you don't notice.

02-11-2013 is a German date for the second of November. In the American format, it is the 11th of Feburary. 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.

As mentioned in the comment, the problem is in your SP, not in the code you posted. Check for date parsing or SQL string build up and .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. The engine will attempt to convert 'weekly' to a date format and give you the error you are seeing.