| Author |
Topic |
|
Brittney10
Posting Yak Master
154 Posts |
Posted - 2011-01-04 : 13:17:32
|
I'm trying to loop through column names that have dates. Since there are about 30 columns that are dates, I'd like to just do a simple loop and not have to type in EVERY column name. Help would be much appreciated!For example: (Data for Day in ( [9/1/2009], [9/2/2009], 9/3/2009] [and so on]) |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-01-04 : 14:09:58
|
| Could you be more specific? Are the columns in the same table? Is [9/1//2009] an actual column name?JimP.S. This is a MS SQL Server forum, your code doesn't look like t-SQLEveryday I learn something that somebody else already knew |
 |
|
|
Brittney10
Posting Yak Master
154 Posts |
Posted - 2011-01-04 : 14:32:40
|
| Yes columns are in the same tableYes columns are named like 9/1/2009and yes this is T-SQL This is just a snippet of code. I'm actually pivoting data so i can report on it..thus the columns being days |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2011-01-04 : 15:04:17
|
| I am with Tkizer -- throw out this design. First of all, you aren't even using ISO-8601 date formats, so you are doing the wrong badly. Think about the data model. A date is a (temporal) value; it it not an attribute. This sounds like a job for a report writer.--CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
Brittney10
Posting Yak Master
154 Posts |
Posted - 2011-01-04 : 15:20:46
|
| I'm aware that the table structure is awful, but it's what i have to work with. I am pivoting the data in such as a way that the temp table structure becomes Data, Name, Day, which I can work with. I have to enter every column name (including the columns that are in date format), thus my need to loop through the column names quicly to save time from entering all the column names. |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-01-04 : 15:27:51
|
| Then use Tara's suggestion. Right-click the table in SSMS and choose script as. Or evenselect column_name from INFORMATION_SCHEMA.COLUMNSwhere table_name = 'yourTable'might save you some typing.JimEveryday I learn something that somebody else already knew |
 |
|
|
|