查询在任一条件下提取数据

时间:2013-11-28 18:22:21

标签: c# sql sql-server

在这段代码中一切正常,这是我唯一的问题,当我从下拉单击“今天”并且今天没有数据在DB中时,它仍在提取数据,就好像它从中获取值“txtCity”。只有当我在txtCity中放入一些东西而不是我在DB中的东西时它返回空

        string strVal = hdnOption.Value;
        IFormatProvider provider = new System.Globalization.CultureInfo("en-GB", true);
        DateTime dtStart = new DateTime();
        DateTime? dtEnd = null;
        string strCity = null;

        if (strVal == "0")
        {
            HideCustomSearch();
            dtStart = DateTime.Today;
            dtEnd = DateTime.Today;

            if (txtCityName != null)
            {
                strCity = txtCityName.Text.ToString().Trim();
            }
        }
        if (strVal == "today") 
        {
            HideCustomSearch();
            dtStart = DateTime.Today;
            dtEnd = DateTime.Today;
            if (txtCityName != null)
            {
                strCity = txtCityName.Text.ToString().Trim();
            }
        }
        if (strVal == "weekly")
        {
            HideCustomSearch();
            dtStart = DateTime.Now.AddDays(-7).Date;
            dtEnd = DateTime.Today;
            strCity = txtCityName.Text.ToString().Trim();
        }
        if (strVal == "byweekly")
        {
            HideCustomSearch();
            dtStart = DateTime.Now.AddDays(-15).Date;
            dtEnd = DateTime.Today;
            strCity = txtCityName.Text.ToString().Trim();
        }
        if (strVal == "monthly")
        {
            HideCustomSearch();
            dtStart = DateTime.Now.AddMonths(-1).Date;
            dtEnd = DateTime.Today;
            strCity = txtCityName.Text.ToString().Trim();
        }
        if (strVal == "yearly")
        {
            HideCustomSearch();
            dtStart = DateTime.Now.AddYears(-1).Date;
            dtEnd = DateTime.Today;
            strCity = txtCityName.Text.ToString().Trim();
        }
        if (strVal == "custom")
        {
            ShowCustomSearch();
            //this.txtdtStart = "22/10/2010";
            //dtStart = DateTime.Parse("07-01-2013", provider,                System.Globalization.DateTimeStyles.NoCurrentDateDefault);
            string d = "07-01-2013";
            dtStart = DateTime.ParseExact(d, "dd-MM-yyyy", null, System.Globalization.DateTimeStyles.AllowWhiteSpaces);
            dtEnd = DateTime.ParseExact(d, "dd-MM-yyyy", null, System.Globalization.DateTimeStyles.AllowWhiteSpaces);
            strCity = txtCityName.Text.ToString().Trim();

            hdndtStart.Value = txtdtStart.ToString();
            hdndtEnd.Value = txtdtEnd.Text.ToString();
        }
        FillGridFilter(dtStart, dtEnd, strCity);

P.S。对不起,我忘了添加查询

if @start_date = DAY(getdate()) and @end_date = null and @city = null
    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) = @start_date
order by no_of_posts Desc 

END

1 个答案:

答案 0 :(得分:1)

试试这个

if (@start_date = DAY(getdate()) AND @end_date IS NULL AND @city IS NULL)
  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) = @start_date
    GROUP BY jp.id, city.name, ,CONVERT(varchar(12), jp.posting_date, 103)
    order by no_of_posts Desc 
 END
在SQL Server NULL中的

UNKNOWN Value所以你真的无法将未知值与任何东西进行比较,在SQL Server中检查NULL时你应该使用IS NULLIS NOT NULL ,比较运算符(<,>,<>,< =,> =)不能使用NULL值