C# DateTime.Parse, SQL

SQL: Structured Query Language

SQL date strings can be handed with DateTime.Parse. This is useful when you have database text-only data. Your ADO.NET provider doesn't parse the dates automatically. We provide examples of date parsing.

DateTime.Parse

Example

Program

You can use DateTime.Parse on the native date time format in the MySQL database. This is useful if for some reason you have output from the database in string format. Each DateTime.Parse call here succeeds, and returns the correct value.

Tip:Normally, MySQL dates and times are stored in columns of types DATETIME, DATE, and TIMESTAMP.

And:These value types are equivalent to the result of the SELECTs. See section 11.6, Date and Time Functions (no longer available).

C# program that parses MySQL dates

using System;

class Program
{
    static void Main()
    {
	// Taken from MySQL: SELECT CURTIME()
	//                   SELECT TIME(...)
	string mySqlTime = "23:50:26";
	DateTime time = DateTime.Parse(mySqlTime);

	// Taken from MySQL: SELECT TIMESTAMP(...)
	string mySqlTimestamp = "2003-12-31 00:00:00";
	time = DateTime.Parse(mySqlTimestamp);
	Console.WriteLine(time);

	// Taken from MySQL: SELECT CURDATE()
	//                   SELECT DATE(...)
	string mySqlDate = "2008-06-13";
	time = DateTime.Parse(mySqlDate);
	Console.WriteLine(time);
    }
}

Output

12/31/2003 12:00:00 AM
6/13/2008 12:00:00 AM

Example 2

With the examples from Microsoft SQL Server, the DateTime.Parse method will parse the string representations from the database. In SQL Server,
dates are stored in the date,
datetime2
and datetimeoffset types.

C# program that uses DateTime.Parse with SQL Server

using System;

class Program
{
    static void Main()
    {
	// Taken from SQL SERVER: SELECT GETDATE()
	//                        SELECT GETUTCDATE()
	string sqlServerDate = "2007-04-30 13:10:02.047";
	DateTime time = DateTime.Parse(sqlServerDate);
	Console.WriteLine(time);

	// Taken from SQL SERVER: SELECT SYSDATETIME()
	//                        SELECT SYSUTCDATETIME()
	string sqlSysDate = "2007-04-30 20:10:02.0474381";
	time = DateTime.Parse(sqlSysDate);
	Console.WriteLine(time);

	// Taken from SQL SERVER: SELECT SYSDATETIMEOFFSET()
	string sqlSysDateOffset = "2007-04-30 13:10:02.0474381 -07:00";
	time = DateTime.Parse(sqlSysDateOffset);
	Console.WriteLine(time);
    }
}

Output

4/30/2007 1:10:02 PM
4/30/2007 8:10:02 PM
4/30/2007 1:10:02 PM

Discussion

Format

When using the valid date formats from MySQL and SQL Server, you do not need to specify a format string or use ParseExact on the DateTime struct. In other words, you can simply use DateTime.Parse to get the dates from the SQL strings.

Note:In some cases this is not possible, and you will want to use a format string and ParseExact.

DateTime Format

Summary

Framework: NET

We parsed MySQL and SQL Server 2005 date strings using DateTime.Parse. We checked the input and output against the MSDN and MySQL developer sites. You can use DateTime.Parse in its default overload to accomplish this task.

Tip:This is needed when you are not using an ADO.NET provider that converts the dates.


C#: Time