Solved

Syntax for putting a varible in a date in SQL

Posted on 2015-01-18
7
186 Views
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.

Thanks,
Scott
0
Comment
Question by:Scott Palmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40556655
try this

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

Author Comment

by:Scott Palmer
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.
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) 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)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 50

Accepted Solution

by:
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)
    And
    DateSerial(Year(DateAdd("m", -1, Date())), 12, 31)

/gustav
0
 

Author Comment

by:Scott Palmer
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?

Scott
0
 

Author Closing Comment

by:Scott Palmer
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.

Scott
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40557129
You are welcome!

/gustav
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

738 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