Solved

Select Distinct Year from Date

Posted on 2014-01-13
2
505 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

792 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