Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8574349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:27:17+00:00 2026-06-11T19:27:17+00:00

On my SQL server I have a very simple table for testing, which only

  • 0

On my SQL server I have a very simple table for testing, which only contains three rows:
ID, Date and Hours.(varchar, DateTime, varchar).

The DateTime format in SQL is like: yyyy-MM-dd HH:mm:ss.fff.

The DateTime format in C# is like: yyyy-dd-MM HH:mm:ss.fff.

I use the following code to get C# DateTime format:

 string ddd = "2012-10-10 00:00:00.000";
 dt1 = DateTime.ParseExact(ddd, "yyyy-MM-dd HH:mm:ss.fff", null);

If I try to make it into yyyy-dd-MM I get an error:

The DateTime, which is represented by the string, isn’t supported by
the calender.

In my C# application is am trying to find total amount of hours between sunder dates. I sort of got it to working, but it only works for dates between 01 and 12.

So like from 2012-01-10 to 2012-10-10 (ten days) will give me the correct amount of total hours from the database.

But when I write 2012-01-10 to 2012-14-10 (fourteen days) I get an error:

The conversion of a char data type to a datetime data type resulted
in an out-of-range”

Thank you in advance.

PS. Can you suggest a easier way to get dates?

mySQL query

string CommandText = "SELECT * FROM date_test WHERE id = '4' AND date BETWEEN '" + dt1 + "' AND '" + dt2 + "'";

I have figured out the problem but not the solution.

The problem is that SQL database looks at the format, and wants yyyy-MM-dd, but C# can only send yyyy-dd-MM.

Why cannot ParseExact() do yyyy-MM-dd?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:27:18+00:00Added an answer on June 11, 2026 at 7:27 pm

    The DateTime, which is represented by the string, isn’t supported by the calender.

    This error is being given because your C# application views the date 2012-14-10 as saying the 14th month, 10th day, and 2012th year. The day and year work find, but the month doesn’t. Further, don’t try and change how your C# application views the date, that’s based off the culture of the system.

    You’re confusing how to define a DateTime object and how to display one.

    Since you’re storing your date as a DateTime in SQL, there’s not a good reason for me to believe that you would need to do any kind of parsing. Consider the below code sample.

    var dataTable = new DataTable();
    var dataAdapter = new SqlDataAdapter("SELECT * FROM YourTable", "{connection string}");
    
    dataAdapter.Fill(dataTable);
    
    var yourDate = dataTable.Rows[0]["Date"]; <=== the type will be DateTime, simple.
    

    Adding Parameters

    Let’s take your example query:

    “SELECT * FROM date_test WHERE id = ‘4’ AND date BETWEEN ‘” + dt1 + “‘ AND ‘” + dt2 + “‘”;

    And let’s fix it a bit, consider the below example:

    var dataTable = new DataTable();
    var dataAdapter = new SqlDataAdapter("SELECT * FROM date_test WHERE id = @ID AND date BETWEEN @StartDate AND @EndDate", "{connection string}");
    
    dataAdapter.SelectCommand.Parameters.AddWithValue("@ID", "4");
    dataAdapter.SelectCommand.Parameters.AddWithValue("@StartDate", new DateTime(2012, 10, 1));
    dataAdapter.SelectCommand.Parameters.AddWithValue("@EndDate", new DateTime(2012, 10, 14));
    
    dataAdapter.Fill(dataTable);
    
    var yourDate = dataTable.Rows[0]["Date"]; <=== the type will be DateTime, simple.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To keep things very simple, say I have a SQL Server 2008 table with
I'm working on SQL server 2005 and I have a very simple stored procedure:
I am using SQL Server 2008 Enterprise. I have created a very simple test
I'm very very new to sql server. I want to have a table with
I have a very large table in SQL Server 2008. It has lots of
I have a very simple select on SQL Server: select * from person where
I have a very simple question about transactions. (In sql server 2000, but I
I have a very simple SQL table that I want to import into Solr
I have this very simple query in my database in SQL Server 2008 R2:
I have an SQL Query (For SQL Server 2008 R2) that takes a very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.