使用substring在SQL语句中分隔3个字符串

时间:2016-02-25 14:13:14

标签: c# sql asp.net

我正在尝试使用substring在我的sql语句中分隔3个字符串,并将它们显示为3个不同的标签。 目前我有这个

string sql = "SELECT TOP 1 IncType + '-' + IncCost + '-' + IncDate FROM Incomings WHERE CONVERT(DATETIME, IncDate, 103) > GETDATE() ORDER BY IncDate";
        using (SqlConnection conn = new SqlConnection("MYCONNECTION"))
        {

            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            string getValue = Convert.ToString(cmd.ExecuteScalar());

            lblNextInc.Text = (getValue);

            conn.Close();


        }

我现在只是尝试将其显示为一个字符串,但我收到错误消息"将数据类型varchar转换为float时出错。"

我不知道为什么我会这样做。最终结果将是获取两个值并将它们显示为两个不同的标签(例如,显示incCost到lblOne,incType到lblTwo,incDate到lblThree) 任何帮助是极大的赞赏!

1 个答案:

答案 0 :(得分:1)

string sql = "SELECT TOP 1 cast(IncType as nvarchar)+ '-'+ cast(IncCost as nvarchar) + '-' + cast(IncDate as nvarchar) FROM Incomings WHERE CONVERT(DATETIME, IncDate, 103) > GETDATE() ORDER BY IncDate";
        using (SqlConnection conn = new SqlConnection("MYCONNECTION"))
        {

            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            string getValue = Convert.ToString(cmd.ExecuteScalar());

            lblNextInc.Text = (getValue);

            conn.Close();


        }