time portion from datetime value in LINQ - winforms

I have field which is of type DateTime.
When inserting a record, I am also providing the time into the field.
When reading that field with LINQ, it will return me the correct date but the time values has the default, 12:00:00 AM
In the database, I have values with date and time for each record which is correct. The problem is that the time changes to default when reading/databinding with LINQ.
dataGridView1.DataSource = IQueriableObject.Select(q => new
{AppointmentTime = q.AppointmentTime.Value.Date.TimeOfDay}) ;
What solution is there for extracting the time portion?

This is what is hurting you
AppointmentTime = q.AppointmentTime.Value.Date.TimeOfDay
When you do q.AppointmentTime.Value.Date it gets just the Date portion of the Value, by Default the TimeOfDay Property on a Date is 12:00:00 AM.
Instead you should do q.AppointmentTime.Value.TimeOfDay (this will only work if value is a type DateTime).

Related

sql datetime converted to specific day if between certain times

I have a string of datetime records. I'd like to make a case condition or if statement which reads the datetime value and assigns this to a specific day.
The day range is 2:45PM up to the next day at 2:45PM. This range would be considered the 'next day'
Ex. Time of post is 3:42 PM 11/12/2016, therefore this would read as 11/13/2016. If this post was made at 2:42 PM 11/12/2016, then this would have read 11/12/2016.
So far don't believe CONVERT(date, 'datetime') will work due to time range constraints.
Thanks
Use DATEADD function to add time to the date so it gets to the next day. For 2:45PM you'll need to add 9:15.
Think I got it...check if datetime time value is above 2:45, if so, then convert to date and add a day, else convert to date.
CASE
WHEN CAST (MAIN.TimeOfRftDecision AS Time) > '14:45:00' THEN DATEADD(dd,1,CONVERT (Date, MAIN.TimeOfRftDecision))
ELSE CONVERT (Date, MAIN.TimeOfRftDecision)
END AS 'DayOfRft'

Should I use the Date or DateTime SqlDbType when adding a DateTime C# value for a Date SQL Server field?

I've got a table with a field of Date data type. Although I would expect just the raw date in that field when inserting a value into it like so:
update reportsgenerated
set begindate = '2016-02-01'
where rptgenid = 2;
...it actually sems to store a date time value, as it is represented in LINQPad as "2/1/2016 12:00:00 AM".
The C# value I'm assigning to the parameter when inserting is a DateTime type (as C# doesn't have a "Date"-only type):
DateTime begDate = // bla
That being the case, should I designate the parameter as a Date type, to match the table:
insertRptsGenerated.Parameters.Add("#BeginDate", SqlDbType.Date).Value = begDate;
...or as a DateTime type, to match the assigned value's type:
insertRptsGenerated.Parameters.Add("#BeginDate", SqlDbType.DateTime).Value = begDate;
?
.NET already knows what type it is in .NET. You are being asked what type to use on the other end, hence the SqlDb part of the SqlDbType ;-). You should match, as closely as possible, the type of the parameter on the database side.
For the complete list of mappings, please see the MSDN page for SQL Server Data Type Mappings.
That chart shows:
SQL Server DATE == .NET DateTime == SqlDbType.Date

What should be a default datetime value?

I am inserting Excel Sheet records in my DataTable in c# and passing this DataTable to an SQL stored procedure. In my c# code I have put certain checks for empty data cells of Excel sheet to avoid Exceptions. But It seems like I am missing something while giving a default value for my SQL Date field.
string InvoiceDate = (row.Cells[3].Text == " ") ? "0/00/0000 00:00:00 AM" : (row.Cells[3].Text);
And I get the following error:
String was not recognized as a valid DateTime.Couldn't store
<0/00/0000 00:00:00 AM> in InvoiceDate Column. Expected type is
DateTime.
Edited -
Declaration of SQL field [InvoiceDate]
[InvoiceDate] [date] NOT NULL
Please don't suggest inserting null as I cannot Insert null for this column.
First, There is no 00/00/0000 date, not in the real world and not in sql server.
Second, why do you even need a default values? just use null instead.
Third, use ISO 8601 format for specifying dates in strings (yyyy-mm-ddThh:mm:ss)
Forth, As Giorgi rightfully pointed out in his comment, why even use strings for a datetime value? use a variable of type DateTime in c#. note that it's min value is different then the sql server DateTime data type min value.
If your datetime column is not nullable, you can use 1753-01-01 (min value for datetime) or 9999-12-31 (max value for datetime)
One last thing, you might want to consider using either datetime2 or separate the date and time to different columns (data types date and time, of course). Why? read here.
Try to insert the current date instead:
string InvoiceDate = string.IsNullOrEmpty(row.Cells[3].Text) ? DateTime.Now.ToString() : (row.Cells[3].Text);

SSRS changing values based upon other values

I have ran into a problem with my report.
This drop down needs to adjust the values in the PeriodStart/PeriodEnd
When the Weekly Period is changed, these values need to change. Although, I want to keep the default values before Weekly Period is changed.
So lets say that the drop down has its value as 10/1/12 | 10/7/12. I am assuming I will change the expression of the two date fields and do some kind of split on the drop down value, but I am not sure what that would be. This is where I need guidance. Thanks
I am not near Report Builder right now, but it should go something like this:
1) make your weekly period drop down a datetime field with the value of the start date, and the label in whatever format you like.
2) set your period start date parameter to be a datetime field, and in the Default Values tab, set the default value to be the expression =Parameters!WeeklyPeriod.Value
3) set your period end date parameter to be a datetime field, and in the Default Values tab, set the default value to be the expressions =DateAdd("d", 7, Parameters!WeeklyPeriod.Value)

database field with type timestamp is not updating

Duplicate of database timestamp value is not
updating
I have a variable which contains the date and time in 0000-00-00 00:00:00 format but when i put this variable in database field with type timestamp .. it is still 0000-00-00 00:00:00
Make sure that you are using the right format of the datetime. Mysql default datetime format is YYYY-MM-DD H:i:s. But, it seems that you are trying to insert the datetime value with the format of (YYYY:MM-DD H:i:s). It does not allow to insert the datetime value, so the inserted value becomes 0000-00-00 00:00:00.
Try by changing your calendar format to (YYYY-MM-DD H:i:s). I assume that you are using datatime datatype for the Valid_till column. Click the below to know more about mysql data structure.
Mysql Date format

Resources