Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:00:02+00:00 2026-06-03T08:00:02+00:00

I have a date field that I populate a dropdownlist with. The date field

  • 0

I have a date field that I populate a dropdownlist with. The date field is like this 2012-05-01 00:00:00 and I need the dropdown list to look like this 5/2012. But to put a wrench in things, I need to pull the date apart before I put it back together again.

Specifically, I need to find all “distinct” months and all “distinct” years, but I only want to find the distinct year, if there is a datefield with a month for that year and vice-versa.

So I have the following data

Table: budget

id   Item   ItemDate
1     A     5/12/2012
2     B     5/14/2012
3     C     4/5/2012
4     D     4/7/2012
5     E     7/15/2011

So I want my dropdown list to show the following only:

5/2012
4/2012
7/2011

I have tried using

SELECT DISTINCT datepart(Month, ItemDate) + ' ' + datepart(year, ItemDate) As ddlList 
FROM Budget

but that adds the month to the year (5 + 2012 = 2017).

and then my asp

<asp:dropdownlist id="ddl1" runat="server" datasourceID="sqldatasrc" DataTextField="ddlList"></dropdownlist>

Any ideas? I’m not sure I’m going about this the right way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:00:04+00:00Added an answer on June 3, 2026 at 8:00 am

    ASP.NET

    If I remember correctly, I believe you can use the property DataTextFormatString available on the DropDownList control to format the boundfield values. You have to set the property to the value {0:M/yyyy}. Please make sure that the property is available. I couldn’t find it in MSDN.

    I could find only ListControl.DataTextFormatString Property

    SQL

    You have to cast the MONTH and YEAR parts of the datetime field values to VARCHAR or string data type before concatenating those values.

    Demo

    Click here to view the demo in SQL Fiddle.

    Script:

    SELECT  
    DISTINCT    CAST(DATEPART(MONTH, ItemDate) AS VARCHAR) + '/' + 
                CAST(DATEPART(YEAR, ItemDate) AS VARCHAR) AS ddlList 
    FROM        dbo.Budget
    

    Alternative way to write the same script:

    SELECT  
    DISTINCT    CAST(MONTH(ItemDate) AS VARCHAR) + '/' + 
                CAST(YEAR(ItemDate) AS VARCHAR) AS ddlList 
    FROM        dbo.Budget;
    

    Create and insert script:

    CREATE TABLE dbo.Budget
    (
         Id         INT         NOT NULL 
      ,  Item       VARCHAR(10) NOT NULL
      ,  ItemDate   DATETIME    NOT NULL
    )
    
    INSERT INTO dbo.Budget (Id, Item, ItemDate) VALUES
     (1, 'A', '05/12/2012'),
     (2, 'B', '05/14/2012'),
     (3, 'C', '04/05/2012'),
     (4, 'D', '04/07/2012'),
     (5, 'E', '07/15/2012');
    

    Output:

    DDLLIST
    -------
    4/2012
    5/2012
    7/2012
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Specifically, I have a model that has a field like this pub_date = models.DateField(date
I have an array that I would like to sort using a date field
I have a datetime field like that. <field name=rel_date type=date indexed=true stored=true default=NOW multiValued=false/>
I have a text field that is a date, although sometimes it also has
I have a record set that includes a date field, and want to determine
I have a bunch of objects that have a value and a date field:
I have an UPDATE sql command that modifies a Date/Time field in a particular
I have table that contain date and time field. id|date|time ========= 1|01/01/2001|10:45 2|01/02/2002|11:45 3|01/03/2003|12:45
I have a dropdown that holds an account number and two date fields. I
I have a symfony form which have created_at and updated_at field for date. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.