【问题标题】:The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value error将 char 数据类型转换为 datetime 数据类型导致超出范围的 datetime 值错误
【发布时间】:2016-03-14 06:41:00
【问题描述】:

我将日期作为"09/10/2014" 添加到文本框中并单击提交按钮,但出现错误:-

将 char 数据类型转换为 datetime 数据类型导致了超出范围的 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 注入在这里不是问题

另外,这是与不同日期时间有关的问题吗? ?

【问题讨论】:

  • sqlserver?或任何其他数据库?
  • 尝试将查询中的Current_Item_Exp_Dt 也转换为103 格式。
  • 确保Current_Item_Exp_Dt 列中有正确的数据。
  • @Sachu:是的,它是sql-server-2005。对不起,我忘了提这个
  • 试试这个convert(date,Current_Item_Exp_Dt,103) = convert(date, '09/10/2014', 103)

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


【解决方案1】:

问题在于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)

【讨论】:

  • 简而言之,我需要进行哪些更改。因为每次用户都可以在文本框中插入任何日期并检查结果
  • @coder :将Current_Item_Exp_Dt 列的数据类型更改为date。您应该只在表中插入有效日期。问题不在于当前输入,而在于表中的现有数据。
  • 我无法更改数据类型,如果代码有任何更改,我可以这样做。
  • 您好,先生,谢谢您的回答,如果可能的话,请用我的代码安排。!
  • @coder :我希望我能帮助你,但通过 C# 代码需要时间。我已经用您需要使用的最终查询更新了答案。希望有帮助!
【解决方案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)";

【讨论】:

    猜你喜欢
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 2012-04-16
    相关资源
    最近更新 更多