Calendar Date String Conversion in Fill Method with specific query - sql

I am using this query to generate a DataTable to display information for users to select from. The fill method I am using gives teh ff error.
Conversion failed when converting date and/or time from character
string
SELECT Time_Slot.TimeSlot, Proposed_Date.Date
FROM Time_Slot CROSS JOIN
Proposed_Date
WHERE (Time_Slot.TimeSlot_ID NOT IN
(SELECT Event_Time_ID
FROM Event_Booking
WHERE (Booking_Date = #Booking_Date)))
DTAvailableBookingsTableAdapter.FillAvailableBookings(DSEventBooking.DTAvailableBookings, MonthCalendar1.SelectionRange.Start.ToShortDateString)
Any help in resolving the error would be welcome

Many people make their own lives difficult when it comes to dates. VB.NET has a dedicated data type for dates so, if you are working with dates, use it. Don't convert anything that is not text to text, unless it is specifically for display/serialisation purposes, where only text is supported.
Presumably your Booking_Date is the appropriate data type for dates in your database, e.g. Date/Time in Access or date in SQL Server. In that case, the #Booking_Date parameter is expecting a value of that type, not text. In that case, why are you converting a value of the correct type to a value of the incorrect type here:
MonthCalendar1.SelectionRange.Start.ToShortDateString
The Start property is the correct type and you spoil it by converting it to a String. Don't. Just pass the DateTime value that you already have because that is what's expected.
MonthCalendar1.SelectionRange.Start
Coming from a MonthCalendar, it should already have the time portion zeroed so there's no need to do that yourself but, in cases where you did need to, you can get the Date property of a DateTime value to get another DateTime value with the same date and the time zeroed.
MonthCalendar1.SelectionRange.Start.Date
Note that DateTime is a .NET type and VB has a Date type that is simply an alias for that, i.e. a Date and a DateTime are the same thing. If this is confusing, it shouldn't be. Int32 is a .NET type too and Integer is a VB data type that maps to it. Do you use Integer all the time without any confusion? If so then you should use Date all the time without confusion too.

Related

Format DateTime without converting to String in SQL Server?

I have a view in SQL Server 2012 and there is a column of containing dates&times. I have been trying to convert the date column as '20/10/2018 18:00' format (no second) by using this feature and lots of approaches on Stackoverflow and other web sites:
FORMAT(StartDate, 'dd.mm.yyyy hh:mm')
However, as the data type of this column is Varchar (String) rather than DateTime, I encountered some problems in C# side and I want to perform this conversion on the database side without changing the data type of the generated format). Is there any way to achieve this?
SQL Server doesn't have a "date format" per se. The formatting of datetime fields is only performed when presenting the datetime to an output - that is, when converting it to a string.
There is a default format for presentation that is controlled by the server's collation setting. However, internally the date is stored as a numeric value (actual format varies by type, as datetime and datetime2 have different internal formats), and that value has no associated formatting.
You can store your date without seconds by using a smalldatetime field, or by manipulating the input data to trim off the seconds value. But, unless you store your date as a string, which is absolutely not recommended, you will not be able to save an output format different from the default collation-driven format in a datetime field.
I would migrate that column to a datetime (or some variant) if possible. Alternatively if that would affect too many things, you could make a computed column on the table which converts the string date you have to a datetime. That way the database doesn't have to care about the formatting at all; it just works with the proper DateTime data type.
If neither of those is an option, you can just pas the string to C# and use DateTime.TryParse() to convert it to a C# DateTime object.
In either case, it's preferable to work with the date as a DateTime up until the very last minute where you need to format it for display somewhere.

linq query convert text to date to compare with another date vb.net

I have a database field, Field260, that stores date representations as text. For instance a value for 3/29/2018 would be stored as a string "03/29/2018"
Dim Db = New DataClasses1DataContext
Return From cf In Db.FileDatas
Where cf.Field260 <= System.DateTime.Today
Returns error
System.Data.SqlClient.SqlException: 'Explicit conversion from data type text to datetime is not allowed.'
When I tried to Parse the date
Where DateTime.Parse(cf.Field260) <= System.DateTime.Today
I receive
System.NotSupportedException: 'Method 'System.DateTime Parse(System.String)' has no supported translation to SQL.'
I'm stumped.
I encountered a similar issue - have an app that I used to have a DatePicker for a field, and my database field defined as DateTime, but the users absolutely wanted to copy/paste whatever garbage they wanted into the field. Problem is, I'm allowing them to search for records by that "Date" field.
As a workaround, I do some basic validations on the garbage they enter to attempt to only allow dates (IsDate function captures most errors, but still allows them to enter years like 208 instead of 2018, so I also check that Year(DateValue(txtDate.Text))>2000, which works for me because it's an application-received date, so should never be older than this year). Then I store it as varchar in the database.
When searching for the records, you have two options - either grab all records, then perform a date conversion on the list returned to pare down the results, or hope and pray that your faulty LINQ WHERE clause will get everything you want.
Figured it out cuz I'm a genius.
Return From cf In Db.FileDatas
Where cf.Field260.ToString() <= System.DateTime.Today.ToString("MM/dd/yyyy")
Somehow comparing string to string works, who knew!

Redshift CONVERT_TIMEZONE Issue

I am facing an issue with the time conversion.
Whenever I tried to change the time to timestamp. It does not work.
I have tried many of functions from http://docs.amazonaws.cn/en_us/redshift/latest/dg/Date_functions_header.html
But still a single function did not convert the date time to timestamp. An eg. I need date time (2017-12-11 23:38:11) as timestamp (1513036800) format.
Can you please guys tell me the exact function to convert that?
Also I have tried to use CONVERT_TIMEZONE function.
It works fine, whenever I passed the static date inside this like: CONVERT_TIMEZONE('GMT','GMT -1','2017-12-11 23:38:11')
But if I change the date with my variable name, I am getting an error message:
"ERROR: function convert_timezone("unknown", "unknown", character varying) does not exist HINT: No function matches the given name and argument types. You may need to add explicit type casts."
Can you guys tell me function to convert the date into timestamp?
Can you guys tell me reason of the above error while passing the dynamic variable?
I want to add timezone and date together and then to get the timestamp value of that, is that possible?
Dateadd function gives the same error while passing the dynamic variable.
Per error message it looks like your timestamp is stored in a varchar column. You have to change the column to timestamp or convert the value on the fly to be able to use timestamp functions. I suggest the first because it has better performance. Redshift doesn't support column data type alteration, so to do this you have to recreate the table DDL with timestamp data type for time columns, insert data to the new table and replace the old table with it.
As for dynamic conversion, it's like this:
EXTRACT('epoch' FROM CONVERT_TIMEZONE('GMT','GMT -1',your_column::timestamp))
you just don't need to use ::timestamp part if you convert the column

"Input value not long enough for date format" When updating with TableAdapter

My queries are managed by a DataAdapter, so I have little to no control over it's generation.The problem query goes this way:
UPDATE MYSCHEMA.MYTABLE
SET MYDATETIME = :myDateTime
WHERE (MYKEY = :myKey)
The data access layer is automatically generated. I can't change how it works...
The type of MYDATETIMEin the database side is Timestamp.
The type of the parameter myDateTime of the DataAdapter query and the field from the dataset is DateTime.
Not sure if it should matter. Also, it seems that on the Oracle side, the hour is 24 hour but the hour managed by the app has a 12 hour base. But Why would it matter? When calling the update method, I pass Date.Now. How more legit could it be?? But it still is rejected with:
ORA-01840: Input value not long enough for date format
Any ideas?
As per comment chat, it seems that a string conversion is occurring because the parameter database type is set to varchar
Set the ProviderType of the parameter to be something more relevant to the type Oracle is expecting - it'll either be Date, DateTime, Timestamp, OracleDateTime or some variation on this theme (the names change according to whether you use .net oracle client or oracle's one) , hopefully...

Access SQL Date Format

How can I retrieve a record based on a Date property? I'm trying:
WHERE Meetings.[MDate] = '16/12/2011'
which is the format I use but I get :
"Data type mismatch in criteria expression"
Problem solved: It should have been:
WHERE Meetings.[MDate] = 16/12/2011
No quotation marks.
For where clauses use
columnName = #mm/dd/yyyy#
You'll want to use the SQL date format: '#2011-12-16#'
Use the cast to DATETIME function, CDATE(), which will honour the machine's regional settings. That said, it still a good idea to use an unambiguous date format and the ISO 8601 format is a good one.
Also note that Access doesn't have a date data type: its sole temporal data type is DATETIME and, as its name suggests, always has a time element accurate to one second time granule, even if that time happens to be midnight. Therefore, it is a good idea to always include a time value to one second time granule in all DATETIME literals e.g.
WHERE Meetings.MDate = CDATE('2011-12-16 00:00:00');
Another advantage to the above is that the Access UI will not attempt to reformat the DATETIME literal because it is held as a string.