Solved

help needed with Microsoft SQL Server syntax

Posted on 2011-03-14
6
261 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
  • 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Syntax Grouping Sum question 7 35
SSRS: Why is Visual Studio stripping these properties? 2 39
MS SQL Conditional WHERE clause 3 36
SQL Query help 3 24
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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