I'm new to SSIS and I'm trying to convert a GetDate() to string "DD-MM-YYYY". This is the expression I've built so far:
(DT_WSTR, 8) DAY( GETDATE()) + "-" + (DT_WSTR, 8) (MONTH(GETDATE()) - 1) + "-" + (DT_WSTR, 8) YEAR(GETDATE())
The problem I've got is Month() converts the Month "23-4-2013" to a single character when I want it in Double character, same as day. How do i make it into a double character no matter what month it is?
For SSIS you could go with:
RIGHT("0" + (DT_STR, 2, 1252) DATEPART("dd" , GETDATE()), 2) + "-" + RIGHT("0" + (DT_STR, 2, 1252) DATEPART("mm" , GETDATE()), 2) + "-" + (DT_STR, 4, 1252) DATEPART("yy" , GETDATE())
Expression builder screen:
Something simpler than what #Milen proposed but it gives YYYY-MM-DD instead of the DD-MM-YYYY you wanted :
SUBSTRING((DT_STR,30, 1252) GETDATE(), 1, 10)
Expression builder screen:
If, like me, you are trying to use GETDATE() within an expression and have the seemingly unreasonable requirement (SSIS/SSDT seems very much a work in progress to me, and not a polished offering) of wanting that date to get inserted into SQL Server as a valid date (type = datetime), then I found this expression to work:
#[User::someVar] = (DT_WSTR,4)YEAR(GETDATE()) + "-" + RIGHT("0" + (DT_WSTR,2)MONTH(GETDATE()), 2) + "-" + RIGHT("0" + (DT_WSTR,2)DAY( GETDATE()), 2) + " " + RIGHT("0" + (DT_WSTR,2)DATEPART("hh", GETDATE()), 2) + ":" + RIGHT("0" + (DT_WSTR,2)DATEPART("mi", GETDATE()), 2) + ":" + RIGHT("0" + (DT_WSTR,2)DATEPART("ss", GETDATE()), 2)
I found this code snippet HERE
for the sake of completeness, you could use:
(DT_STR,8, 1252) (YEAR(GetDate()) * 10000 + MONTH(GetDate()) * 100 + DAY(GetDate()))
for YYYYMMDD or
RIGHT("000000" + (DT_STR,8, 1252) (DAY(GetDate()) * 1000000 + MONTH(GetDate()) * 10000 + YEAR(GetDate())), 8)
for DDMMYYYY (without hyphens). If you want / need the date as integer (e.g. for _key-columns in DWHs), just remove the DT_STR / RIGTH function and do just the math.
#[User::path] ="MDS/Material/"+(DT_STR, 4, 1252) DATEPART("yy" , GETDATE())+ "/" + RIGHT("0" + (DT_STR, 2, 1252) DATEPART("mm" , GETDATE()), 2) + "/" + RIGHT("0" + (DT_STR, 2, 1252) DATEPART("dd" , GETDATE()), 2)
Try this. It will give you DD-MM-YYYY
SUBSTRING((DT_WSTR, 30)getdate(), 9,2)+"-"+SUBSTRING((DT_WSTR, 30)getdate(),6,2)+"-"+SUBSTRING((DT_WSTR,30)getdate(), 1,4)
Related
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.
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.
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)
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)
hi I have taking flatfile source name dynamically I.e. filename like "source 2011-08-11"
I'm creating expression builder for taking most recent file as per filename.
I did like
Created one variable which is having folder path : C\backup\
now inside expression builder how can i add date ???
i tried like
#[User::DirPath]+"source"+ (DT_STR,4,1252)YEAR( DATEADD( "dd", -1, getdate() ))
+"-"+(DT_STR,4,1252)MONTH( DATEADD( "dd",-1, getdate() ))+"-"+(DT_STR,4,1252)
DAY(DATEADD( "dd", -1, getdate() )) +".CSV"
which is wrong please give me the expression which gives me output :
"source 2011-08-11"
Correct expression is
"source " + (DT_STR,4,1252)DATEPART( "yyyy" , getdate() ) + "-" +
RIGHT("0" + (DT_STR,4,1252)DATEPART( "mm" , getdate() ), 2) + "-" +
RIGHT("0" + (DT_STR,4,1252)DATEPART( "dd" , getdate() ), 2) +".CSV"
Looks like you created a separate question. I was answering your other question How to change flat file source using foreach loop container in an SSIS package? with the same answer. Anyway, here it is again.
Create two string data type variables namely DirPath and FilePath. Set the value C:\backup\ to the variable DirPath. Do not set any value to the variable FilePath.
Select the variable FilePath and select F4 to view the properties. Set the EvaluateAsExpression property to True and set the Expression property as #[User::DirPath] + "Source" + (DT_STR, 4, 1252) DATEPART("yy" , GETDATE()) + "-" + RIGHT("0" + (DT_STR, 2, 1252) DATEPART("mm" , GETDATE()), 2) + "-" + RIGHT("0" + (DT_STR, 2, 1252) DATEPART("dd" , GETDATE()), 2)
"source " + LEFT((DT_WSTR, 50)(DT_DBTIMESTAMP)GETDATE(), 10) +".CSV"