Date conversion using SSIS - ssis

I am trying to load excel data into a table using SSIS.
In my excel, there is one column 'TRANSACTION DATE'.
TRANSACTION_DATE IN EXCEL TRANSACTION_DATE IN TABLE
01/12/2014 2014-01-12 00:00:00.000
01/12/2014 2014-01-12 00:00:00.000
02/12/2014 2014-02-12 00:00:00.000
05/12/2014 2014-05-12 00:00:00.000
05/12/2014 2014-05-12 00:00:00.000
13/12/2014 2014-12-13 00:00:00.000
16/12/2014 2014-12-16 00:00:00.000
16/12/2014 2014-12-16 00:00:00.000
19/12/2014 2014-12-19 00:00:00.000
20/12/2014 2014-12-20 00:00:00.000
22/12/2014 2014-12-22 00:00:00.000
26/12/2014 2014-12-26 00:00:00.000
29/12/2014 2014-12-29 00:00:00.000
31/12/2014 2014-12-31 00:00:00.000
31/12/2014 2014-12-31 00:00:00.000
Problem:
If you observe first 5 rows in the table values, the syntax is quite different with other values of the table.it should be (2014-12-01,2014-12-02 etc..). Because of this issue, if I fire a query to sum the values of of another column where DATEPART(MM,TRANSACTION_DATE)=12, The top 5 values are getting excluded.
Table Structure :
TRANSACTION_DATE DATETIME
SSIS DATA CONVERSION :
TRANSACTION_DATE DT_DBDATE

What's happening is the first 5 records are being parsed in the American format (MM/DD/YYYY), and the rest of the records are being parsed as DD/MM/YYYY.
Check the datetime format of the cells in Excel to make sure the Locale setting is consistent for all the dates.

Related

Update Most Recent Record, with Date and Time in two columns, Time Dependent on Date

I need to select the most recent record by date and time. The date and time are in two separate columns. I can select the max on time in the where clause. Is the best way to get the max of time by date to group by max date, and then select the max time in the where clause?
Test Table
SeqNo PEOPLE_ID FIRST_NAME LAST_NAME STATUS REVISION_DATE REVISION_TIME
1 1280 Justin Bonnor Yes 2008-03-26 00:00:00.000 1900-01-01 17:21:25.000
2 83801 Aaron Odem Yes 2013-04-18 00:00:00.000 1900-01-01 14:23:43.897
3 83811 Sonja South No 2014-12-10 00:00:00.000 1900-01-01 12:41:67.032
4 83811 Sonja South No 2014-12-10 00:00:00.000 1900-01-01 16:58:34.060
5 86221 Bryanna Parson No 2014-12-10 00:00:00.000 1900-01-01 16:58:23.480
6 88294 Jaclyn Velmer No 2016-04-14 00:00:00.000 1900-01-01 15:05:51.427
You could order by both of those columns and select top 1
SELECT TOP 1 * FROM TestTable ORDER BY RevisionDate DESC, RevisionTime DESC

Time difference in SQL between two rows

I have a table called eventdata like this
Timestamp GUId EventTypeId
2015-02-01 05:00:00 1100 E012-1
2015-02-01 05:30:00 1100 E012-2
2015-02-01 06:00:00 1100 E012-1
2015-02-01 06:20:00 1100 E012-2
2015-02-01 07:00:00 2200 E012-1
2015-02-01 07:15:00 2200 E012-2
Now corresponding to Guid, we have different EventTypeId. Now when I see E012-2 to corresponding E012-1 for a GUID (this could be different, for example sake I gave like this) I need a difference in Timestamp in minutes.
Example: for first two rows, for GUID=1100 it's 30. For the 3rd and 4th rows it's 20 minutes. for Guid=1100 for 5th and 6th rows it's 15 mints for Guid=2200.
I am using MySQL.

Calculation to be made while updating a field

Origin Dest Date Amount 50% Due
92509 0021 2013-07-30 00:00:00.000 5.37 0.00
92509 0021 2013-07-30 00:00:00.000 5.37 0.00
92509 0021 2013-07-30 00:00:00.000 5.37 0.00
92509 0021 2013-07-31 00:00:00.000 5.37 2.69
92509 0021 2013-07-31 00:00:00.000 5.37 2.69
92509 0021 2013-07-31 00:00:00.000 5.37 2.69
92509 0021 2013-08-01 00:00:00.000 5.37 2.69
92509 0021 2013-08-01 00:00:00.000 5.37 2.69
42101 0029 2013-03-06 00:00:00.000 6.06 0.00
42101 0029 2013-03-06 00:00:00.000 6.06 0.00
42101 0029 2013-03-07 00:00:00.000 6.06 3.03
42101 0029 2013-03-07 00:00:00.000 6.06 3.03
42101 0030 2013-03-06 00:00:00.000 6.06 0.00
42101 0030 2013-03-06 00:00:00.000 6.06 0.00
42101 0030 2013-03-07 00:00:00.000 6.06 3.03
42101 0030 2013-03-07 00:00:00.000 6.06 3.03
So I have a table something similar to what i shown above. Right now, the 50% Due field is empty. I need to fill that field with values as shown above.
The 50% due field should populate values that are half of what is present in the Amount field. But, it should fill zero for the initial date (2013-07-30 00:00:00.000) and for the consecutive days it should fill half of what is present in the Amount field.
I have a lot of rows like these that needs to get updated. Also there are rows with different Origin and Destination.
I am dealing with some freight parcels. The data describes the parcels that were sent to the same destination from same origin on consecutive days. The parcels that were sent on consecutive days could have been sent together on the initial date itself. So I am trying to generate a claim for those parcels that were sent on consecutive days to the same destination from the same origin. And the 50% Due would be the claim!
I am fairly new to SQL! This seems to be very complicated for me. Please help.
If you want to update all origin/dest combinations that had an entry on the prior day then you can do this:
update t
set [50% due]=coalesce(cast(p.amount/2.0 as smallmoney),0.00)
from [table] t
left join [table] p
on t.origin=p.origin and t.dest=p.dest
and dateadd(D,-1,cast(t.[date] as date))=cast(p.[date] as date)
or use a cte and LAG in SQL 2012 and later versions:
;with previous as
(
select origin,dest,[date]
,LAG(cast([date] as date),1,cast([date] as date)) OVER (PARTITION BY origin,dest ORDER BY cast([date] as date)) as previous
from (select distinct origin, dest, [date]
from [table]) a
)
update t
set [50% Due]=case when dateadd(D,-1,cast(t.[date] as date))<>cte.previous then 0.00 else cast(t.[amount]/2.0 as smallmoney) end
from [table] t
join previous cte
on cte.origin=t.origin and cte.dest=t.dest and cte.[date]=t.[date]
If you want to update all your records based on the earliest day that the origin/dest combination occurred, then this will work in SQL Server:
;with earliest as
(
select origin,dest,min(cast([date] as date)) earliest
from [table]
group by origin,dest
)
update t
set [50% Due]=case when cast(t.[date] as date)=cte.earliest then 0.00 else cast(t.[amount]/2.0 as smallmoney) end
from [table] t
join earliest cte
on cte.origin=t.origin and cte.dest=t.dest
If you want to update all your records only based on the earliest day in the table and you don't care about the orgin/dest combination then you don't need the cte to group the earliest dates and you can simply do the compare in the update.
UPDATE t
set [50% Due]=case when cast(t.[date] as date)=(select min(cast(t.date as date))) then 0.00 else cast(t.[amount]/2.0 as smallmoney) end

Mysql order date by the m/h/s

How to order date, like this - 2012-02-01 00:00:00 by the hour,minutes,and the seconds, not by the year/moth/day.
If i have..
2012-02-01 02:00:00
2012-03-01 20:00:00
2012-04-01 12:00:00
2012-05-01 07:00:00
I wan't to get this output.
Column tipe is timestamp.
2012-02-01 02:00:00
2012-05-01 07:00:00
2012-04-01 12:00:00
2012-03-01 20:00:00
ORDER BY TIME(date_column)
This will, however, slow down your queries, as it isn't possible to index the on-the-fly calculation. If you have a lot of records, or if this query runs frequently, you should break the time portion of the date into its own column so you can index it for faster sorting.
Use the TIME() function to extract the time portion of the expression passed, e.g.
mysql> SELECT TIME('2012-02-01 02:00:00');
-> '02:00:00'
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_time

JOIN unrelated tables by t1.date (between t2.startdate and t2.enddate)

A SQL question, probably not the most difficult.
I'm making a view from a bunch of related table join on ID's -> easy. Now there is one table that hasn't got a key relationship with all the others. (BatchDates)
`ALTER VIEW [ECSUB].[FCT_Ext_Collection]
AS
SELECT sh.id AS idSubmissionHistory, dh.id, dd.id AS Description, sch.id AS idScoringHistory, sh.CreationDate, sh.UpdateDate, bd.id AS BatchDateID
FROM ECSUB.SubmissionHistory AS sh INNER JOIN EC.DocumentHistory AS dh ON sh.id = dh.idSubmissionHistory
LEFT OUTER JOIN ECSM.ScoringHistory AS sch ON sh.idScoringHistory = sch.id
LEFT OUTER JOIN EC.DocumentDescriptions AS dd ON dd.id = dh.Description
LEFT OUTER JOIN ECSUB.AddressBilling AS ab ON sh.id = ab.id
LEFT OUTER JOIN ECSUB.AddressPremise AS ap ON sh.id = ap.id
CROSS JOIN EC.BatchDates AS bd --ON sh.documentdate between .......
GO`
Well, my main table 'documentHistory' contains a document date, I have to define in which batch this falls.
Each batch has an ID and startdate. A batch is always one month long.
This will make it much more easy to understand, the data from the BatchDates table:
id month startdate
1 2010-12-01 00:00:00.000 2010-12-01 00:00:00.000
1 2011-01-01 00:00:00.000 2010-12-01 00:00:00.000
1 2011-02-01 00:00:00.000 2010-12-01 00:00:00.000
2 2011-03-01 00:00:00.000 2011-03-01 00:00:00.000
2 2011-04-01 00:00:00.000 2011-03-01 00:00:00.000
2 2011-05-01 00:00:00.000 2011-03-01 00:00:00.000
3 2011-06-01 00:00:00.000 2011-06-01 00:00:00.000
3 2011-07-01 00:00:00.000 2011-06-01 00:00:00.000
3 2011-08-01 00:00:00.000 2011-06-01 00:00:00.000
4 2011-09-01 00:00:00.000 2011-09-01 00:00:00.000
4 2011-10-01 00:00:00.000 2011-09-01 00:00:00.000
4 2011-11-01 00:00:00.000 2011-09-01 00:00:00.000
5 2011-12-01 00:00:00.000 2011-12-01 00:00:00.000
5 2012-01-01 00:00:00.000 2011-12-01 00:00:00.000
5 2012-02-01 00:00:00.000 2011-12-01 00:00:00.000
6 2012-03-01 00:00:00.000 2012-03-01 00:00:00.000
6 2012-04-01 00:00:00.000 2012-03-01 00:00:00.000
6 2012-05-01 00:00:00.000 2012-03-01 00:00:00.000
7 2012-06-01 00:00:00.000 2012-06-01 00:00:00.000
7 2012-07-01 00:00:00.000 2012-06-01 00:00:00.000
7 2012-08-01 00:00:00.000 2012-06-01 00:00:00.000
8 2012-09-01 00:00:00.000 2012-09-01 00:00:00.000
8 2012-10-01 00:00:00.000 2012-09-01 00:00:00.000
8 2012-11-01 00:00:00.000 2012-09-01 00:00:00.000
9 2012-12-01 00:00:00.000 2012-12-01 00:00:00.000
9 2013-01-01 00:00:00.000 2012-12-01 00:00:00.000
9 2013-02-01 00:00:00.000 2012-12-01 00:00:00.000
10 2013-03-01 00:00:00.000 2013-03-01 00:00:00.000
10 2013-04-01 00:00:00.000 2013-03-01 00:00:00.000
10 2013-05-01 00:00:00.000 2013-03-01 00:00:00.000
etc...........
So I need to fetch the batchID based on the documentdate, therefore we use the currentMonth of the column startdate.
Thus: ...JOIN BatchDates where documentDate is in startDate.month (there is no between here)
I don't even know if I need a join, cross join, union, etc...
Thanks in advance!
L
join BatchDates
on datepart(yyyy,[document date]) = datepart(yyyy,[startDate])
and datepart(mm,[document date]) = datepart(mm,[startDate])