Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Syntax for putting a varible in a date in SQL

Posted on 2015-01-18
7
Medium Priority
?
196 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 1000 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 51

Accepted Solution

by:
Gustav Brock earned 1000 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 51

Expert Comment

by:Gustav Brock
ID: 40557129
You are welcome!

/gustav
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

721 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