I am using this SQL query to convert my _Submission_date column which of type nvarchar(max) to datetime format:
SELECT
CAST(PSCData._SUBMISSION_DATE AS DATETIME2)
FROM
PSCData
I have tried every possible way but still its giving this error:
Conversion failed when converting date and/or time from character string.
The data inside my _Submission_Date is in this form:
"2017-8-21 21:13:55.00000"
"2017-9-21 14:13:55.00000"
When I run this query it works fine:
SELECT CAST('2017-08-25' AS DATETIME);
but with this format :
SELECT CAST('2017-9-21 14:13:55.00000' AS DATETIME);
I get the same error mentioned above.
Any suggestions that how I can solve this?
Found the solution. The actual problem was the double quotes around the string which was causing the error it can be solved like this:
update PscData
SET _SUBMISSION_DATE = REPLACE(_SUBMISSION_DATE,'"', '')
select CAST(PSCData._SUBMISSION_DATE as DATETIME2)
FROM PSCData
i.e: first use the REPLACE method to remove double quotes around the string than you can use the cast method and can easily convert the "varchar" type data to "datetime" format without any problem. Plus u have to use "datetime2" in the cast method as "datetime" will not work with it.
Thanks for the help btw :)
Related
I have a DateTime variable (default formatting), and I would like to format it to a format to I receive from a string parameter.
I normally do something similar to: {myDate:yyyy-MM-dd}, and it works properly.
Now I have a lot of possible date formats and need to format according to the chosen one.
I have tried the following but returned garbage (ae0aor0aa):
string testFormat = "yyyy. MM. dd.";
{myDate:testFormat }
I have also tried to convert the date to string and back to date with ParseExact, but gave me an invalid date exception. NB: the date in myDate is valid, as I have checked it with the debugger.
Can you kindly advise?
Thanks to apc, it was easily solved by myDate.ToString(testFormat)
My source is a file and loading into SQL Server table. I'm working on a scenario where i have to convert a string '2019-04-02T21:24:00.065' to informatica datetime format.
I tried below expression but some times its failing due to we are not receiving milliseconds from our source file in few occasions.
IIF(NOT ISNULL(DATEFIELD),TO_DATE(SUBSTR (DATEFIELD, 0, 10) || ' ' || SUBSTR(DATEFIELD, 12, 12), 'YYYY-MM-DD HH24.MI.SS.US'),NULL)
I'm looking for a permanent fix to handle all types of datetime formats regardless of what we receive in the file.
Well... I'm sorry to say, but there is no magic component that will recognize all possible date and time formats (including e.g. verbal in swahili).
You will need to detect the format for yourself. You can use a DECODE function, like e.g.:
DECODE(True,
IS_DATE(your_input_port, 'DD/MM/YYYY'), TO_DATE(your_input_port, 'DD/MM/YYYY'),
...)
If you are completely sure that only seconds/milliseconds are the missing part, you can check for length, if less than 12, use RPAD to the second part of your SUBSTR with missing format, or you can use decode as suggested by #maciejg and write code for all possible date formats.
Thanks for your inputs guys. Since we are not sure which date format we are receiving , we decided to go with a simple fix for this. I have changed the target field to varchar and simply replacing the T with ' ' value since this a staging mapping.
Again thanks for your time and inputs.
I have a two tables in first table i filled the values with name id,
on second table if i gave the id the table needs to fill the name automatically, how can i do this please help.
You commented that the error is in the following line:
axsl.TransDate = DateTimeUtil::utcNow();
This is logic because axsl.TransDate is Date and DateTimeUtil::utcNow() return a UtcDateTime when you compile get this error Operand types are not compatible with the operator.
There are many ways to fix this error.
Try this:
axsl.TransDate = DateTimeUtil::date(DateTimeUtil::utcNow())
DateTimeUtil::date() convert UtcDateTime in Date.
or you can use today() method to return the actual date.
Help me please with the next problem.
I have date time string in the next format(ISO 8601): 1999-12-31T23:59:59
and I need to cast it to TIMESTAMP value. The main problem in 'T' separator character.
I have tried next query:
SELECT TIMESTAMP_FORMAT('1999-12-31T23:59:59','YYYY-MM-DD HH24:MI:SS') FROM ROMAN.EMPLOYEE;
and use different format strings, such as, YYYY-MM-DDTHH24:MI:SS, YYYY-MM-DD"T"HH24:MI:SS.
Could you provide me correct way to cast this type of strings without any character replacement and substrings.
Thanks in advance!
There is no built-in function that can format a timestamp in ISO-8601 format in DB2 for Linux/UNIX/Windows.
As you have probably surmised, you can do this with REPLACE:
select
TIMESTAMP_FORMAT(REPLACE('1999-12-31T23:59:59','T',' '), 'YYYY-MM-DD HH24:MI:SS')
from
ROMAN.EMPLOYEE;
It's trivial to create a user defined function (UDF) to handle this formatting for you as well so you don't have to out this long string in every query.
It may also be possible to do it via XQuery with and xs:dateTime, although this would be even more code than just embedding REPLACE in the call to TIMESTAMP_FORMAT.
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.