I have a database table containing a row of dates in DateTime format. what I need to do is get all the distinct weeks numbers of the available dates. for example if I have the following dates:
03-JAN-13
04-JAN-13
09-JAN-13
the sql query would give me the following weeks numbers: 1 and 2.
PS: afterwards I will put these values in a dropdownlist (no problem with that step).
So can anybody tell me how to do it?
You did not specify what RDBMS you are using but you could use the following to get week numbers.
SQL Server you would use
DatePart():See SQL Fiddle with Demo
In MySQL you could use
Week():See SQL Fiddle with Demo
In Oracle, you could use
to_char():See SQL Fiddle with Demo
In PostgreSQL you can use the following:
See SQL Fiddle with Demo
Replace the
yourtablewith your table name anddateswith your date column.Edit #1: If you are using MS Access then you can still use
DatePart()(this was tested in MS Access 2003):