Going mad with date in SQL and c# [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am developing a C# application, and I have a date column in SQL Server.
I am performing a query based on date, using DateTimePicker. I have changed the format of datetime picker to short, now it displays date as mm-dd-yyyy
Now while executing I get correct output from date 1-12 but as soon as I select 13 date I get error as
conversion failed when converting date/time from character string
I don't know what is happening!

I suspect you're using the formatted string from that DateTimePicker to build an SQL query on the fly via format strings or concatenation.
Don't do that.
Date/time string literals in SQL tend to use a sane format (i.e. ISO 8601) where the order of the parts differs. The fact that it blows up when you change a piece from 12 to 13 should give it away, actually.
An SqlCommand has parameters (you can insert in the query with #foo. Using those will ensure that everything gets passed in the right way and properly quoted:
using (SqlCommand c = connection.CreateCommand()) {
c.CommandText = "Select roll_number,name from Attendance_table where date=#date";
c.Parameters.AddWithValue("date", dateTimePicker1.Value);
using (var r = c.ExecuteReader()) {
...
}
}
(roughly – you should consult the documentation for proper usage, it's been a while that I wrote such things)

Related

Convert DateTime Format with Millisecond in C# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
This is date time format i want "2013-06-25 18:46:54.687" to pass in to sql server.
How to convert in C#?
DateTime LastUpdateTime=Convert.toDateTime(LastUpdateTime);
//2013-06-25 18:46:54.687 with 3 index of millisecond
sc.Parameters.Add("#LastUpdateTime", SqlDbType.DateTime).Value = LastUpdateTime;
You're using the wrong Data Typ in SQL.
The datetime2 can be considered as an extension of the existing datetime type that has a larger default fractional precision, and optional user-specified precision.
C# Format Milliseconds exactly the way you want.
In an Example:
DateTime date2 = new DateTime(2008, 1, 1, 0, 30, 45, 125);
Console.WriteLine("Date: {0:o}",
date2);
// Displays the following output to the console:
// Date: 2008-01-01T00:30:45.1250000
Look at Why is SQL Server losing a millisecond? and DateTime2 vs DateTime in SQL Server
These are great Question with good Answers, BTW.
If your database query parameter is string, use following format:
LastUpdateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")
Otherwise, it would be better to send datetime as original object,
and let sql server do all conversions.

how to check if sql table date in type string is lower then today's date in string C# winform [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a table with a date but it is in type string not type date (dd/mm/yyyy).
I want to get from the table all of the rows that the date is lower then today's date,
I have today's date in string and the date in the table in string,
so how do i check who is bigger?
FOR SQL SERVER
You need to cast it to date in order for you to compare the records.
SELECT *
FROM TableName
WHERE convert(date, [date], 103) < GETDATE()
SQLFiddle Demo
FOR MYSQL
You need to convert those values into date datatype using STR_TO_DATE in MySQL.
SELECT *
FROM TableName
WHERE STR_TO_DATE(date, '%d/%m/%Y') < CURDATE()
SQLFiddle Demo
STR_TO_DATE

can storing data in a database sometimes lead to corrupted data? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a field that's stored in the database as a string. It's actually a comma-separated string of numbers that I convert to a list of longs. The lines of code that do the conversion look somewhat like this:
TheListOfLongs = (from string s in StringFromDB.Split(',')
select Convert.ToInt64(s)).ToList<long>();
The code that creates the database storage string looks like this:
return String.Join(",", TheListOfLongs.Select(x=> x.ToString()).ToArray());
This works fine but as you can see, if for some reason there's a problem with the string, the code in the first line of code breaks on Convert.ToInt64(s).
Now I know I can wrap all this around a try statement but my question is this: can storing and retrieving a string in the database corrupt the string (in which case I definitely need the try statement) or is this a one a trillion odd type of event?
I wouldn't worry about corrupt data per se. However, you definitely need to handle the more general case where you can't parse what should be numeric data.
Even in the most controlled situations it is good programming practice to provide conditions for when you can't process data as you're expecting to be able to. What that means to your application is something you'll need to decide. Wrapping the statement with a try..catch will prevent your application from choking, but may not be appropriate if the parsed list is critical later on.
Selecting from the DB shouldn't corrupt your string.
If the connection is dropped mid transfer or something like that then an exception is thrown.

Changing date-time format in c# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Creating digital clock. I'm newbie to C#. My code look like that
timeLbl.Text = DateTime.Now.ToLongTimeString();
dateLbl.Text = DateTime.Now.ToLongDateString();
Here is, the result
http://content.screencast.com/users/TT13/folders/Jing/media/da6d1f65-bf5f-4735-97dc-70485112a998/2012-07-02_1826.png
I got some questions:
Can I change time's format to 24 Hour? how?
How to change date into digits only format (like dd/mm/yyyy) or this result but in exact language (I mean, words "Monday, July" in another language, which windows support, for ex Turkish)?
How to make window dynamically change it's width (depending on text
length)?
Please help me,to achieve these 3 things. Thx in advance
Ans 1:
timeLbl.Text = DateTime.Now.ToString("HH:mm:ss");
Will convert time to 24 hour format.
Ans 2:
dateLbl.Text = DateTime.Now.ToString("dd/MM/yyyy");
Will convert date format to 31/06/2012
More formats here
The first and second point where been answered, for the last point set SizeToContent="WidthAndHeight" on the window and the window will dynamically resize based on its content size. I assume ur working with wpf, else it doesnt works!

Convert timestamp (database) to DateTime (C#) [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
In my SQL Server table I have a column of datatype timestamp and I want to convert to DateTime in C#.
That's not possible.
"The timestamp data type is just an incrementing number and does not
preserve a date or a time."
http://msdn.microsoft.com/en-us/library/ms182776%28v=SQL.90%29.aspx
You can't. The timestamp data type is an alias for rowversion; its a binary incrementing value, its not composed of nor based upon a date & time.
The timestamp type is not a representation of a date or time but simply a generated number that is guaranteed to be unique in the database. As such, it can't be converted directly.
The only way to perform a rough conversion would be to periodically store the current time along with a timestamp in a new table, and then select which time is closest to the timestamp you want to convert from.
Alternatively you might want to add a new column to your table of type datetime, and update this whenever a row changes.
The Transact-SQL timestamp data type is a binary data type with no time-related values....., a timestamp is neither a date nor a time.So, you can't do..... See BOL:
timestamp:
timestamp is a data type that exposes automatically generated binary numbers, which are guaranteed to be unique within a database. timestamp is used typically as a mechanism for version-stamping table rows. The storage size is 8 bytes.

Categories