TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# DateTime.Parse SQL Dates

This C# article uses DateTime.Parse on date strings from SQL databases. It tests strings for correctness.

DateTime.Parse, SQL. 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. 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. 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. 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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf