Formatting a date in an MDX query

Thought I'd add a note as I was getting confused with the built-in function "FormatDateTime()". The example is shown as:

copyraw
=FormatDateTime(Fields!BirthDate.Value, DateFormat.ShortDate)
  1.  =FormatDateTime(Fields!BirthDate.Value, DateFormat.ShortDate) 

The other formats are:
copyraw
=FormatDateTime(Fields!BirthDate.Value, DateFormat.GeneralDate)
=FormatDateTime(Fields!BirthDate.Value, DateFormat.LongDate)
=FormatDateTime(Fields!BirthDate.Value, DateFormat.ShortDate)
=FormatDateTime(Fields!BirthDate.Value, DateFormat.LongTime)
=FormatDateTime(Fields!BirthDate.Value, DateFormat.ShortTime)
  1.  =FormatDateTime(Fields!BirthDate.Value, DateFormat.GeneralDate) 
  2.  =FormatDateTime(Fields!BirthDate.Value, DateFormat.LongDate) 
  3.  =FormatDateTime(Fields!BirthDate.Value, DateFormat.ShortDate) 
  4.  =FormatDateTime(Fields!BirthDate.Value, DateFormat.LongTime) 
  5.  =FormatDateTime(Fields!BirthDate.Value, DateFormat.ShortTime) 

Unfortunately if you are using US dates and want the report to use a specific European date format and you spend as long as I did searching the web for a solution, then ignore all the above.

Another built-in function is the text-formatter

copyraw
=Format(Fields!myDateTime.Value, "M/d/yy") ... 6/15/09
=Format(Fields!myDateTime.Value, "M/d/yyyy h:mmtt") ... 6/15/2009 2:45PM
=Format(Fields!myDateTime.Value, "MM/dd/yy HH:mm") ... 06/15/09 14:45
=Format(Fields!myDateTime.Value, "MMM d, yyyy") ... Jun 15, 2009
=Format(Fields!myDateTime.Value, "Short Date") ... 6/15/2009
=Format(Fields!myDateTime.Value, "Long Date") ... Monday, June 15, 2009
=Format(Fields!myDateTime.Value, "ddd dd/MM/yyyy") ... Mon 04/04/2011
  1.  =Format(Fields!myDateTime.Value, "M/d/yy") ... 6/15/09 
  2.  =Format(Fields!myDateTime.Value, "M/d/yyyy h:mmtt") ... 6/15/2009 2:45PM 
  3.  =Format(Fields!myDateTime.Value, "MM/dd/yy HH:mm") ... 06/15/09 14:45 
  4.  =Format(Fields!myDateTime.Value, "MMM d, yyyy") ... Jun 15, 2009 
  5.  =Format(Fields!myDateTime.Value, "Short Date") ... 6/15/2009 
  6.  =Format(Fields!myDateTime.Value, "Long Date") ... Monday, June 15, 2009 
  7.  =Format(Fields!myDateTime.Value, "ddd dd/MM/yyyy") ... Mon 04/04/2011 

European and Now:
The above is about formatting dates and times but if you want a NOW timestamp in a specific format, try...
copyraw
=Format(now(), "ddd, dd MMMM yyyy HH:mm:ss")
=Format(now(), "dd/MM/yyyy")
  1.  =Format(now(), "ddd, dd MMMM yyyy HH:mm:ss") 
  2.  =Format(now(), "dd/MM/yyyy") 
The reserved expression letters used are on the MSDN website at http://msdn.microsoft.com/en-us/library/ms146084.aspx

NOW() vs TODAY()

Noticed this when setting date as the parameter and trying to specify default date as today's in the format DD/MM/YYYY.

Googled this but couldn't find an answer, then I just replaced "=NOW()" [yields: 01/01/2011 01:00:00] with "=Today()" [yields: 01/01/2011] and this did the trick.

English Ordinal Suffix in T-SQL for MDX

Working in the UK my bosses like the English Ordinal Suffix. I haven't seen this anywhere in Google, I thought I could use SWITCH to do this in an MDX query but I spent over an hour before I gave up trying to get SWITCH to work. Instead I cheated and got the ordinal in the Transact-SQL query:
copyraw
CASE DATEPART(DAY, [MyDateValue]) 
   WHEN 1 THEN 'st'
   WHEN 21 THEN 'st'
   WHEN 31 THEN 'st'
   WHEN 2 THEN 'nd'
   WHEN 22 THEN 'nd'
   WHEN 3 THEN 'rd'
   WHEN 23 THEN 'rd'
   ELSE 'th'
   END AS Ordinal,
  1.  CASE DATEPART(DAY, [MyDateValue]) 
  2.     WHEN 1 THEN 'st' 
  3.     WHEN 21 THEN 'st' 
  4.     WHEN 31 THEN 'st' 
  5.     WHEN 2 THEN 'nd' 
  6.     WHEN 22 THEN 'nd' 
  7.     WHEN 3 THEN 'rd' 
  8.     WHEN 23 THEN 'rd' 
  9.     ELSE 'th' 
  10.     END AS Ordinal, 
All very posh.

Even Microsoft don't have helpful documentation on using SWITCH in an MDX query. Chances are they can't get it working either:
copyraw
Switch(
	Format(Fields!Date.Value,"d")=1,"st",
	Format(Fields!Date.Value,"d")=21,"st",
	Format(Fields!Date.Value,"d")=31,"st", 
	Format(Fields!Date.Value,"d")=2,"nd",
	Format(Fields!Date.Value,"d")=22,"nd",
	Format(Fields!Date.Value,"d")=3,"rd",
	Format(Fields!Date.Value,"d")=23,"rd")
  1.  Switch( 
  2.      Format(Fields!Date.Value,"d")=1,"st", 
  3.      Format(Fields!Date.Value,"d")=21,"st", 
  4.      Format(Fields!Date.Value,"d")=31,"st", 
  5.      Format(Fields!Date.Value,"d")=2,"nd", 
  6.      Format(Fields!Date.Value,"d")=22,"nd", 
  7.      Format(Fields!Date.Value,"d")=3,"rd", 
  8.      Format(Fields!Date.Value,"d")=23,"rd") 
Can anyone say what I did wrong with this? It returns "#Error".
Category: Transact-SQL :: Article: 317

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.