SSIS expression to find previous Friday - sql-server-2008

Using SQL Server 2008, I have built an SSIS variable with dynamic value base on current date. I would like to have its value to be Friday if the current day is Monday, and here is the expression built:
DATEPART("dw",GETDATE()) != 2?
RIGHT("0" + (DT_WSTR,2)MONTH(DATEADD("dd", -1, GETDATE())), 2) + "/"
+ RIGHT("0" + (DT_WSTR,2)DAY(DATEADD("dd", -1, GETDATE())), 2) + "/" +
(DT_WSTR,4)YEAR(DATEADD("dd", -1, GETDATE())) : RIGHT("0" + (DT_WSTR,2)MONTH(DATEADD("dd", -1, GETDATE())), 2) + "/"
+ RIGHT("0" + (DT_WSTR,2)DAY(DATEADD("dd", -3, GETDATE())), 2) + "/" +
(DT_WSTR,4)YEAR(DATEADD("dd", -1, GETDATE()))
Issue: it is not accurate when the month or year changed. Is there any better way to do this? Help would be appreciated. Thanks.

Will this work (you can substitute GETDATE() for #date, I just used that to easily test out different dates)
DECLARE #date DATETIME
SET #date = '2013-01-14'
SELECT
PrevFriday = CASE WHEN DATEPART(weekday, #date) <> 2 THEN #date
ELSE DATEADD(DAY, -3, #date)
END
UPDATE: Here is same, but done in SSIS Variable Expression:
DATEPART("dw", GETDATE()) != 2?
GETDATE():
DATEADD("dw", -3, GETDATE())
UPDATE #2: Here is how to return the previous Friday for ANY date, not just Monday
SELECT DATEADD(DAY, -1 - (DATEPART(weekday, #date) % 7), #date)

Related

Evaluate Expression with expression builder

I am new to ssis and I would like to evaluate this using expression builder to get the current date in bigin. Any idea?
DATEPART(second, getdate()) +
DATEPART(minute, getdate()) * 100 +
DATEPART(hour, getdate()) * 10000 +
DATEPART(day, getdate()) * 1000000 +
DATEPART(month, getdate()) * 100000000 +
DATEPART(year, getdate()) * 10000000000
The error is
Expression cannot be evaluated.
------------------------------
ADDITIONAL INFORMATION:
The expression contains unrecognized token "second". If "second" is a variable, it should be expressed as "#second". The specified token is not valid. If the token is intended to be a variable name, it should be prefixed with the # symbol.
Attempt to parse the expression "DATEPART(second, getdate()) +
DATEPART(minute, getdate()) * 100 +
DATEPART(hour, getdate()) * 10000 +
DATEPART(day, getdate()) * 1000000 +
DATEPART(month, getdate()) * 100000000 +
DATEPART(year, getdate()) * 10000000000" failed and returned error code 0xC00470A4. The expression cannot be parsed. It might contain invalid elements or it might not be well-formed. There may also be an out-of-memory error.
(Microsoft.DataTransformationServices.Controls)
------------------------------
The syntax for DATEPART in an SSIS expression is different then the syntax in TSQL. Because that helps makes our lives harder than necessary.
The first argument, datepart, has to be enclosed in double quotes. This should get you closer to what you're after.
DATEPART("second", getdate()) +
DATEPART("minute", getdate()) * 100 +
DATEPART("hour", getdate()) * 10000 +
DATEPART("day", getdate()) * 1000000 +
DATEPART("month", getdate()) * 100000000 +
DATEPART("year", getdate()) * 10000000000
The next error you'll get is:
The literal "10000000000" is too large to fit into type DT_I4. The magnitude of the literal overflows the type.
You can fix that one by adding an L to the end of the last literal:
DATEPART("second", getdate()) +
DATEPART("minute", getdate()) * 100 +
DATEPART("hour", getdate()) * 10000 +
DATEPART("day", getdate()) * 1000000 +
DATEPART("month", getdate()) * 100000000 +
DATEPART("year", getdate()) * 10000000000L
And that evaluated correctly to 20200611143622.

Yesterday's date in SSIS package setting in variable through expression

I am setting a variable in SSIS package and I'm using this expression:
DATEPART("yyyy", GETDATE())*10000
+ DATEPART("month", GETDATE())*100
+ DATEPART("day",GETDATE())
The expression will give me a variable value like 'yyyymmdd'. My problem is that I want yesterday's date.
For example on 11/1/2014 it should be 20141031
You can use DATEADD function
your expression would be :
DATEPART("yyyy", DATEADD( "day",-1, GETDATE()))*10000 + DATEPART("month", DATEADD( "day",-1, GETDATE())) * 100 + DATEPART("day", DATEADD( "day",-1, GETDATE()))
This will give yesterday's date
(DT_WSTR, 4) YEAR(DATEADD("day",-1,GETDATE()))
+RIGHT("0" + (DT_WSTR, 2) DATEPART("MM", DATEADD("day", -1, GETDATE())),2)
+RIGHT("0" + (DT_WSTR, 2) DATEPART("DD", DATEADD("day", -1, GETDATE())),2)
less code...
CONVERT(varchar(8), DATEADD(dd,-1,GETDATE()),112)
The following example gives the date yesterday with hours and minutes:
2015-09-06-14-40
(DT_WSTR, 4) Year(dateadd("day",-1,getdate())) + "-"
+ ( month(dateadd("day",-1,getdate())) < 10 ? "0" + (DT_WSTR, 4) month(dateadd("day",-1,getdate())):(DT_WSTR, 4) month(dateadd("day",-1,getdate())))
+ "-" +( day(dateadd("day",-1,getdate())) <10 ? "0" + (DT_WSTR, 4) day(dateadd("day",-1,getdate())):(DT_WSTR, 4) day(dateadd("day",-1,getdate())))
+ "-" + right("0"+(DT_WSTR,4)datepart("hh",getdate()),2)
+ "-" + right("0"+(DT_WSTR,4)datepart("mi",getdate()),2)
(DT_WSTR, 4) YEAR(GETDATE()) +RIGHT("0" + (DT_WSTR, 2) MONTH(GETDATE()),2) +RIGHT("0" + (DT_WSTR, 2) DATEPART("DD", DATEADD("day", -1, GETDATE())),2)
Above will also give you date one day before. Main issue issue doing plus or minus gives truncation error in expression.
RIGHT("0" + (DT_WSTR, 2) DATEPART("DD", DATEADD("day", -1, GETDATE())),2)
will not break the expression.

How to get previous month's number

I have the following SSIS expression:
Right("0" + (DT_STR,4,1252) DatePart("m",getdate()),2) + RIGHT("0" + (DT_STR,4,1252) DatePart("yyyy",getdate()),2)
which gives me 0614.
How can I change the month code so it always gives me the previous month's number?
Actually, I have no idea what your expression is - it looks like a mix of SQL and SSRS VBA. It may be SSIS as #mmarie suggests.
So I'll give you two answers - SQL (which you can use in the query expression in SSRS) and the actual VBA SSRS expression.
To adjust what you have to SQL to get the previous month, you would use:
Right('0' + CAST(DatePart(mm, DateAdd(mm, -1, getdate())) AS VARCHAR), 2)
+ RIGHT('0' + CAST(DatePart(yy, DateAdd(mm, -1, getdate())) AS VARCHAR), 2)
To have this as a SSRS expression, you would use:
=Right("0" & CStr(DatePart(DateInterval.Month, DateAdd(DateInterval.Month, -1, Today))), 2)
& Right("0" & CStr(DatePart(DateInterval.Year, DateAdd(DateInterval.Month, -1, Today))), 2)
To convert your original SSIS expression, you would replace getdate() with DateAdd(mm, -1, getdate()) like so:
Right("0" + (DT_STR,4,1252) DatePart("m", DateAdd(mm, -1, getdate())), 2)
+ RIGHT("0" + (DT_STR,4,1252) DatePart("yyyy", DateAdd(mm, -1, getdate())), 2)

Access to T-SQL 2008 date serial

Could any one help me convert this Microsoft access query to Tsql 2008:
SELECT
dbo_Invoiceable__c.NAME AS Invoiceable,
dbo_Invoiceable__c.Payment_is_due__c,
dbo_Invoiceable__c.Day_Due__c,
DATE () AS [Invoice Date],
DateSerial(Year(DATE ()), Month(DATE ()), [Day_Due__c])
AS [On a day of the month],
DateAdd("d", [day_due__c], DATE ())
AS [In a given number of days],
DateAdd("d", [day_due__c] - 1, DateAdd("m", 1, DateSerial(Year(DATE ()), Month(DATE ()), 1)))
AS [No of days after EOM],
IIf([Payment_is_due__c] = "In a given number of days", [In a given number of days], "") & IIf([Payment_is_due__c] = "On a day of this month", [On a day of the month], "") & IIf([Payment_is_due__c] = "No of days after EOM", [No of days after EOM], "")
AS [Due Date]
FROM dbo_Invoiceable__c;
I have tried this:
SELECT *,
CASE
WHEN [Payment_is_due__c] = 'In a given number of days' THEN [In a given number of days] ELSE ''
END + CASE
WHEN [Payment_is_due__c] = 'On a day of this month' THEN [On a day of the month] ELSE ''
END + CASE
WHEN [Payment_is_due__c] = 'No of days after EOM' THEN [No of days after EOM] ELSE ''
END AS [Due Date]
FROM (SELECT [SalesForce_LIVE.dbo.Invoiceable__c].[Name] AS Invoiceable,
[SalesForce_LIVE.dbo.Invoiceable__c].[Payment_is_due__c],
[SalesForce_LIVE.dbo.Invoiceable__c].[Day_Due__c],
CONVERT (DATETIME, CONVERT (VARCHAR, GETDATE(), 1), 1) AS [Invoice Date],
dbo.DateSerial(Year(CONVERT (DATETIME, CONVERT (VARCHAR, GETDATE(), 1), 1)), Month(CONVERT (DATETIME, CONVERT (VARCHAR, GETDATE(), 1), 1)), [Day_Due__c]) AS [On a day of the month],
DATEADD(d, [day_due__c], CONVERT (DATETIME, CONVERT (VARCHAR, GETDATE(), 1), 1)) AS [In a given number of days],
DATEADD(d, [day_due__c] - 1, DATEADD(m, 1, dbo.DateSerial(Year(CONVERT (DATETIME, CONVERT (VARCHAR, GETDATE(), 1), 1)), Month(CONVERT (DATETIME, CONVERT (VARCHAR, GETDATE(), 1), 1)), 1))) AS [No of days after EOM]
FROM SalesForce_LIVE.dbo.Invoiceable__c) AS Nested1;
But still cant get the dateserial working in Tsql
DECLARE #firstdayofmonth datetime;
DECLARE #lastdayofmonth datetime;
SET #firstdayofmonth = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0);
SET #lastdayofmonth = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0));
SELECT *,
CASE WHEN [Payment_is_due__c]='In a given number of days' THEN [In a given number of days] ELSE '' END +
CASE WHEN [Payment_is_due__c]='On a day of this month' THEN [On a day of the month] ELSE '' END +
CASE WHEN [Payment_is_due__c]='No of days after EOM' THEN [No of days after EOM] ELSE '' END AS [Due Date]
FROM (SELECT SalesForce_LIVE.dbo.Invoiceable__c.[Name] AS Invoiceable, SalesForce_LIVE.dbo.Invoiceable__c.[Payment_is_due__c], SalesForce_LIVE.dbo.Invoiceable__c.[Day_Due__c],
CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),1),1) AS [Invoice Date],
DATEADD(DAY,[day_due__c],#firstdayofmonth-1) AS [On a day of the month],
DATEADD(DAYOFYEAR,[day_due__c],CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),1),1)) AS [In a given number of days],
DATEADD(DAY,[day_due__c],#lastdayofmonth) AS [No of days after EOM]
FROM SalesForce_LIVE.dbo.Invoiceable__c
) AS Nested1

Getting the Starting and ending date of week? In Sql server?

In sql, how to get the starting and ending date of week for the particular month. Can you help me.
The following will work whatever you consider the first day of the week (sunday, monday etc), just ensure you use SET DATEFIRST if you want to change from the default. SET DATEFIRST 1 will make the first day of the week monday.
SELECT DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [WeekStart],
DATEADD(DAY, 7 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [WeekEnd]
EDIT
I have just re-read you request and I think you may be after something different to what I have given above. If you want the day of the week of the first and last of the month this will do the trick:
SELECT DATENAME(WEEKDAY, DATEADD(DAY, 1 - DATEPART(DAY, GETDATE()), GETDATE())) [FirstDayOfMonth],
DATENAME(WEEKDAY, DATEADD(DAY, - DATEPART(DAY, DATEADD(MONTH, 1, GETDATE())), DATEADD(MONTH, 1, GETDATE()))) [LastDayOfMonth]
Your requirement is unclear: what do you consider to be the first and last days of a week? Sunday and Saturday? Monday and Friday? Monday and Sunday? But the best solution for many date-related queries is to create a calendar table. That gives you the ability to easily set the first and last days of weeks, months and years according to your definition and even in multiple formats if necessary.
This is especially useful if you work with concepts like financial years, business days, and so on where the definitions vary by country and even company. It is also the easiest solution when there are exceptions to your logic, e.g. the first week of the year has different rules than a 'normal' week.
With a calendar table you could pre-populate the first and last days of the week and then your query could be something like this:
-- first/last days stored as dates in their own columns
select FirstDayOfThisWeek, LastDayOfThisWeek
from dbo.Calendar
where BaseDate = #SomeDate
-- first/last days stored as bit flags
select max(BaseDate)
from dbo.Calendar
where BaseDate <= #SomeDate and IsFirstDayOfWeek = 0x1
Below is a case statement you can use to create a Weekbegin for your date value. It will make your weeks start on Monday or Tuesday or Wed... etc. dependant on what day of the week you have set as #pDate.
Create parameters
DECLARE #pDate Date = NULL --Replace with your date, which will also be the first day of the week
DECLARE #pDatePart SMALLINT = DATEPART(dw, #pDate)
Then put this case Statement after your select
CASE
WHEN DATEPART(dw, CAST(DATEVALUE AS DATE)) BETWEEN #pDatePart AND 7 THEN DATEADD(d, (DATEPART(dw, CAST(DATEVALUE AS DATE))*-1)+#pDatePart, CAST(DATEVALUE AS DATE))
WHEN DATEPART(dw, CAST(DATEVALUE AS DATE)) BETWEEN 1 AND #pDatePart-1 THEN DATEADD(d, (DATEPART(dw, CAST(DATEVALUE AS DATE))*-1)+(#pDatePart-7), CAST(DATEVALUE AS DATE))
END
AS DynamicWeekBegin
You can always get the final date of the week, just by using dateadd(d, 6, THE CASE STATEMENT)
Here i given
Week,month,quarter,half year,year start and end date.
Select CONVERT(varchar(50), GETDATE(),105) 'GETDATE' ,
CONVERT(varchar(50), DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)),105) [WeekStart],
CONVERT(varchar(50),DATEADD(DAY, 8 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) ,105)[WeekEnd],
CONVERT(varchar(50),DATEADD(dd, -DAY(getdate()) + 1, getdate()),105) MonthStart,
CONVERT(varchar(50),DATEADD(dd, -DAY(DATEADD(mm, 1, getdate())), DATEADD(mm, 1, getdate())),105) MonthStart,
CONVERT(varchar(50), DATEADD(q, DATEDIFF(q, 0, GETDATE()), 0),105) AS 'QStart Date',
CONVERT(varchar(50), DATEADD(d, -1, DATEADD(q, DATEDIFF(q, 0, GETDATE()) + 1,0)),105) AS 'QEnd Date',
CONVERT(varchar(50), CAST(CAST(((((MONTH(GETDATE()) - 1) / 6) * 6) + 1) AS VARCHAR) + '-1-' + CAST(YEAR(GETDATE()) AS VARCHAR) AS DATETIME),105) StartOfHalfYear,
CONVERT(varchar(50), CAST(CAST(((((MONTH(GETDATE()) - 1) / 6) * 6) + 6) AS VARCHAR) + '-1-' + CAST(YEAR(GETDATE()) AS VARCHAR) AS DATETIME),105)EndOfHalfYear,
CONVERT(varchar(50), DATEADD(yy, DATEDIFF(yy,0,getdate()), 0),105) AS StartOfYear,
CONVERT(varchar(50), DATEADD(yy, DATEDIFF(yy,0,getdate()) + 1, -1),105) AS EndOfYear