I have VB Script which is connected with the my database (SQL Server 2008), and this script query contains getdate() function, now I want to subtract 2 time value suppose SQL1-SQL2 and want only time difference, not the date.
SELECT DATEDIFF(MINUTE, SQL2, SQL1) FROM dbo.table;
If you want to do it in VBScript, then something like:
x = datediff("n", rs("SQL2"), rs("SQL1"))
T-SQL docs for datediff
VBScript docs for datediff
Related
How can I write an SSIS expression that determines whether a date is a weekend day?
I have this expression, but it seems it only 'works on my machine'
DATEPART( "Weekday", #[User::CurrentDate] ) ==6 || DATEPART( "Weekday", #[User::CurrentDate] ) ==7
After I deploy SSIS package to server, is suddenly changes order of numbers and
Monday became 2.
When on my computer Monday is 1, exactly as I expected.
I checked SQL Server itself, but it seems is something inside SSIS decide if Sunday or Monday is first day.
I cannot find exact question related to this issue.
Well, if you must do this in an SSIS expression, here is one way to work around the problem: you would compare the DW of your variable to the DW of a known constant (like August 3, 2019, which is a Saturday in any locale that I know of).
In pseudocode, IF ( DW(DateVariable) = DW("20190803") OR DW(DateVariable) = (DW("20190803")+1)%7 ) THEN {DateVariable is a weekend}
Personally, I would look for a way to do this in TSQL. Import the raw data "as is" into a staging table, and then do the transformation while moving the data to the destination table with a stored procedure.
DATEPART("DW",GETDATE()) or DATEPART("WEEKDAY",GETDATE()) works perfectly fine on both SQL server and local.
How are you passing value to #[User::CurrentDate] variable(Please check if same date is set when packages is executed on SQL server and on local)
I changed DateTime on my local and tested your expression as part Of Execute SQL task and Derived Column Transformation which gives me 1 for Sunday, 2 for Monday and so on and the result matches with SQL.
SQL query to get day of week:
SELECT GETDATE() Today,DATENAME("DW", GETDATE()) DayofWeekToday,
DATEPART("DW",GETDATE()) DayNumberToday,GETDATE()-1 Yesterday,
DATENAME("DW", GETDATE()-1) DayofWeekYesterday,DATEPART("DW",GETDATE()-1) DayNumberYesterday
Adding more to my answer : There is a difference between DateFirst and DatePart. DateFirst sets first day of the week while Datepart gives you day of the week. use this sql function to check what's your first day of the week
Set Datefirst 7 -- To set first day of week (7 = Sunday)
SELECT ##DATEFIRST -- To check first day of week
**Update your system settings to match first day of the week with sql server and you should get same values when you evaluate expression.
Your expression looks good its only your first day of week on local and sql server are not matching.
Once I updated first day of week on SQL, I was able to replicate the issue you are facing.
I have a question regarding Microsoft Access 2010. I'm working on an inherited complaints database and I've been asked to produce a report outlining how long complaints have/ or were active.
Using the following I'm able to calculate the difference between when a complaint was opened and when it was closed and show the number of days active.
DaysActive: DateDiff("d",[COMPLAINTS]![DateRcvd],[COMPLAINTS]![DateClosed])
My issue is when a complaint hasn't been closed I don't get a value returned. Is there a way to modify the expression so that if the DateClosed is empty it will use the current date instead?
Assuming this is a query expression, the db engine supports the Date() function which returns today's date. So you can use an IIf expression to give you Date() when DateClosed is Null, or otherwise DateClosed
DateDiff("d", COMPLAINTS.DateRcvd, IIf(COMPLAINTS.DateClosed Is Null, Date(), COMPLAINTS.DateClosed))
If the query will always be run from within an Access session, you can use Nz instead of IIf ...
DateDiff("d", COMPLAINTS.DateRcvd, Nz(COMPLAINTS.DateClosed, Date()))
Note that Nz is a VBA function and IIf is supported directly by the db engine, so IIf should theoretically be faster . But the difference may not be perceptible in your context.
Try using this.
DaysActive: DateDiff("d",Nz(COMPLAINTS.DateRcvd, Date()),[COMPLAINTS]![DateClosed])
Nz will check if the date is available or not, if not , it will replace today's date for that, using DATE() function.
I have viewed a lot of what others have asked and answered for this through Google, but have an additional question others haven't covered. My situation is that I am calling an Access query from AUTOMATE, a controller program. The process will run monthly against the data of the preceding month.
My intent is to use two functions declared in a module within Access to put the date range in the WHERE clause, but am finding putting it in on the query criteria line seems to be a problem.
Here are the two functions:
'##################################################################################
' This function returns the date string for the first day of the prior month.
'##################################################################################
Public Function FirstDayPriorMonth() As Date
Dim dateEnd As Date
dateEnd = DateSerial(Year(Date), Month(Date), 0)
FirstDayPriorMonth = dateEnd - Day(dateEnd) + 1
End Function
'##################################################################################
' This function returns the date string for the last day of the prior month.
'##################################################################################
Public Function LastDayPriorMonth() As Date
LastDayPriorMonth = DateSerial(Year(Date), Month(Date), 0)
End Function
The call from AUTOMATE uses DoCmd.OpenQuery to call the query. The SQL version of the full query looks like this:
SELECT dbo_V_HB_Outsourced_AR.REPORTING_DATE,
dbo_V_HB_Outsourced_AR.REPORTING_FISCAL_YEAR AS FY,
dbo_V_HB_Outsourced_AR.LOC_NAME AS FACILITY,
dbo_V_HB_Outsourced_AR.REVCBO_LEGACY_FINANCIAL_CLASS,
dbo_V_HB_Outsourced_AR.EPIC_FINANCIAL_CLASS,
dbo_V_HB_Outsourced_AR.PRIMARY_FIN_CLASS_NAME,
dbo_V_HB_Outsourced_AR.ACCT_STATUS_NAME,
dbo_V_HB_Outsourced_AR.OUTSOURCED_FLAG_YN,
dbo_V_HB_Outsourced_AR.IN_HOUSE_FLAG_YN,
dbo_V_HB_Outsourced_AR.DNFB,
dbo_V_HB_Outsourced_AR.[0-30],
dbo_V_HB_Outsourced_AR.[31-60],
dbo_V_HB_Outsourced_AR.[61-90],
dbo_V_HB_Outsourced_AR.[91-120],
dbo_V_HB_Outsourced_AR.[121-150],
dbo_V_HB_Outsourced_AR.[151-180],
dbo_V_HB_Outsourced_AR.[181-210],
dbo_V_HB_Outsourced_AR.[211-240],
dbo_V_HB_Outsourced_AR.[241-365],
dbo_V_HB_Outsourced_AR.[366+],
dbo_V_HB_Outsourced_AR.[CR BAL],
dbo_V_HB_Outsourced_AR.[Total (Debit Only)],
dbo_V_HB_Outsourced_AR.[Over 90],
dbo_V_HB_Outsourced_AR.COL_AGNCY_NAME
FROM dbo_V_HB_Outsourced_AR
WHERE (((dbo_V_HB_Outsourced_AR.REPORTING_DATE) Between [FirstDayPriorMonth] And [LastDayPriorMonth])
AND ((dbo_V_HB_Outsourced_AR.COL_AGNCY_NAME)='ADVICARE'
Or (dbo_V_HB_Outsourced_AR.COL_AGNCY_NAME)='CONVERGENT'
Or (dbo_V_HB_Outsourced_AR.COL_AGNCY_NAME)='MEDALIST MANAGEMENT')
AND ((dbo_V_HB_Outsourced_AR.LOC_ID)=1010
Or (dbo_V_HB_Outsourced_AR.LOC_ID)=1011
Or (dbo_V_HB_Outsourced_AR.LOC_ID)=1012
Or (dbo_V_HB_Outsourced_AR.LOC_ID)=1013
Or (dbo_V_HB_Outsourced_AR.LOC_ID)=1014));
My question is why can I see the function calls on the query criteria line, but have to change the query in the SQL view? I am still working on getting the function calls to work in the SQL view as well.
I am calling an ACCESS query from AUTOMATE, a controller program.
Unfortunately, a query run from outside an Access application session can not use VBA user-defined functions. That means your FirstDayPriorMonth and LastDayPriorMonth functions are not available in that situation.
Fortunately both those functions are based on fairly simple DateSerial expressions, and DateSerial is usable in any Access query regardless of whether the query is run from within or outside an Access session.
So instead of this ...
Between [FirstDayPriorMonth] And [LastDayPriorMonth]
Use this ...
BETWEEN DateSerial(Year(Date()), Month(Date()) -1, 1)
AND DateSerial(Year(Date()), Month(Date()), 0)
"<first part of query>
from dbo_V_HB_Outsourced_AR
WHERE (((dbo_V_HB_Outsourced_AR.REPORTING_DATE) Between #" & FirstDayPriorMonth() & "# and #" & LastDayPriorMonth() & "#" &
"rest of query"
You need to construct your query string by concatenating the results of the function calls (which should not be in square brackets as they are not field names). Also, dates need a "#" side on each side (in MS-Access).
I am working on a query to pull all turnover in the past calendar year that is going to be used daily. Rather than going in and having to change the date each time I would just like to be able to run the query and have it automatically only pull the last 365 days worth of data. The code itself looks like:
SELECT O867IA_VJOBHST.SYS_EMP_ID_NR, O867IA_VJOBHST.REC_EFF_STT_DT, O867IA_VJOBHST.EMP_ACN_TYP_CD
FROM O867IA_VJOBHST
WHERE (((O867IA_VJOBHST.EMP_ACN_TYP_CD)="HIR"));
Where the REC_EFF_STT_DT is the date the ACN_TYP_CD occurred, in this case when they were HIR (Hired)
Any Ideas?
Access SQL provides Date() and DateAdd() functions. You can work out what you need from those functions in the Immediate window ...
? Date()
9/9/2013
? DateAdd("d", -365, Date())
9/9/2012
Then you can filter REC_EFF_STT_DT on the same date range in a query like this ...
SELECT o.SYS_EMP_ID_NR, o.REC_EFF_STT_DT, o.EMP_ACN_TYP_CD
FROM O867IA_VJOBHST AS o
WHERE
o.REC_EFF_STT_DT BETWEEN DateAdd('d', -365, Date()) AND Date();
I am trying to use Reporting Services to create a report displaying the call activity of various sales reps. The report will group by extension and then date of call. For each group of call dates (that is, all the calls for a particular date), I want to display some totals. One of the totals I want to display is the total number of calls whose duration greater than 2 minutes. I can see how to use the RunningValue function to keep a running total of ALL calls for the date, but I'm not sure how to make that conditional on the length of call. Any ideas?
UPDATE: The checked answer below did it... I used a case statement in linq like this:
var qry = from Q in c.CallList
select new
{
Q.Extension,
Q.CallDate,
Q.Duration
CallCountOverTwoMinutes = Q.duration > 120 ? 1 : 0,
};
Then I sum the value of CallCountOverTwoMinutes. Thanks for the help, Chris!
The easiest way would be to pass the value as part of the dataset. For example, using SQL:
SELECT Extension, CallDate, Duration,
CASE WHEN Duration > 2 THEN 1 END AS CallsOver2Mins
FROM CallTable
Then just sum on CallsOver2Mins.