Month Year extract (1 Viewer)

Gismo

Registered User.
Local time
Today, 12:02
Joined
Jun 12, 2017
Messages
1,298
Hi All,

Please could you assist

Month year extract from date not working

Date format YYYY/MM/DD
Result is all Jan 2022

Period: Format([Transaction Date],"mmm\ yyyy")
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:02
Joined
Feb 28, 2001
Messages
27,186
What is the data type of [Transaction Date]? The "Date Format" you quoted simply defines how the information would be presented in a table or query and is in fact immaterial to the operation of the Format function - unless [Transaction Date] is a string.
 

Gismo

Registered User.
Local time
Today, 12:02
Joined
Jun 12, 2017
Messages
1,298
What is the data type of [Transaction Date]? The "Date Format" you quoted simply defines how the information would be presented in a table or query and is in fact immaterial to the operation of the Format function - unless [Transaction Date] is a string.
Transaction date is set up as a short date yyyy/mm/dd
 

ebs17

Well-known member
Local time
Today, 11:02
Joined
Feb 7, 2020
Messages
1,946
Filter to January 2022 (without time shares):
SQL:
SELECT
   *
FROM
   TableX
WHERE
   [Transaction date] BETWEEN #1/1/2022# AND #1/31/2022#
This allows the use of a possibly existing index on the date.
 

Gismo

Registered User.
Local time
Today, 12:02
Joined
Jun 12, 2017
Messages
1,298
Filter to January 2022 (without time shares):
SQL:
SELECT
   *
FROM
   TableX
WHERE
   [Transaction date] BETWEEN #1/1/2022# AND #1/31/2022#
This allows the use of a possibly existing index on the date.
Hi,

I dont want to have any limitations in hardcoding any date ranges
 

Gismo

Registered User.
Local time
Today, 12:02
Joined
Jun 12, 2017
Messages
1,298
unless the field is Null (blank).
Al the transactions in the transaction table has a date

Just not sure why in this DB this is not working, I have it set up exactly as in another DB and it works perfect
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:02
Joined
May 7, 2009
Messages
19,243
how about the Fieldname, is it correct?
 

ebs17

Well-known member
Local time
Today, 11:02
Joined
Feb 7, 2020
Messages
1,946
I dont want to have any limitations in hardcoding any date ranges
Neither do I. But first let's look at how the query should look like so that it is functional and performant.

In the second step, you use parameters and make the whole thing variable.
SQL:
PARAMETERS
   pStartDate Date,
   pEndDate Date
;
SELECT
   *
FROM
   TableX
WHERE
   [Transaction date] BETWEEN pStartDate AND pEndDate
A month is a period. If necessary, the time limits could also be set in such a way that 10 days, four weeks or a fiscal year (spanning the turn of the year) can be queried. That too is variability.
 

Gismo

Registered User.
Local time
Today, 12:02
Joined
Jun 12, 2017
Messages
1,298
Sorry all, sorted now, the format pulled through incorrectly when imported from excel
 

Gismo

Registered User.
Local time
Today, 12:02
Joined
Jun 12, 2017
Messages
1,298
I do have another question
How do I sort by month?

The Month year is not in sequence
This is a sum query so I can not include the actual date to sort

1667899436044.png
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:02
Joined
May 7, 2009
Messages
19,243
you sort it on the Base date field or create another Calculated Column:

Expr1: Format([base field], "yyyymm")

then sort on this column. you do not need to "show" this column
 

ebs17

Well-known member
Local time
Today, 11:02
Joined
Feb 7, 2020
Messages
1,946
Do you know the difference between strings and numbers? Strings are sorted alphanumerically.
SQL:
SELECT
   Format(XDate, "mmm yyyy") AS Period
FROM
   TableX
ORDER BY
   Year(XDate),
   Month(XDate)
 

Gismo

Registered User.
Local time
Today, 12:02
Joined
Jun 12, 2017
Messages
1,298
you sort it on the Base date field or create another Calculated Column:

Expr1: Format([base field], "yyyymm")

then sort on this column. you do not need to "show" this column
I have the 1st query sorted by the actual date
Query 2 I did not include the date as I have a sum in this query but the Sort order for month year is not in the same sequence as in the 1st query

Not sure how to add a column to sort the second query by
 

Gismo

Registered User.
Local time
Today, 12:02
Joined
Jun 12, 2017
Messages
1,298
Do you know the difference between strings and numbers? Strings are sorted alphanumerically.
SQL:
SELECT
   Format(XDate, "mmm yyyy") AS Period
FROM
   TableX
ORDER BY
   Year(XDate),
   Month(XDate)
A bit in the dark here, sorry

This is what I did but can not sort by period as this will give a incorrect sequence by month
 

ebs17

Well-known member
Local time
Today, 11:02
Joined
Feb 7, 2020
Messages
1,946
XDate should be a date, i.e. a field with data type Date. Internally, a date is a number:
Code:
? #11/8/2022# * 1   
44873

Therefore, a real date can be easily sorted, even though it looks like text because it is represented as a string with separators between the digits. However, the format statement produces text. This then makes sorting alphanumeric:
Code:
? "Aug" < "Jan"
True
 

Gismo

Registered User.
Local time
Today, 12:02
Joined
Jun 12, 2017
Messages
1,298
XDate should be a date, i.e. a field with data type Date. Internally, a date is a number:
Code:
? #11/8/2022# * 1 
44873

Therefore, a real date can be easily sorted, even though it looks like text because it is represented as a string with separators between the digits. However, the format statement produces text. This then makes sorting alphanumeric:
Code:
? "Aug" < "Jan"
True
I believe this is what I did but still not sorted by month correctly

SELECT Format([Transaction Date],"mmm yyyy") AS Period, Transactions.[Transaction Type Ind], Sum(Transactions.[Total (UGX)]) AS [Total (UGX)]
FROM Transactions
GROUP BY Format([Transaction Date],"mmm yyyy"), Transactions.[Transaction Type Ind]
HAVING (((Transactions.[Transaction Type Ind])=2))
ORDER BY Format([Transaction Date],"mmm yyyy");
 

Minty

AWF VIP
Local time
Today, 10:02
Joined
Jul 26, 2013
Messages
10,371
I think the last line in your query might give you a hint...

Code:
SELECT Format([Transaction Date],"mmm yyyy") AS Period, Transactions.[Transaction Type Ind], Sum(Transactions.[Total (UGX)]) AS [Total (UGX)]
FROM Transactions
GROUP BY Format([Transaction Date],"mmm yyyy"), Transactions.[Transaction Type Ind]
HAVING (((Transactions.[Transaction Type Ind])=2))
ORDER BY Format([Transaction Date],"mmm yyyy");

Try

ORDER BY Format([Transaction Date],"mm yyyy")

Which should give you a numeric month number with a leading zero?
 

Users who are viewing this thread

Top Bottom