Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

help needed with Microsoft SQL Server syntax

Posted on 2011-03-14
6
Medium Priority
?
269 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 1880 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 120 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

650 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