"Input value not long enough for date format" When updating with TableAdapter - vb.net

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...

Related

SQL: date type column with only mm-dd

I want to create a column of data type having only 'mm-dd' values.
Is it possible and if yes how should I do it?
Note: Instead of "2022-06-07", I want "07-06"
There is no date type that can store that format - in fact none of the date types store a date and/or time in any of the formats you typically recognize.
For your specific requirement, that looks like a char(5) for the data type, but how you constrain it so that it will only accept valid date values, I have no idea. You'd think this would work:
CHECK (TRY_CONVERT(date, string_column + '-2022', 105) IS NOT NULL)
But what about leap years? February 29th is sometimes valid, but you've thrown away the only information that can make you sure. What a bunch of mess to store your favorite string and trust that people aren't putting garbage in there.
Honestly I would store the date as a date, then you can just have a computed column (or a column in a view, or just do this at query time:
d_slash_m_column AS CONVERT(char(5), date_column, 105)
Why not just in your query (or only in a view) say:
[output] = CONVERT(char(5), data_in_the_right_type, 105)
?
I'd personally stay away from FORMAT(), for reasons I've described here:
FORMAT() is nice and all, but…
FORMAT is a convenient but expensive function - Part 1
FORMAT is a convenient but expensive function - Part 2
You can use the SQL Server FORMAT function:
FORMAT(col1, 'dd/MM')
Check the demo here.
In such cases using char or varchar is not the best option as in those cases the underlying DB constraints that validate the integrity of the data do not kick in.
Best option is to use an arbitrary year and then put in a proper date, so for example for storing 01-Jan, the db column should store proper date with year as any arbitrary value, e.g. 2000. So your db should say 2000-01-01.
With such a solution you are still able to rely on the DB to raise an error if you tried month 13. Similarly sorting will work naturally as well.

Calendar Date String Conversion in Fill Method with specific query

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.

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!

converting date/time format in SQL

My question is similar to some other questions here in Stackoverflow, but none of them worked for me. I have date and time column in the format 1991-04-01 00:00:00.000, and I want to convert it to this format: 19910401
I have tried this code below:
Update [TestFamily].dbo.List
set dbo.List.DtBirth = convert(
varchar(10), cast(dbo.List.DtBirth as
datetime ),112)
after executing it says 320104 rows affected , but actually nothing has changed , the date format remains the same.
Every suggestion will be appreciated.
DateTime data in SQL Server doesn't have a format. They are just values that are only changed to a certain format when you display them. So it is taken into account when you view the results of a query in SSMS, show them in an app, report, etc.
Displaying dates is almost always the job of the display layer, not the database. I would strongly suggest making this change at that level (report, app, etc.) rather than at the SQL level. Otherwise, you'll have issues with sorting, filtering, etc.
I think your problem is that you are trying to use an update. SQL updates change values, not data types. You are trying to create a different datatype, a char or varchar.
The field dbo.List.DtBirth is presumably a datetime, and your update does not change that. You could add another field, perhaps called DTBirthAsVarChar varchar(8), then update that based on the date field.
Update [TestFamily].dbo.List
set dbo.List.DTBirthAsVarChar = convert(
varchar(10), cast(dbo.List.DtBirth as
datetime ),112)
You also don't need both a convert and a cast in the same statement, they basically do the same thing.

how to retrieve date and time separately from the currently selected value in datetimepicker?

i want to know the way to extract date and time separately from the currently selected value in datetime picker control??
if you just want them for display purposes, you can to the following:
DateTimePicker.Value.ToShortDateString() - will give you a date string
DateTimePicker.Value.ToShortTimeSttring() - will give you time string
DateTimePicker.Value returns a DateTime, so you can use the Date and TimeOfDay properties of DateTime.
Important Note:
If you're saving the value to a database, put the date value in a data parameter, don't concatenate strings to build SQL queries; that'll leave you open to the SQL injection attack. Here's a brief example (not tested code, just to give you an idea):
DateTime theDate = dtPicker.Value.Date;
IDbCommand command = GetDbCommand("insert into table_name (name, thedate) values (#name, #thedate)");
command.Parameters.Add(command.CreateParameter("#name", theName));
command.Parameters.Add(command.CreateParameter("#thedate", theDate));
command.ExecuteNonQuery();
More info on avoiding SQL injection attacks (and solving your "invalid month") problem can be found here:
http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx
DateTimePicker.Value
Worked for me. It shows the date and time.