Syntax for putting a varible in a date in SQL

Posted on 2015-01-18
Last Modified: 2015-01-18
I need to know the syntax for putting a date in SQL in Access with a variable.

This is what i want:

dim vYear as Date
vYear = Year(Now)

WHERE FinalDate = 1/1/vYear

vYear is the of course the variable.

Question by:ScootterP
  • 3
  • 2
  • 2
LVL 119

Expert Comment

by:Rey Obrero
ID: 40556655
try this

WHERE FinalDate = dateserial(year(now()),1,1)

Author Comment

ID: 40556663
I am trying to find the data parameters for each year for year to date reporting so the whole WHERE column is this dummy code.

WHERE FinalDate Between 1/1/vYear and 12/31/vYear

What i currently have is this:

WHERE FinalDate Between 1/1/2014 and 12/31/2014

And i was updatting so I would not have to change my code every January with the new year

Also the variable vYear could be the current year or it could be the previous month's year when it is january.
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
ID: 40556673
then you need to use this

WHERE FinalDate = Between dateserial(year(now()),1,1)  and  dateserial(year(now()),12,31)
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

LVL 49

Accepted Solution

Gustav Brock earned 250 total points
ID: 40556717
>  it could be the previous month's year when it is january.

Then it would be:

WHERE FinalDate Between
    DateSerial(Year(DateAdd("m", -1, Date())),1,1)
    DateSerial(Year(DateAdd("m", -1, Date())), 12, 31)


Author Comment

ID: 40556741
Gustav is correct.  That is why I was hoping to just add variable vYear after 1/110 or 12/31/ that way I would not have to add the longer SQL statement all throughout out my, I could do it once when the form is opened.  So there is no way to do this?


Author Closing Comment

ID: 40556751
Thanks, I can use the code once when the form opens and store that in a variable for the from and to date.

LVL 49

Expert Comment

by:Gustav Brock
ID: 40557129
You are welcome!


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now