Sunday, February 19, 2012

Date Conversion and Expressions

I have a table with about 20,000 records that have a date field, stored as a datetime in the database like '8/28/2006 8:42:14 AM'. The dates range from March 2004 to current. What I would like to do is retrieve the dates in that format (month year) and put them in a dropdown. I have this so far:

SELECT DISTINCT DATEPART(month, dte_date) AS Expr1, DATEPART(yyyy, dte_date) AS Expr2
FROM myTable
ORDER BY DATEPART(yyyy, dte_date), DATEPART(month, dte_date)

And the query returns the information that I want, but I can only bind one field to the dropdown. I was thinking that if I return the results a single expression (concantenate?) then I could bind that to the dropdown. I'm not sure as how to go about this. Also, the month returned is numeric and I would rather have the name of the month returned (like "July" instead of "7").

Thanks in advance to anyone who helps me.

using the Northwind Orders table as an example,

selectdistinctdatename(month, requireddate)+' '+datename(year, requireddate)from orders

|||

I figured it out, just in time for the requirements to change. For those who may be curious, I did something like this:

SELECT DISTINCT DATEPART(month, dte_date) + ' ' + DATEPART(yyyy, dteDate) AS Expr1, DATENAME(Month, dte_Date) AS Expr2, DATENAME(Year, dte_Date) AS Expr3
FROM myTable
ORDER BY Expr3 DESC, Expr2 DESC.

Now I am trying to figure out how I can create a SQL query using separate dropdowns, one for month and one for year. Any suggestions?

|||

For sorting, you can add the original field, as the output one is text

selectdistinctdatename(month, requireddate)+' '+datename(year, requireddate)as mmdd, requireddatefrom ordersorderby requireddateasc

|||Not simple, one affects the other - pick a year, get months for that year. Pick a month, get years for that month, also then need a reset button to show all months or all years again. THen, do you go javascript, client call back, or postback?

No comments:

Post a Comment