mercredi 13 juillet 2016

Incorrect syntax near the keyword 'FROM' when extracting year from datetime

I am trying to extract 'YEAR' from a specific date time using a SQL query and ASP.NET C# but I get

Incorrect syntax near the keyword 'FROM'

I tried to figure out for so many days but I could not find the answer. Can anyone tell me what is wrong with my SQL query?

String theDate="";
String sQuery = "SELECT EXTRACT(YEAR FROM StartDateTime) AS MyDates FROM Date WHERE DateID='1'";

SqlConnection conn = new SqlConnection(_connStr);
SqlCommand cmd = new SqlCommand(sQuery, conn);

conn.Open();

SqlDataReader dr = cmd.ExecuteReader();

while(dr.Read())
{
    theDate=dr["MyDates"].ToString();
}

conn.Close();
dr.Close();

Aucun commentaire:

Enregistrer un commentaire