将char数据类型转换为datetime数据类型会导致超出范围的datetime值错误

时间:2015-12-09 05:15:07

标签: c# sql asp.net datetime sql-server-2005

我将"09/10/2014"日期添加到文本框中,然后点击提交按钮,但收到的错误为: -

  

将char数据类型转换为datetime数据类型会导致日期时间值超出错误。

以下是调试时生成的查询: -

select * from WMS_BIN_STATUS_TRACK where 1!=1 or Current_Item_Exp_Dt = convert(datetime, '09/10/2014', 103)

及以下是完整代码: -

protected void btnTrack_OnClick(Object sender, EventArgs e)
{
    string whereClause = "1!=1";

    if (ddlBin.SelectedValue != "0")
    {
        whereClause = whereClause + "or location_name='" + ddlBin.SelectedValue + "'";
    }
    if (ddlItem.SelectedValue != "0")
    {
        whereClause = whereClause + "or Current_Item_code='" + ddlItem.SelectedValue + "'";
    }
    if (txtBatch.Text != "")
    {
        whereClause = whereClause
            + " or Current_Item_Batch " + (ddlmathsign.SelectedValue == "Equal" ? (" = '" + txtBatch.Text + "'") : (" like '%" + txtBatch.Text + "%'"));
    }
    if (txtExpCal.Value != "")
    {
        whereClause = whereClause + "or Current_Item_Exp_Dt " + (ddlAssignvalue.SelectedValue == "Greater than" ? ">" : (ddlAssignvalue.SelectedValue == "Less than" ? "<" :
                  (ddlAssignvalue.SelectedValue == "Equal to" ? "=" : (ddlAssignvalue.SelectedValue == "Greater than equal to" ? ">=" : "<=")))) + "convert(datetime, '" + txtExpCal.Value + "', 103)";
    }

    if (ddlBin.SelectedValue == "0" && ddlItem.SelectedValue == "0" && txtBatch.Text == "" && txtExpCal.Value == "")
    {
        BindGrid();
    }

    else
    {
        string query = "select * from WMS_BIN_STATUS_TRACK where " + whereClause;

        SqlDataAdapter da = new SqlDataAdapter(query, strConnString);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GrdBinStockTracker.DataSource = dt;
        GrdBinStockTracker.DataBind();
    }
}

注意由于我正在处理本地条件只是为了测试, SQL注入不是一个值得关注的问题

此外,这是一个与不同日期时间相关的问题。 ?

2 个答案:

答案 0 :(得分:2)

问题在于Current_Item_Exp_Dt列中的数据。

这是一种重现它的方法。

create table #tt(dd char(10))
insert into #tt values('13/13/2014')

select 1 from #tt where dd = convert(datetime, '09/10/2014', 103)

您可以使用此查询来识别日期格式不正确的记录。

SELECT Current_Item_Exp_Dt FROM WMS_BIN_STATUS_TRACK WHERE ISDATE(Current_Item_Exp_Dt)=0

这是可行的解决方法。将您where conidtion更改为..

WHERE (CASE WHEN ISDATE(Current_Item_Exp_Dt)=1 
               THEN Current_Item_Exp_Dt 
               ELSE NULL END) = CONVERT(DATETIME, '09/10/2014', 103)

您的最终查询应为

SELECT Current_Item_Exp_Dt 
FROM WMS_BIN_STATUS_TRACK 
WHERE 1!=1 or (CASE WHEN ISDATE(Current_Item_Exp_Dt)=1 
               THEN Current_Item_Exp_Dt 
               ELSE NULL END) = CONVERT(DATETIME, '09/10/2014', 103)

答案 1 :(得分:2)

,因为您的专栏为varchar,无法更改为datetime 您还需要将该列转换为条件的日期时间。

请检查以下查询

    whereClause = whereClause + "or convert(datetime,Current_Item_Exp_Dt,103) " 
    + (ddlAssignvalue.SelectedValue == "Greater than" ? ">" : 
     (ddlAssignvalue.SelectedValue == "Less than" ? "<" : 
    (ddlAssignvalue.SelectedValue == "Equal to" ? "=" : 
    (ddlAssignvalue.SelectedValue == "Greater than equal to" ? ">=" : "<=")))) + 
"convert(datetime, '" + txtExpCal.Value + "', 103)";
  

如果您不确定Current_Item_Exp_Dt是否始终包含有效日期   您可以根据DarkNight使用以下查询构建

   whereClause = whereClause + "or (CASE WHEN ISDATE(Current_Item_Exp_Dt)=1 
           THEN convert(datetime,Current_Item_Exp_Dt,103)  ELSE NULL END) " 
    + (ddlAssignvalue.SelectedValue == "Greater than" ? ">" : 
     (ddlAssignvalue.SelectedValue == "Less than" ? "<" : 
    (ddlAssignvalue.SelectedValue == "Equal to" ? "=" : 
    (ddlAssignvalue.SelectedValue == "Greater than equal to" ? ">=" : "<=")))) + 
"convert(datetime, '" + txtExpCal.Value + "', 103)";