There is a table with a column "Date" format 2018-03 in sql server. Entered year 2018 in textBox. How to display all records in a DataGridView with such a year?

  • Make a selection of the database with the parameter year. Formatting can be done both on the server and on the client. string.Format - great for string.Format with dates. - nick_n_a pm
  • with the Date column of the 2018-03 format in the sql server, SQL Server will not agree to accept the date without the Day component. How to display all records in the DataGridView with this year? By completing the selection in the query for the entered value. Either singling out a year from a date, or making up the starting and ending dates of a year. - Akina
  • @Akina I already did, everything works. You do not know how to search the tables? Suppose there are tables, each is the date of the month and year (2018-03). How to display the value of all tables in the data grid that contain the year that I enter, for example, in the textbox 2018 - ne0n
  • @Akina Just like I didn’t like it right now, I need the data I entered to be in a separate table, what year and month this is and the table is being created by me. - ne0n

2 answers 2

table with a column "Date" format 2018-03

This is not a "date". This is a regular line.

Entered in textBox year 2018

If you need to search for individual components and the “day” component is not foreseen, then break this field into two integer “year” and “month”

  • Date type in the table - ne0n
  • one
    @ ne0n Date type can not contain a day and can not be in some kind of "format" - Anton Shchyrov
  private void butFound_Click(object sender, EventArgs e) { //поиск по году if(numericUpDownMonth.Value == 0) { SqlConnection conn = new SqlConnection(MainWindow.connectionString); conn.Open(); SqlCommand cmd = new SqlCommand(string.Format("Select * From dbo.[{0}] Where YEAR(date) = {1}", MainWindow.currentDate, numericUpDownYear.Value), conn); DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); dgvArchive.DataSource = dt; conn.Close(); } else {//поиск по году и месяцу SqlConnection conn = new SqlConnection(MainWindow.connectionString); conn.Open(); SqlCommand cmd = new SqlCommand(string.Format("Select * From dbo.[{0}] Where YEAR(date) = {1} and Month(date) = {2}", MainWindow.currentDate, numericUpDownYear.Value, numericUpDownMonth.Value), conn); DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); dgvArchive.DataSource = dt; conn.Close(); } }