Marketing cloud Tracking Time conversion day light saving - salesforce-marketing-cloud

I have job record
Select JobID, DeliveredTime from _Job where JobID=75XXX1
This give me :
DELIVEREDTIME : Oct 5 2021 2:06AM (Central Standard Time (CST) Time Zone, but do not observe Daylight Savings Time (DST)) (https://help.salesforce.com/s/articleView?id=000323225&type=1)
The tracking for this job shows : 05/10/2021 10:06 ( I am in Amsterdam/Brussels)
I run a sql activity (below) where i try to convert this time to my time (CET) today . I get 5/10/2021 9:06:00 AM
Select
T1.JobID as JobId,
CONVERT(datetime,(T1.DeliveredTime AT TIME ZONE 'Central Standard Time' AT TIME ZONE 'Central European Standard Time'),0) as SentTime,
T1.EmailName as EmailName,
T1.EmailSubject as Subject,
T1.FromName as FromName,
T1.FromEmail as FromEmailAddress,
T1.TriggererSendDefinitionObjectID as TriggeredSendDefinition,
T1.EmailID as EmailID,
T1.AccountID as AccountID,
T1.DynamicEmailSubject as DynamicEmailSubject,
T1.EventID as EventID
From
_Job T1 WITH (NOLOCK)
So there is 1 hour difference. I assume this is because of day light saving which happens in nov in US.
The problem i have jobs which have run post day light saving run have correct time and job which have run before day light saving have wrong time.
how can i change my sql so that it gives me right time always

Just found the answer to my question.
Thanks to "https://www.linkedin.com/pulse/its-timezone-finally-real-convert-function-salesforce-jack-hobbs/" from Jack Hobbs
and "https://bornsql.ca/blog/dates-and-times-in-sql-server-at-time-zone/"
The problem was source timezone .
Orginal SQL :
Select JobID,
DeliveredTime as del0,
DeliveredTime AT TIME ZONE 'Central Standard Time' AT TIME ZONE 'Central Europe Standard Time' as Deltim2
from _Job where JobID = 7XXX9
I was first using 'Central Standard Time' as mentioned by salesforce marketing cloud on article (https://help.salesforce.com/s/articleView?id=000323225&type=1)
"Marketing Cloud servers use the Central Standard Time (CST) Time
Zone, but do not observe Daylight Savings Time (DST)."
This was a mistake. i should have used 'Central America Standard Time' instead of 'Central Standard Time'
Modified SQL:
Select JobID,
DeliveredTime as del0,
DeliveredTime AT TIME ZONE 'Central America Standard Time' AT TIME ZONE 'Central Europe Standard Time' as Deltim2
from _Job where JobID = 7XXX9
So when i modified my sql, the dates were correct for jobs before day light saving change and after it also
Thanks a lot guys , you dont spend the morning finding this as i did.

Related

How to make SSRS subscriptions Kick off on Business Day 6 of month

Found a similar question
5th BUSINESS DAY subscription SSRS
But in that case a work around -schedule for 1st of month- was suggested and accepted as the answer.
I want to know if anyone has found a clever way to make SSRS subscriptions run on a specified Business Day. I.E run on Business Day 6. This is not just to prevent the report from going out on a weekend, but also because certain Finance operations related to closing the month have an agreed upon date (EX. "Will be done by BD 3") And I need my report to run after that each month.
One comment also suggested setting up a sql agent job to calculate what BD X would be each month, and insert a SQL job for that date to kick off the Report. I can see in theory how this would work - but the subscription wouldn't be managed within SSRS then and could easily be over-looked in the future.
I have been getting by with the following imperfect code:
--: returns last day of last month (DT), but only provides a result row if we are on BusinessDay X of the month.
--No rows returned prevents the report from being processed & sent.
--SSRS Schedule must be set to run monthly in a range covering all possible Calendar Days that could be Business Day X
declare #dt datetime = Getdate(), #BDTarget int = 6
SELECT DATEADD(MONTH, DATEDIFF(MONTH, -1,#dt)-1, -1) as DT,sum(dimdate.BusinessDay) as BD
FROM DimDate
where FullDate between DATEADD(mm, DATEDIFF(mm, 0, #dt), 0) and #dt
having sum(dimdate.BusinessDay)=#BDTarget
Though recently discovered that this logic can kick off the report two days in a row, for example if we are looking for BD 6, this month July 2021 the above query returned a row on both Friday 7/9 and Sat 7/10.
You can use a data-driven subscription that runs daily.
Use a query that will only give a result on the 6th business day.
e.g.
WITH cte_6th
AS
(
SELECT *
FROM dimDate dd
WHERE dd.TheMonth = MONTH(current_timestamp)
AND dd.TheYear = YEAR(current_timestamp)
AND dd.BusinessDay = 1
ORDER BY dd.Date
OFFSET 5 ROWS
FETCH NEXT 1 ROWS ONLY
)
SELECT *
FROM cte_6th c
WHERE c.Date = CAST(current_timestamp as DATE);

strange result for str_to_date in mysql

A developer created a table for us, and made a date field as VARCHAR(255) and a field name of "Requested Ship Date".
First yes I know these both are bad database management, but I was not involved at the creation point, just now in the please run reports against it point.
So, I am trying to convert this field to a date value, and was using STR_TO_DATE(Requested Ship Date,"%m/%d/%y") (it has the correct syntax in the code)
The problem is I am getting a weird result. 'Requested Ship Date' = 5/31/2017 after the conversion it comes out as 2020-05-31.
Thank you in advance.
%y is the year without the century, %Y is the year with the century.
So 2017 is being interpreted as '20, which is 2020.
This is the results I am getting from my query
05/30/2017 05/31/2017 2017-05-30
05/30/2017 05/31/2017 2017-05-30
05/31/2017 05/31/2017 2017-05-31
Here is the actual code for the Query
select `Requested Ship Date`,
`Actual Post Goods Issue date`,
STR_TO_DATE(`Requested Ship Date`,'%m/%d/%Y')
FROM `previous day shipments`
WHERE LENGTH(`Serial Number`) = 0

MySQL IF Time Conversion

I am looking for a way to do something, I have never done before.
I have the Query as
SELECT DATE_FORMAT(CONVERT_TZ(pi.start,'+00:00','+10:00'), '%Y-%m-%d %h:%m:%s') as start,
ifnull(dc.name,'Unknown') as source,
format(sum(ifnull(pi.rev,0)),2) as revenue
from trk.provider_imp pi
left outer join trk.dsp_client dc
on pi.clientid = dc.id
and type = 'client'
where pi.start between DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 5 MONTH) AND DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 1 MONTH),'%Y-%m-%d')
and pi.status = 1
group by start,pi.clientid
order by pi.start, pi.clientid;
The above query works and I get the results that I desire, however, for the Time Zone Conversion, I am an hour behind, for Nov, Dec and Jan.
Is there a way where I could add a if statement, and that would do something like, if 'start' is in ('Nov 2014','Dec 2014', 'Jan 2015', than 'start' plus one hour)?
To do what you need, the simplest and most effective way I have found was to use your actual location as a parameter, and not the GMT offset.
For instance, if you are in Melbourne, instead of using '+10:00' in Winter and '+11:00' in Summer, you would simply mention 'Australia/Melbourne' in the timezone parameter. Doing this will take care of the daylight savings time offset.
Your example mentioned +10, I'm assuming Melbourne; these 2 statements will return different times as the DST offset is different:
SELECT CONVERT_TZ('2015-01-01 02:00:00','UTC','Australia/Melbourne');
SELECT CONVERT_TZ('2015-10-01 02:00:00','UTC','Australia/Melbourne');
The caveat is that standard MySQL installations do not come with the timezone names built-in.
Follow these instructions to get those in your MySQL server: http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html

Finding available timeslots in a day based on existing records in MySQL

I have a table that has these fields:
How would I find all available time slots for that day (based on the day starting at 7am and ending at 10pm) that aren't currently in this table? For example, on one particular day, if all timeslots from 7am till 10pm were taken bar one at 6pm till 7pm, that would be the one result.
The duration of each time slot does not vary - they all last one hour.
I have tried many different things, but I have a feeling I am so far off, it is hardly worth posting what I've tried.
Any help would be greatly appreciated!
One thing I would think about would be having a "hours table" with all the values you wanna check. Then, with a left join and selecting only null values, you'd get only the values you haven't assigned. I built a SQLfiddle http://sqlfiddle.com/#!2/66b441/6 to check it with some dummy values to show how this works:
SELECT h.slot
FROM hours h
LEFT JOIN deliveries d
ON ( h.slot = d.start_time AND date_stamp = '2014-04-04' )
WHERE start_time IS NULL
Check the data in the SQLfiddle, if you know the slots and they don't overlap, with that you will get for the date the values
SLOT
January, 01 1970 07:00:00+0000
January, 01 1970 10:00:00+0000

Counting hours using MySQL

I'm creating simple project where I count the employee attendance and other things
when employee late on work ,I have to enter the delay time, example
employee came to work late by 30 min, so I will enter on the field 30:00 or 00:30:00 , in the end of each month , week, or day, I want to calculate the time, such as 30 min + 1H + 25 min and in the end of each month I will get the total of all that hours and minutes on the report, the report will show me the average and the sum of that time.
the following fields ( Timing Logged in Duration , Total Talking Time, Work , Time Not ready Time ) I want to calculate the hours and minutes.
what I’m getting in the report is showing the SUM of the hours I entered as time with AM/PM which is not what I’m looking for.
this is MySQL query
SELECT
ID,
`Date`,
`Shift Time`,
`In charge`,
`Agent Name`,
Attendance,
Timing,
`Logged in Duration`,
`Total Talking Time`,
`Work Time`,
`Not ready Time`,
`Calls Handled`,
RNA,
`Shift Calls Presented`,
`Shift Calls Handled`,
`HD Calls Abandoned`,
`Upload Your Report`
FROM `shift report`
I thought I would post you the answer in the perspective of summing up time value releated issues developers, users have face so far. It's not just about formatting.
You may or may not have noticed that there's posibility sum of your total time can be miscalculated by the engine. Further mysql engine could return null although you have values.
When you are adding/aggregating time values it is converted as a number and results will be in number format. If you attempt to adding up time values like the following:
e.g. 01:38:50, 03:40:25 --> should ideally result in 05:19:15
If you do Sum(the above two) --> you get a number
If you use CAST(Sum(the above two) AS TIME) --> you get null
There are two other possibilities showed in the my code snippet.
Sample code reference. It will show you the different results that we just discussed. As for now, it seems like three way formatting could help.
time_format(sec_to_time(sum(time_to_sec(logged_duration))),'%h:%i:%s') total_log_duration
The bug reported to MySQL is not rectified yet.