Check date format in conditional split transformation in ssis - 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.

Related

How to get last entry by date

I have a table exchange_rates with columns ( Id , currency, date , selling_rate).
Sample data :
Id
currency
date
selling_rate
1
usd
10/11/2021
80
2
usd
15/11/2021
82
3
usd
29/11/2021
81
4
eur
30/11/2021
95
I want to get last entered (by date) for usd selling_rate
$sql = " SELECT selling_rate
FROM echange_rates
WHERE currency=usd
ORDER BY date DESC
LIMIT 1
";
Result should be 81 (for usd latest entry is 29/11/2021)
I have tested your query using the table data you provided except I addded a couple of quotation marks for you currency column due to its string datatype. The answer I got is 81. I suppose you ran into a syntax error for that? By the way be careful with the date column you defined. By default, MySQL does not support dd/mon/year format naturally. Therefore, to adjust to your depicted data format, I used the varchar type. This is prone to mistakes when doing comparisons as the initial character value matters most. e.g. insert into tb values (5,'usd','11/12/2021',84); The answer you get is still 81 rather than 84.
My suggestion is to use the date type for the column which is meant to be a date, then use date_format function to display it in the intended way. In this case , date_format(`date`,'%d/%m/%Y') will do the work.

sql incorrect date output incorrect

i have a table like this
|id| date |name|
1 23/11/20 jake
2 01/07/20 jhon
3 23/05/20 blake
4 11/02/20 drake
5 1/03/14 crake
i ran a query like this
WHERE date >= '1/07/20' AND date <= '23/11/20'
i expected a result where i would get only the results between those dates
but i got some results which were from 2014
the data type for the date column is varchar
#note i can not change the datatype
how can i only get dates between the two ?
String-wise comparison is the problem: typically, '10/01/19' (Janurary 10th, 2019) is greater than '01/01/20' (January 1st, 2020), because the former starts with 1, and the later with 0.
You need to turn these strings to dates before you can compare them:
where str_to_date(date, '%d/%m/%y') between '2020-07-01' and '2020-66-23'
This is inefficient, because the entire column needs to be converted before the filtering can happen. I would warmly recommend fixing your data model, and store dates as dates.
Side note: your strings need to be consistently formatted as mm/dd/yy for this to work; if you have varying formats - or strings that do not map to valid dates - then you have a bigger problem than what you have asked here.

SQL Query to get data between two weeks?

I have a week column with week numbers as w0, w1, w2.... I am trying to get last last six weeks data. Here's the sql query I am using.
SELECT * FROM week
WHERE uid = '9df984da-4318-1035-9589-493e89385fad'
AND report_week BETWEEN `'w52' AND 'w5'`;
'w52' is essentially week 52 in December 2015 and 'w5' is Jan 2016. The 'between' seems to not work. Whats the best way to get data from the above two weeks?
Here's the CREATE TABLE statement:
CREATE TABLE `week` (`uid` VARCHAR(255) DEFAULT '' NOT NULL,
`report_week` VARCHAR(7) NOT NULL,
`report_files_active` BIGINT DEFAULT NULL);
Essentially this table is getting populated from other table which has date column. It uses dates from other table and summarizes weekly data into this.
Any help is appreciated.
Refer to this SO Discussion which details the reasons for a problem similar to yours.
BETWEEN 'a' and 'b' actually matches to columnValue >='a' and columnValue <= 'b'
In your case w52 is greater than w5 due to lexicographic ordering of Strings - this means that the BETWEEN clause will never return a true (think about it as equivalent to saying BETWEEN 10 and 1 instead of BETWEEN 1 and 10.
Edit to my response:
Refrain from storing the week value as a string. Instead here are a couple of approaches in order of their preference:
Have a timestamp column. You can easily then use MySQL query
facilities to extract the week information out of this. For a
reference see this post.
Maintain two columns - YEAR, WEEKNO where YEAR will store values
like 2015, 2016 etc and WEEKNO will store the week number.
This way you can query data for any week in any year.
please show me table structure and DB name because it different for other, if it is any timestamp then we can use BETWEEN 'systemdate' AND 'systemdate-6'

Extract/Compare only Year from DATETIME field in SSIS

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]

Exclude weekends from calculated-field date using expression-builder MS Access

i have two fields in my access db table. One contains dates [D] and the other contains numbers [N]. I have created a calculated field that stores ( [D] - [N] ) dates.
my problem is that i would like to exclude weekends from the dates stored in the calculated field and cant seem to find how to do this from expression builder... is there a way to this? or is there any other way? thanks
Assuming the [d] column contains a date and [n] is a number of days then a calculated column may look something like :-
=IIf(Weekday(DateAdd("d",[n]*-1,[d])) In (7,1),"",DateAdd("d",[n]*-1,[d]))
Notes :
(7,1) on my system Saturday is 7 and Sunday is 1 - this can be
changed by altering the DateAdd function
You may wish to replace the
"" with NULL
I have assumed [n] is a positive number so using the *-1 to go backwords in days.