Solved

help needed with Microsoft SQL Server syntax

Posted on 2011-03-14
6
262 Views
Last Modified: 2012-06-21
loop through all tables in database and finding date, datetime fields, returning : tablename, date1, date2 - the order of the dates does not matter e.g:

table1 deliverydate
table2 deliverydate, orderdate, invoicedate
table3 invoicedate, cutoffdate
0
Comment
Question by:Jimmy_inc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
JoeNuvo earned 470 total points
ID: 35135482
SELECT DISTINCT A.TABLE_NAME, LEFT(el.ColList, LEN(el.ColList) - 1) ColList
FROM Information_Schema.COLUMNS A
CROSS APPLY (
	SELECT COLUMN_NAME + ', ' AS [text()]
	FROM Information_Schema.COLUMNS B
	WHERE TABLE_NAME = A.TABLE_NAME
		AND DATA_TYPE IN ('datetime','smalldatetime')
	FOR XML PATH('')
	) el (ColList)
WHERE DATA_TYPE IN ('datetime','smalldatetime')

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 35135563
What is your SQL version?
0
 

Author Comment

by:Jimmy_inc
ID: 35154057
SQL 2000

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:JoeNuvo
ID: 35154244
then how about this one?

DECLARE @Temp TABLE (rid int IDENTITY (1,1) PRIMARY KEY, TABLE_NAME sysname, COLUMN_NAME sysname, LISTS varchar(max))
DECLARE @tb sysname, @lists varchar(max)

INSERT INTO @temp (TABLE_NAME, COLUMN_NAME, LISTS)
	SELECT TABLE_NAME, COLUMN_NAME, ''
	FROM Information_Schema.COLUMNS 
	WHERE DATA_TYPE IN ('datetime','smalldatetime')
	ORDER BY TABLE_NAME, ORDINAL_POSITION

UPDATE @Temp
SET @lists = CASE @tb WHEN TABLE_NAME THEN @lists + ', ' + COLUMN_NAME ELSE ', ' + COLUMN_NAME END,
	LISTS = @lists,
	@tb = TABLE_NAME

SELECT TABLE_NAME, MAX(SUBSTRING(LISTS,3,LEN(LISTS))) ColLists
FROM @Temp
GROUP BY TABLE_NAME

Open in new window

0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 30 total points
ID: 35154340
JoeNuvo -  very nice. The only change I would suggest is - instead of varchar(max), it should be something like varchar(4000). As the asker is working on sql 2000, varchar(max) won't be recognized.
0
 
LVL 11

Expert Comment

by:JoeNuvo
ID: 35154395
Sharath_123 : thank you for your comment. I forgot about that. (since I code in on version 2005, there is no error raise)

:)
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Recursion schedule 13 47
SQL - Simple Pivot query 8 37
Merge join vs exist 3 37
Any benefit to adding a Clustered index here? 4 37
In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 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