Extract/Compare only Year from DATETIME field in SSIS - mysql

Trying to check if a datetime column has a year of less than 1754 in a derived column.. my expression goes
(YEAR([OffenseEndDate]) < 1754) ? (DT_DBTimeStamp)"1900-01-01 00:00:00" : [OffenseEndDate]
I keep getting an error from this. I've also piped the error to another file and the some of the dates look like this
0010-05-06 00:00:00
Not understanding why this method is not working in SSIS. I either want to null the dates that have year less than 1754 or just output 1900-01-01.

Try derived column code:
(DT_I4)(SUBSTRING((DT_WSTR,20)[OffenseEndDate],1,4)) < 1754 ? (DT_DBTimeStamp)"1900-01-01 00:00:00" : [OffenseEndDate]

Related

How to avoid error on MYSQL CONVERT() method, when passing a 0 second string and subtract from that value?

I am using MySQL 8 and have a problem with this type of query:
INSERT INTO review (name, create_date) VALUES('name', CONVERT(timestamp, DATETIME) - 1)
I have not had this error when using this expression in a where clause.
When the value for the timestamp is like '2020-12-16 06:15:01' it's working.
But with a value of 0 seconds (like: '2020-12-16 06:15:00') an error is dropped.
Incorrect datetime value: '20201216061499' for column 'create_date' at row 1
code: ER_TRUNCATED_WRONG_VALUE
errno: 1292
sqlState: 22007
I used this type of expression in my whole project. Is there a simple solution to this problem, without changing each expression?
Is that one a bug?
One solution to this problem is:
DATE_SUB(CONVERT(timestamp,DATETIME) INTERVAL 1 SECOND).
But as I already mention this requires changing each expression.
You do need to update each expression. When you subtract a number from your timestamp, it first converts your timestamp into a number (e.g. 20201216061500), then you are subtracting one and, because the column you are inserting is a datetime, it tries to interpret the resulting number as a date/time, failing when the subtraction produced 20201216061499. The correct way to subtract one second is to say - INTERVAL 1 SECOND or use DATE_SUB(..., INTERVAL 1 SECOND).

Date difference between two dates with format 01/06/18

Hi ALL I am trying to get date difference between two dates. I am getting data from cube and saving in temp table. I tried to defined date field as a varchar and also datetime and date
My date is in the format 03/04/18
And I am trying DATEDIFF(DAY, PAAR.DateReceived, O.DateReceived) for getting number of days difference. But I am getting error as below.
When I defined date field in temp table as DateTime or Date
"Operand type clash: ntext is incompatible with datetime"
When I defined date field in temp table as Varchar then
"Conversion failed when converting date and/or time from character string."
Thanks In Advance
I suggest you use str_to_date() with the appropriate format modifiers to convert those strings into dates.
DATEDIFF(str_to_date(PAAR.DateReceived,'%d/%m/%y'), str_to_date(O.DateReceived,'%d/%m/%y'))
see: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
Try this
set #sdate = '01/01/18';
set #edate = '01/26/18';
set #sdate = CONCAT_WS('-',SUBSTR(#sdate FROM 7 FOR 2),SUBSTR(#sdate FROM 1 FOR 2),SUBSTR(#sdate FROM 4 FOR 2));
set #edate = CONCAT_WS('-',SUBSTR(#edate FROM 7 FOR 2),SUBSTR(#edate FROM 1 FOR 2),SUBSTR(#edate FROM 4 FOR 2));
SELECT DATEDIFF(date(#edate), date(#sdate));

Query database where month is equal to

I have a query where I need to select entries only where their month is equal to a certain month.
Each entry has the date saved in the ymd format (20140211).
I have tried the following, but it doesn't recognise the month.
AND (MONTH(matrix.col_id_3) = '1' OR MONTH(matrix.col_id_4) = '1')
Is this due to the date format? I am stuck with this format as its part of the CMS I use.
Any help would be fantastic.
if you can use the default date column type (why?) so you have to convert your column
i.e.
... MONTH(STR_TO_DATE(matrix.col_id_3,'%Y%m%d')) = 1 ...
probably your dates are stored as strings, so you could use the following
.... SUBSTRING(matrix.col_id_3, 5, 2) = '01' ...

Check date format in conditional split transformation in ssis

I am having flat file which contain bad date like [02/02/0200].I want export data into sql table.I am using Condition split or derived column transformation for date column.
I want that correct date goes to main table and errors rows go to error table.
Could you please let me know what expression i have to used for date column .
Thanks,
Jeetesh Garg
When you convert your date [02/02/0200] it's gonna be 200 Year 02 Month 02 Day.
So you need to eliminate nonsenses dates. This gonna eliminate dates which are lower than 2000 Years.
(DT_DATE)([YourDate]) > (DT_DATE)(2000 - 01 - 01)
OK so new derived column statement (bccPostDate - data type is "unicode string" ) :
((DT_I4)(SUBSTRING(bccPostDate,1,2))) > 12 || ((DT_I4)(SUBSTRING(bccPostDate,4,2))) > 31 || ((DT_I4)(SUBSTRING(bccPostDate,7,4))) < 1900 ? "" : bccPostDate
IS working I have tried with Years and this statement returning column with "Unicode string" data type.

How to add 1 year to date stored as varchar?

How can add 1 year to a date value stored in the DB as varchar (format: "2011.03")?
I'm trying with this, but returns NULL :(
DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03', '%Y.%m'), INTERVAL 1 YEAR),'%Y.%m')
Thank you very much!
edit.: this is the query i want to use in:
SELECT DISTINCT column FROM table WHERE column BETWEEN '2011.03' AND DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03', '%Y.%m'), INTERVAL 1 YEAR),'%Y.%m') ORDER BY column DESC
("2011.03" is a parameter value and comes outside)
What you are trying is giving correct result
Try :
SELECT DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03', '%Y.%m'), INTERVAL 1 YEAR),'%Y.%m')
it's giving 2012.03.
Edit:
You can use below trick to make it workable in mysql version > 5.0
Add month with date because in Mysql version > 5.0 str_to_date() sometimes would return NULL if the %D format specifier was not the last specifier in the format string input.
Try below:
SELECT DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03.01', '%Y.%m.%d'), INTERVAL 1 YEAR),'%Y.%m')