Convert string to DateTime in SSIS - sql

The data from source has string data type and sometime has invalid dates, empty column ('') I want to check whether it is valid date and convert string to date time. How can i do this in SSIS?
My string value is already formatted as mm/dd/yyyy.

Here is an expression that you can use in a Derived Column Transformation that will convert the date to a DT_DBTIMESTAMP data type. Or, it will set the value to null (with DT_DBTIMESTAMP data type) if it is null or blank. The DT_DBTIMESTAMP data type matches the SQL Server DATETIME data type.
ISNULL(MyDate) || TRIM(MyDate) == "" ? NULL(DT_DBDATE) : (DT_DBDATE)MyDate
If the input value is not a valid date you can map the error output path to a another Derived Column Transformation that assigns a null to the column.

What datetime value should an empty string be transformed into? NULL? You could use a derived columns transformation and / or script transformation.

Related

Convert String datetime to numeric date time

I have a column that I need converting from string date time to an actual data time data value.
The current format is as follows
15-JUN-22 10.24.10.414000
and I need to change it to the following format
15-06-22 10.24.10.414000
I use a stored procedure to automatically change the format, but it fails on this stage due to the non numeric characters.
Is there a way map or change all the string months to int values within the datetime? and if so how?
Converts used so far
TRY_CAST(CREATE_DATE AS DATETIME2(7)) AS CREATE_DATE
AND
CASE WHEN LEN([INTERVAL_START_DATE]) > 0 THEN TRY_CONVERT(DATETIME2, [INTERVAL_START_DATE], 103) ELSE NULL END
try this
CAST({your field name} as date) CONVERT(date,'{systemClock.UtcNow.ToString("o")}',127)

Getting Error : "Invalid date: [19/8/2013]. The row will be skipped in informatica

We have a source as a flat file which consists of a date column in format 19/08/2013.
We have a target in oracle table which consists of a date column in format 11-AUG-13.
When we are trying to pass the source column value in target using expression TO_DATE AND
TO_CHAR like
**source column is A ---> Source column
v1=TO_CHAR(A)
O1(output column)=TO_DATE(V1,'DD-MON-YY') we are getting the below error.
Invalid date: [19/8/2013]. The row will be skipped.**
Can anyone please help where I'm going wrong.
Thank you
You need to convert the str to date properly and then infa will load them.
Change the mapping like this -
Change data type in source to a string. read data as string.
Then use your expressions but change the to_date function like below.
v1(var column, data type string)=A
O1(output column, data type date)=IIF(IS_DATE(V1,'DD/MM/YYYY'), TO_DATE(V1,DD/MM/YYYY'),null)
IS_DATE - will check if the data is a date or not, if yes then only it will try to convert to a proper date else put null.
3. Then connect O1 column to a date type in oracle target.
This will make sure all data are loaded and none skipped.

How to convert string to date type in the Pentaho?

I am try to convert string value to date. The string has this format : yyyy-MM-dd. But when I try to convert using select values (in meta-date I selected fildname, type = Date and currency = dd/MM/yyyy I got this error :
String : couldn't convert string [2017-01-30] to a date using format [yyyy/MM/dd HH:mm:ss.SSS] on offset location 4
If I do in calculator step : Create a new field, Final_date as a Copy of field A; on Field A put the name of your input string; Data type is date and on Conversion mask choose the yyyy-MM-dd format (you don't have to pick one from the dropdown menu, you can write your own). I got the same error.
I am using the Pentaho Data Intagration 9.
I am try to convert the string format in yyyy-MM-dd to date type format in dd/MM/yyyy. For this case, how to convert string to date ?
When converting from string to date you specify the source format that the string is using, so in this case yyyy-MM-dd. That should be in the format selection list, but you can also manually type in any format needed.
Once the field is in date format, it will be correctly output to most database types. For files, you can define the new format (dd/MM/yyyy) in the output step like Text File Output or Excel Writer. Alternatively, you convert the date back into a string with the desired format using Select Values.

How to convert string with a specific date format like 'mm/dd/yy' to date object in hibernate query

One of the columns my_column in my table is of type varchar where i store value of date in string. The format is 'MM/DD/YY'. I want to use cast function of hibernate to convert this column value to date. But it appears that the cast function accepts string of 'yy-mm-dd' format to convert. Is it possible to use cast for string with specific date format for conversion. Something like this: cast(am.my_column as date format 'mm/dd/yy')
cast function in hibernate is used this way: cast(am.my_column as date).
My use case is basically converting the column value from string to date and making a date comparison. cast(am.my_column as date format 'mm/dd/yy') > '12/30/2017'
Instead of casting a column value inside the query, you may consider to give '12/30/2017' as parameter (with type of Java Date). So that you can directly write am.my_column > :parameterDate

Converting Date format in Sql Server

I have a column in my table for storing dates and it is in 12-06-2013 15:32:45. I want to convert it to MM/DD/YYYY format. How can I do it?
Coulmn type is varchar
First you need to CONVERT VARCHAR() to datetime type and and then to CONVERT it to string in desired format:
SELECT CONVERT (varchar (10), CONVERT (date,'12-06-2013 15:32:45' ,103),101)
First 103 is used to interpret current date format, and second - 101 - target format.
If you change target date format from varchar to date then your output in MSMS will be in default display date format of you SQL Server, not the desired format. This is because SQL Server stores dates as integers and converts them before dispalying the value. Therefore if you need to store in certain format, then store in VARCHAR type.
Check out CAST and CONVERT functions on MDSN
or you can do it in your c# code like that :
First you have to take your date from your table and put it in string var "DT" exemple :
string strDT =db.TableTitle.date;
string date = strDT .ToString("MM/DD/YYYY");
DateTime DT = Convert.ToDateTime(date);
then you can use the DT time variable :)