I'm trying to save data of birth in database from client but it gives format exception
heres my conversion
cmd.Parameters.AddWithValue("#dob", DateTime.ParseExact(dob.Text ,"dd-mm-yyyy",System.Globalization.CultureInfo.InvariantCulture));
input example dob.text="22-2-2012"
in this case exception is "String was not recognized as valid dateTime"
but in case of "22-12-2012"
the exception is "Conversion failed when converting date and/or time from character string"
Try changing it to
DateTime.ParseExact(dob.Text, "dd-M-yyyy", System.Globalization.CultureInfo.InvariantCulture)
instead (note the single capital M instead of your double small m)
Have a look at Custom Date and Time Format Strings and look at the difference between a single M and a double M.
Related
I have to convert timestamp which is in varchar (3/12/2021 1:38:53 PM) to datetime using SSIS. This is code I used to do the same
(DT_DBDATE)(SUBSTRING(TimeStamp,FINDSTRING(TimeStamp,"/",2),4) + "-" + (LEN(SUBSTRING(TimeStamp,FINDSTRING(TimeStamp,"/",1),FINDSTRING(TimeStamp,"/",2)))==1?"0"+SUBSTRING(TimeStamp,FINDSTRING(TimeStamp,"/",1),FINDSTRING(TimeStamp,"/",2)):SUBSTRING(TimeStamp,FINDSTRING(TimeStamp,"/",1),FINDSTRING(TimeStamp,"/",2))) + "-" + (LEN(SUBSTRING(TimeStamp,1,FINDSTRING(TimeStamp,"/",1)))==1?"0"+SUBSTRING(TimeStamp,1,FINDSTRING(TimeStamp,"/",1)):SUBSTRING(TimeStamp,1,FINDSTRING(TimeStamp,"/",1))))
The above threw the following error:
[Derived Column [2]] Error: An error occurred while attempting to
perform a type cast.
I would also need to include the time as my destination SQL column is DATETIME2.
Without manual parsing method:
From microsoft docs:
When a string is cast to a DT_DATE, or vice versa, the locale of the transformation is used.
Go into derived column advanced setting and set LocaleId to something appropriate for your date source. LocaleId of "English (United States)" should work based on the example you provided.
Then leverage DT_DATE to do the parsing before casting it to DT_DBDATE
(DT_DBDATE)((DT_DATE)TimeStamp)
I have a date string like '20200814' that means day 14 of month 08 of year 2020.
In the docs of intl it said this:
'yyyy.MM.dd G 'at' HH:mm:ss vvvv' 1996.07.10 AD at 15:08:56 Pacific Time
So if I use this:
DateFormat('yyyyMMdd').parse('20200814')
It must works, but throw this error:
════════ Exception caught by animation library ═════════════════════════════════
The following FormatException was thrown while notifying status listeners for AnimationController:
Trying to read MM from 20200814 at position 8
When the exception was thrown, this was the stack
#0 _DateFormatField.throwFormatException
package:intl/…/intl/date_format_field.dart:87
#1 _DateFormatPatternField.parseField
package:intl/…/intl/date_format_field.dart:337
#2 _DateFormatPatternField.parse
package:intl/…/intl/date_format_field.dart:250```
I've taken a look at the intl source, and unfortunately the library does not support parsing a date string containing numerical components that are not separated by non-numeric characters.
The reason for this is that parse(input) converts the input string to a stream, and when it tries to extract the year value, it calls the _Stream instance method _Stream.nextInteger() on the input stream, which then consumes the entire string, since the whole string can be read as a single integer. This leaves nothing in the stream to be parsed as month or day, which is why the error is thrown.
As #Michael Horn mentioned, The package:intl's DateFormat library does not support parsing date strings unless they are character-delimited.
In this case, parser sees all the numbers entered as years, so it occurs an error trying to find the month according to the pattern.
So we have to put some separator between the input.
String input = '20210701';
/// convert 20210701 to 2021/07/01
String newInput = '${input.substring(0, 4)}/${input.substring(4, 6)}/${input.substring(6, 8)}';
DateTime date = DateFormat('yyyy/MM/dd').parse(newInput);
I am doing an F# course by Richard Broida. In one lecture I am supposed to parse a CSV document. Everything works fine except for parsing datetime. My local culture settings are Slovak (sk-SK) and I am trying to parse the following datetime string:
9/14/2016 19:31
I have read through the CSV file and found out that months, days and hours can all have both one or two digits. Years always have 4 digits and minutes always have 2 digits. So, the datetime 10/7/2016 9:01 is also a valid one.
I have created an adapter function:
let dateTimeParseAdapter format provider date =
DateTime.ParseExact(date, format, provider)
and then called
dateTimeParseAdapter "M/d/yyyy h:mm" CultureInfo.InvariantCulture "9/14/2016 19:31"
but I got an exception message saying
System.FormatException: String was not recognized as a valid DateTime.
I tried adjusting the format string to exactly match the number of digits in my single date. However, even the following code resulted in the same error message.
dateTimeParseAdapter "M/dd/yyyy hh:mm" CultureInfo.InvariantCulture "9/14/2016 19:31"
Why am I getting the exception message? Should I change the CultureInfo settings? Or is there a pre-defined datetime format to parse my datetimes?
When working with 24 hour format you should use H (HH) not h hh (which is for 12 hour format) pattern:
format = #"M/d/yyyy H:mm";
since you are using 24 hour in your value you must put capitals in hours. Change
"M/dd/yyyy hh:mm"
to
"M/dd/yyyy HH:mm"
I am using oracle 12c with the username system. My problem is when I execute this insert statement that I took from oracle live sql site:
insert into emp
values(7788, 'SCOTT', 'ANALYST', 7566,to_date('13-JUL-87','dd-mm-rr') - 85,3000, null, 20);
it shows :
sql error ora-01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
what is this -85 after the to_date(..)
To handle dates, you would better use the ANSI format (date 'yyyy-mm-dd'):
insert into emp values(7788, 'SCOTT', 'ANALYST', 7566, date '1987-07-13'- 85,3000, null, 20);
If you need to use a to_date for some reason, you have to be sure that the format of your string exactly matches the format mask you use: if your month is written as 'JUL' you need 'MON' in the format mask and not 'mm'. 'mm' would match a month written as '07'.
Please notice that even with the right format mask, this way to write dates is dangerous, because it's based on the language of your DB.
The -85 means "subtract 85 days".
I have fetched a set of dates from postgresql, they look correct:
[1] "2007-07-13" "2007-07-14" "2007-07-22" "2007-07-23" "2007-07-24"
[6] "2007-07-25" "2007-08-13" "2007-08-14" "2007-08-15" "2007-08-16"
etc.
Then I want to run a loop on them to make new sql sentences to fetch some other data sets (yes, I know what I am doing, it would not have been possible to do all the processing in the database server)
So I tried
for(date in geilodates)
mapdate(date,geilo)
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not Retrieve the result : ERROR: invalid input syntax for type date: "13707"
LINE 1: ...id_date_location where not cowid is null and date='13707' or...
mapdate is a function I have written, the use of date within that is
sql=paste('select * from gps_coord where cowid=',cowid," and date='",date,"'",sep='')
So, what has happened is that R silently converted my formatted dates to their integer representations before i tried to paste the sql together.
How do I get the original textual representation of the date? I tried
for(date in geilodates){
d=as.Date(date,origin="1970-01-01")
mapdate(d,geilo)
}
Error in charToDate(x) :
character string is not in a standard unambiguous format
And I have not managed to find any other functions to create a datestring (or to "serve" the date as the string I get when listing the variable
Thanks to wush978 for pointing me in the right direction, In the end I had to do:
for(d in geilodates){
date=format(as.Date(d,origin="1970-01-01"))
mapdate(date,geilo)
}
For some reason, inside the loop the "date" variable was seen as an integer, so I explicitely had to convert it to a date and then format it...
try ?format.Date
x <- Sys.Date()
class(x)
class(format(x))
In R, the data of class Date is a numeric type.
An official way to represent Date as string is to call format.
I doubt that the format of date is defined in your case, so paste
does something unexpected.
Maybe you need to put format(x, "%Y-%m-%d") in your paste function instead of date to tell R how which format you want for Date.