SQL查询格式化日期数据类型以排除时间

时间:2019-01-23 04:45:34

标签: c# sql date

当在另一个列表框中选择一个帐户交易时,我试图用SQL数据库中的日期填充一个列表框。现在,日期显示的是时间和日期。我在日期数据表中使用了日期数据类型,但是我不确定如何格式化日期以排除时间。

 try
        {
            string query = "select a.Date from Date a inner join AccountTransaction act on" +
                " a.Id = act.DateId where act.AccountId = @AccountId";

            SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);

            using (sqlDataAdapter)
            {
                sqlCommand.Parameters.AddWithValue("@AccountId", AccountList.SelectedValue);
                DataTable associatedDateTable = new DataTable();
                sqlDataAdapter.Fill(associatedDateTable);
                DateList.DisplayMemberPath = "Date";
                DateList.SelectedValuePath = "Id";
                DateList.ItemsSource = associatedDateTable.DefaultView;
            }
        }
        catch (Exception e)
        {
            // MessageBox.Show(e.ToString());
        }

1 个答案:

答案 0 :(得分:0)

DateTime没有格式。这只是个约会。如果要以特定方式显示它,则必须对其进行转换。

select getdate()

给你:2019-01-23 12:51:03.663

select CONVERT (varchar(10), getdate(), 103) AS [DD/MM/YYYY]

为您提供23/01/2019

string query = "select CONVERT (varchar(10), a.Date, 103) AS [DD/MM/YYYY] from Date a inner join AccountTransaction act on" +
                " a.Id = act.DateId where act.AccountId = @AccountId";
相关问题