Evaluate Expression with expression builder - ssis

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.

Related

SQL Server - convert number to time

I am using SQL Server 2008 R2.
I have one field storing time as integer value.
I want to convert integer to time as follows.
IntegerValue TimeValue
-------------------------
1 00:01 AM
11 00:11 AM
123 01:23 AM
541 05:41 AM
1317 13:17 PM
Please reply.
Thanks.
SELECT
CONVERT(VARCHAR, coulmn / 100) + ':' + RIGHT('00' + CONVERT(VARCHAR, column% 100),2)
FROM YourTable
Try like this,
It is almost similar to #Mukund answer
SELECT c AS IntegerValue
,CONVERT(VARCHAR, c / 100) + ':' + RIGHT('00' + CONVERT(VARCHAR, c % 100), 2) AS TimeValue
FROM (
VALUES (1)
,(11)
,(123)
,(541)
,(1317)
) t(c)

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.

DATEDIFF() seconds with millisecond to float or decimal

How to convert the below code to float or decimal type?
SELECT DATEDIFF(ss, StartTime, GETDATE()) + '.' +
DATEDIFF(ms, StartTime, GETDATE())
This is what I needed. CONVERT(float, DATEDIFF(ms, StartTime, GETDATE()) / 1000.0)
The code i my question was completely wrong because DATEDIFF(ms, StartTime, GETDATE()) returns total number of milisecond between the two dates and not as i thought only difference in the milliseconds part.
The code im my question would work if I used the DATEPART instead DATEDIFF in both expressions:
DATEPART(ss, GETDATE()) - DATEPART(ss, #StartTime) + '.'
+ DATEPART(ms, GETDATE()) - DATEPART(ms, #StartTime)

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)

SSIS expression to find previous Friday

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)