Solved DatePart not working in select query like (1 Viewer)

why

Member
Local time
Today, 15:19
Joined
Sep 28, 2020
Messages
40
Any idea why this won't work?
Code:
SELECT Max(Val(Right([ClosedNumbers]![ClosedNumbers],4))) AS Expr2
FROM Closednumbers
WHERE (((Left([ClosedNumbers]![ClosedNumbers],2)) Like "Str(Right(DatePart("yyyy",Now()),2))"));

It says the expression you entered contains invalid syntax. You may have entered an operand without an operator. It then focuses on yyyy

If I run it like this it works
Code:
Str(Right(DatePart("yyyy",Now()),2))
and this also works
Code:
SELECT Max(Val(Right([ClosedNumbers]![ClosedNumbers],4))) AS Expr2
FROM Closednumbers
WHERE (((Left([ClosedNumbers]![ClosedNumbers],2)) Like "20"));

Any help would be great my brain hurts.
 

Isaac

Lifelong Learner
Local time
Today, 13:19
Joined
Mar 14, 2017
Messages
8,777
Is it something more like this? I haven't done much of wildcards combined with expressions inside the wc, but:

WHERE Left([ClosedNumbers]![ClosedNumbers],2) Like '*Str(Right(DatePart("yyyy",Now()),2))*'
 
  • Like
Reactions: why

theDBguy

I’m here to help
Staff member
Local time
Today, 13:19
Joined
Oct 29, 2018
Messages
21,454
Try:
SQL:
...=Str(Right(DatePart("yyyy",Now()),2))));
Don't need to use Like if you're not going to use wildcards.
 

why

Member
Local time
Today, 15:19
Joined
Sep 28, 2020
Messages
40
Is it something more like this? I haven't done much of wildcards combined with expressions inside the wc, but:

WHERE Left([ClosedNumbers]![ClosedNumbers],2) Like '*Str(Right(DatePart("yyyy",Now()),2))*'
This runs but it returns no values.
Is it something more like this? I haven't done much of wildcards combined with expressions inside the wc, but:

WHERE Left([ClosedNumbers]![ClosedNumbers],2) Like '*Str(Right(DatePart("yyyy",Now()),2))*'
On this one, it runs but I get no values returned.
 

Isaac

Lifelong Learner
Local time
Today, 13:19
Joined
Mar 14, 2017
Messages
8,777
Hmm. Not sure to what extent you can combine expressions inside Like '*'

Another option is create a vba function that returns a literal string.

Edit -i see dbGuy's post, best one :)
 

why

Member
Local time
Today, 15:19
Joined
Sep 28, 2020
Messages
40
Try:
SQL:
...=Str(Right(DatePart("yyyy",Now()),2))));
Don't need to use Like if you're not going to use wildcards.
This runs but it also returns 0 values.
Code:
SELECT Max(Val(Right([ClosedNumbers]![ClosedNumbers],4))) AS Expr2
FROM Closednumbers
WHERE (Left([ClosedNumbers]![ClosedNumbers],2)) =Str(Right(DatePart("yyyy",Now()),2));
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:19
Joined
Oct 29, 2018
Messages
21,454
This runs but it also returns 0 values.
Code:
SELECT Max(Val(Right([ClosedNumbers]![ClosedNumbers],4))) AS Expr2
FROM Closednumbers
WHERE (Left([ClosedNumbers]![ClosedNumbers],2)) =Str(Right(DatePart("yyyy",Now()),2));
One more try then. What do you get with this?
SQL:
...=Format(Date(),"yy")
Or this one?
SQL:
...="'" & Format(Date(),"yy") & "'"
 
  • Like
Reactions: why

why

Member
Local time
Today, 15:19
Joined
Sep 28, 2020
Messages
40
One more try then. What do you get with this?
SQL:
...=Format(Date(),"yy")
Or this one?
SQL:
...="'" & Format(Date(),"yy") & "'"
One more try then. What do you get with this?
SQL:
...=Format(Date(),"yy")
Or this one?
SQL:
...="'" & Format(Date(),"yy") & "'"
Thanks the first one worked here is the code

SQL:
SELECT Max(Val(Right([ClosedNumbers]![ClosedNumbers],4))) AS Expr2
FROM Closednumbers
WHERE (Left([ClosedNumbers]![ClosedNumbers],2))=Format(Date(),"yy");
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:19
Joined
Oct 29, 2018
Messages
21,454
Thanks the first one worked here is the code

SQL:
SELECT Max(Val(Right([ClosedNumbers]![ClosedNumbers],4))) AS Expr2
FROM Closednumbers
WHERE (Left([ClosedNumbers]![ClosedNumbers],2))=Format(Date(),"yy");
Okay, glad to hear you got it to work. Good luck with your project.
 
  • Like
Reactions: why

Users who are viewing this thread

Top Bottom