May 31, 2007 at 7:08 pm
I want to query my database for all records that have a time of 11:59:59 PM from the DateTime column. How do I accomplish this?
sql2day
May 31, 2007 at 10:28 pm
With a fair amount of dynamic SQL...
Do you mean you want to do an "auto discovery" of each table having any DateTime column (even if more than 1 per table) for all tables in the database? Why do you want to do this?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 31, 2007 at 11:42 pm
Just check if the given code below does work for you.
select substring(convert(varchar(30),getdate(),109),13,len(convert(varchar(30),getdate(),109)))
Cheers,
Sugeshkumar Rajendran
SQL Server MVP
http://sugeshkr.blogspot.com
June 1, 2007 at 7:41 am
sql2day
Here's a stub query
SELECT * FROM TableName t WHERE DATEPART (hh, t.DateTimeColumn) = '23' AND DATEPART (mm, t.DateTimeColumn) = '59' AND DATEPART (ss, t.DateTimeColumn) = '59'
but as Jeff pointed out, if you want to scan the DateTime column (if one exists) for each table in the db, then there's quite a lot more to add to this.
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 1, 2007 at 7:53 am
From your post I get the idea that you want to query the whole database (i.e. each table in the database). If this is the case, you can use the query posted above with the sp_msforeachtable stored procedure. It takes a parameter @command and you can reference the database within the command using the ? symbol.
This is also assuming that each table in the database will have a column with the same name (i.e. dateTimeColumn).
sp_msforeachtable 'SELECT * FROM ? t WHERE DATEPART (hh, t.DateTimeColumn) = '23' AND DATEPART (mm, t.DateTimeColumn) = '59' AND DATEPART (ss, t.DateTimeColumn) = '59'
June 1, 2007 at 5:27 pm
Nicely done... still going to take a fair bit more than that if table has more than 1 DateTime column, has no DateTime column, or, as you said, has different names for DateTime columns.
But I want to find out what the OP has in mind before I dedicate any more time to this...
--Jeff Moden
Change is inevitable... Change for the better is not.
June 5, 2007 at 11:28 pm
Don't know if this is still relevant for you, but output of following query will give you the required SQL statements:
SELECT 'SELECT * FROM [' + sysU.name + '].[' + sysO.name + '] WHERE CONVERT(varchar(8), [' + sysC.name + '], 108) = ''11:59:59'''
FROM
dbo.syscolumns sysC INNER JOIN
dbo.sysobjects sysO ON sysC.id = sysO.id AND sysO.xtype = 'U' INNER JOIN
dbo.sysusers sysU ON sysO.uid = sysU.uid
WHERE sysC.xtype IN (58, 61)
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply