Solved

Select Distinct Year from Date

Posted on 2014-01-13
2
499 Views
Last Modified: 2014-01-13
I would like to do a select distinct in a dropdown menu from a date field, but return only the Year, not the full date. The attached code returns all dates once, but I'd like returned only the year.
Thanks for your help.
<%
sql="SELECT distinct(OccDate) FROM Compliments"
Set rsYears = conn.Execute(sql)
%>

<select name="myYear">
<option></option>
<%
Do While Not rsYears.EOF
Response.Write "<option>"& rsYears("OccDate") &"</option>"
rsYears.MoveNext
Loop
Response.Write "</select>"
%>

Open in new window

0
Comment
Question by:romsom
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39778137
You need to extract Year from the date. Use this SQL syntax:
<%
sql="SELECT distinct DATEPART(year, OccDate) AS OccYear FROM Compliments"
Set rsYears = conn.Execute(sql)
%>

<select name="myYear">
<option></option>
<%
Do While Not rsYears.EOF
Response.Write "<option>"& rsYears("OccYear") &"</option>"
rsYears.MoveNext
Loop
Response.Write "</select>"
%>

Open in new window

0
 

Author Closing Comment

by:romsom
ID: 39778176
This is perfect, thank you very much!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 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