Python 3, 5555 40 bytes
Any help with golfing it would be appreciated
lambda N:lensum([x forx%10==2for x in range(N+1)if str(x)[-1]=="2"])
Lots of thanks to the commentors for helping me out( I have problems with memory, see names below)