Use of MonthName (1 Viewer)

Rmaster2022

Member
Local time
Today, 04:27
Joined
Apr 1, 2022
Messages
32
I've created a birthday report that groups birthdays by month, then sorts by day. I have a heading for each month written: =MonthName(DatePart("m",[DOB]))
DOB is date of birth. However I have a few null values for DOB, so, of course, the null records are grouped under the heading: #type!
Is there a way to write an expression so the heading is "Unknown" for a group of null values in the DOB field.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:27
Joined
Oct 29, 2018
Messages
21,473
You could try:
Code:
=IIf(IsNull([DOB]),"Unknown",MonthName(DatePart("m",[DOB])))
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 19:27
Joined
Jan 20, 2009
Messages
12,852
You could try:
Code:
=IIf(IsNull([DOB]),"Unknown",MonthName(DatePart("m",[DOB])))
IsNull is a VBA function. Better to use a database engine expression Is Null.
Code:
=IIf([DOB] Is Null,"Unknown",MonthName(DatePart("m",[DOB])))

BTW An alternative to MonthName(DatePart("m", DOB)) is
Code:
Format(DOB, "mmmm")

"mmmm" returns the full name
"mmm" returns the three letter abbreviation.
 

ebs17

Well-known member
Local time
Today, 11:27
Joined
Feb 7, 2020
Messages
1,946
I think a header should always have meaningful content. Since Monthname(null) generates an error, you could make sure up front that valid numbers from 1 to 12 are always passed.

To do this, you can use an auxiliary table to get the missing values into the query, like ...
SQL:
SELECT
   Monthname(N.Number) AS Header,
   D.*
FROM
   NumberTable AS N
      LEFT JOIN Data AS D
      ON N.Number = Month(D.DOB)
 

Rmaster2022

Member
Local time
Today, 04:27
Joined
Apr 1, 2022
Messages
32
Thank you all for your answers and help. They all worked, now is choosing a preference. I guess there is more than one way to skin a cat! (Apologies to our cat Gracie!)
 

Users who are viewing this thread

Top Bottom