Solved

help needed with Microsoft SQL Server syntax

Posted on 2011-03-14
6
258 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 40

Expert Comment

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

Author Comment

by:Jimmy_inc
ID: 35154057
SQL 2000

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 40

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now