I would like to to format one of my data fields to be in the date format of MMM YYYY. For example, if "201209" is being returned, then I would like it to be displayed as Sep 2012. On the SQL side I am using a CAST to only look at the Year and Month, normally this field would look like "20120914". Here is what I used in the procedure:
cast(left(cast(TransactionDateKey as varchar(100)), 6) as int)
Within SSRS, I use the following code:
=Format(DateValue(MonthName(Right(Fields!Month.Value, 2))
+ "," +
Left(Fields!Month.Value,4)), "Y")
however the output is "September 2012", how would I go about get the abbreviation "Sep", instead of the full month name?
Another Option is Format() in SQL Server
Select Format(CONVERT (date,convert(char(8),TransactionDateKey )),'MMM yyyy')
For example
Select Format(CONVERT (date,convert(char(8),20120914 )),'MMM yyyy')
-- Returns Sep 2012
I should note that Format() is not known for its performance, but does offer some nice features
If you are returning an actual datetime field in your dataset - which is what your TransactionDateKey appears to be - you can handle the formatting completely in an SSRS expression using format:
=format(Fields!TransactionDateKey.Value,"MMM yyyy")
If you are returning your yyyyMM for grouping purposes, there is nothing stopping you grouping on a datetime value of the first of the month for TransactionDateKey within your SQL:
select dateadd(m,datediff(m,0,TransactionDateKey),0) as FirstDayOfTheMonth
If you absolutely need to return a varchar in the format yyyyMM, you can convert it to MMM yyyy, though you first need to convert it to a date in your expression - slashes and all - before SSRS will play ball:
=format(cdate(left(Fields!Month.Value,4) & "/" & right(Fields!Month.Value,2) & "/01"), "MMM yyyy")
In SSRS you can use:
=StrConv(LEFT(
MONTHNAME(REPLACE(RIGHT(Fields!Month.Value,2),"0","")),3
),vbProperCase,NOTHING) & " " & LEFT(Fields!Month.Value,4)
Which returns Sep 2012 for 201209.
Let me know if this helps.
Related
I have built a SSRS report which combines two int columns of data (i.e.: 6 & 2017) to form a valid month and year result in a column of the SSRS report (i.e.: June 2017) using the following statement: Format(CDate(Fields!MonthEarnedId.Value & "/" & Fields!YearEarned.Value)
That works as desired, however some of the int values for the month have a value of 13 which is supposed to represent all months of the year so that the required format for these values in the report is such as "All 2017". I thought an IIF statement such as the following would specify the necessary formating for the exclusion, however the rows with a month value of 13 simply display #error. Any suggestions would be greatly appreciated!
=iif(Fields!MonthEarnedId.Value < 13, Format(CDate(Fields!MonthEarnedId.Value & "/" & Fields!YearEarned.Value), "MMM yyyy"), "All " & Fields!YearEarned.Value)
The issue in expression is CDate() when the 13 occurs. As per the design in SSRS, IIF evaluates TRUE and FALSE both and in your case when the 13 is selected as MonthEarnedId then CDate() tries to convert 13/2017 in MMM yyyy format which is invalid and throws #Error.
I would suggest you to convert the int into month names in your SQL query and bring the data to just concatenate in SSRS but if you strictly have to do it in SSRS then below is the expression you have to put in.
=SWITCH(Fields!MonthEarnedId.Value=1,"Jan"
,Fields!MonthEarnedId.Value=2,"Feb"
,Fields!MonthEarnedId.Value=3,"Mar"
,Fields!MonthEarnedId.Value=4,"Apr"
,Fields!MonthEarnedId.Value=5,"May"
,Fields!MonthEarnedId.Value=6,"Jun"
,Fields!MonthEarnedId.Value=7,"Jul"
,Fields!MonthEarnedId.Value=8,"Aug"
,Fields!MonthEarnedId.Value=9,"Sep"
,Fields!MonthEarnedId.Value=10,"Oct"
,Fields!MonthEarnedId.Value=11,"Nov"
,Fields!MonthEarnedId.Value=12,"Dec"
,Fields!MonthEarnedId.Value=13,"All"
) & " " & CStr(Fields!YearEarned.Value.Value)
You are casting that field type to a date variant to apply formatting in doing so, you are also changing the data type of the column from string to datetime. The error occurs when the renderer attempts to cast the string value "All 2017" into a corresponding date time. To get around this you need to cast it back to a string subtype using CStr() or some other way.
=iif(Fields!MonthEarnedId.Value < 13, CStr(Format(CDate(Fields!MonthEarnedId.Value & "/" & Fields!YearEarned.Value), "MMM yyyy")), "All " & Fields!YearEarned.Value)
I'm using this query
SELECT convert(nvarchar(MAX), GETDATE(), 22) AS Date
Result: 08/05/16 12:23:08 PM
But I want result like this 8/5/2016 12:23:08 PM
dd/mm/yyyy hh:mm:ss a
As of SQL Server 2012 the FORMAT function is available allowing you to specify the format of data types and is locale-aware so it will consider date formatting in relation to the session's language or optional culture parameter.
You can achieve your custom formatting like so: FORMAT(GETDATE(), 'd/M/yyyy hh:mm:ss tt')
Note your requested format dd/mm/yyyy hh:mm:ss a is incorrect as in the case of single digits you want to remove zero padding i.e. 10/8/2016 not 10/08/2016. That's why in the format string I use only d and M.
Also, pay attention to #GarethD comment about the cost on larger datasets.
You could use the FORMAT function in T-SQL : https://msdn.microsoft.com/en-us/library/hh213505(v=sql.120).aspx
Here is the code :
SELECT FORMAT(GETDATE(), 'd/M/yyyy hh:mm:ss tt')
I have 2 x fields T1.period and T1.year both have data type smallint
Using SQL Management Studio 2014 how may I Concatenate them AND return result as a DATE type?
Also, T1.period has values 1 to 12 how may I pad this out to 01 to 12 ... or will changing to date type sort this out?
Much appreciated!
Sample data ...
period yr
1 2015
2 2009
12 2009
11 2010
10 2011
Result will be ...
Date
01/01/2015
01/02/2009
01/12/2009
01/11/2010
01/10/2011
Thanks!
Looks terrible struggling to get it into lists - sorry :(
Converting Your Values The Old Fashioned Way
Assuming that your t1.period value actually just represents a month, you could consider just converting your values to strings and then converting that concatenated result into a date via the CAST() function :
SELECT CAST(CAST(t1.year AS VARCHAR) + '-' + CAST(t1.period AS VARCHAR) + '-1' AS DATE)
This will build a string that looks like {year}-{month}-1, which will then be parsed as a DATE and should give you the first date of the given month/year.
Using The DATEFROMPARTS() Function
SQL Server 2012 and above actually support a DATEFROMPARTS() function that will allow you to pass in the various parts (year, month, day) to build a corresponding DATE object, with a much easier to read syntax :
SELECT DATEFROMPARTS(t1.year,t1.period,1)
Additionally, if you needed a DATETIME object, you could use DATETIMEFROMPARTS() as expected.
Can you please suggest me how to display date format for a date column as MM/DD/YYYY HH:MM:SS Z format in ssrs ?
I have googled but i am not getting exact format.
I was wondering how to do this(how to display date in UTC format ('MM/dd/yyyy HH:mm:ss Z'). But in Sql Server 2012 it's too easy. Can be done like this.
In Sql query we can do like below
SELECT EmpName, FORMAT(ApprovedDate, 'MM/dd/yyyy HH:mm:ss Z') as ApprovedDate From Employee
Or in SSRS report expression we can do like below
Format(Fields!ApprovedDate.Value, "MM/dd/yyyy hh:mm:dd" & " Z")
EmpName and ApprovedDate are the columns of Employee Table.
Hope this might be helpful for others
=System.TimeZone.CurrentTimeZone.ToUniversalTime(First(Fields!NOW.Value, "DataSet1"))
This works for me:
=Format(Fields!FeildName.Value, "MM/dd/yyyy hh:mm:ss tt")
I've a SQl Query. I'm using dropdownlist to display the dates. I'd like to display the month in MMMM format.
SELECT DISTINCT[drdates] (CONVERT(CHAR(4), [drdates], 100) + CONVERT(CHAR(4), [drdates], 120)) FROM [DRReceive_20141229]
SELECT DISTINCT UPPER(LEFT(DATENAME(MONTH,MONTH([drdates])),4))
+ CONVERT(CHAR(4), [drdates], 120)
FROM [DRReceive_20141229]
On a side note I have never seen date values being formatted as MMMMyyyy, a rather strange format to show date values.
But if you wanted something rather simple or usual format like MMMyyyy and if you are using sql server 2012 or later version you can do the following ...
SELECT DISTINCT UPPER(FORMAT ( [drdates], 'MMMyyyy' ))
FROM [DRReceive_20141229]
This will help you to retrieve months first four characters :
select CONVERT(CHAR(4),DATENAME(MONTH, [drdates])) + CONVERT(CHAR(4),[drdates], 120)) [DRReceive_20141229];