Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Compare Dates Fields
Hello,

I have a table that has 3 date fields.  I need to compare the 3 against each
other and get the max date.  What would be the best way to do this,
function, case statement in a stored procedure?  If there is any code
available, I would greatly appreciate it.
--
Thanks in advance,

sck10

Example of Table
--------------------
Date1: '10/1/2006'
Date2: '11/20/2006'
Date3: '5/1/2005'

Value = '11/20/2006'



Report this thread to moderator Post Follow-up to this message
Old Post
sck10
03-01-06 01:23 AM


Re: Compare Dates Fields
select  MAX(date_column_name
) from your_table_name



"sck10" <sck10@online.nospam> wrote in message
news:%23Ta80EIPGHA.2176@TK2MSFTNGP10.phx.gbl...
> Hello,
>
> I have a table that has 3 date fields.  I need to compare the 3 against
> each
> other and get the max date.  What would be the best way to do this,
> function, case statement in a stored procedure?  If there is any code
> available, I would greatly appreciate it.
> --
> Thanks in advance,
>
> sck10
>
> Example of Table
> --------------------
> Date1: '10/1/2006'
> Date2: '11/20/2006'
> Date3: '5/1/2005'
>
> Value = '11/20/2006'
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
ME
03-01-06 01:23 AM


Re: Compare Dates Fields
Thanks, but I'm trying to figure out the latest date of 3 datefields across
the row.  So the table structure would be like the following:
tblDateTracking
----------------
ScanID
UserName
SystemDate
ScanDate
IntlDate
--
Thanks in advance,

sck10



"ME" <ME@mail.com> wrote in message
news:OcjoZ7IPGHA.3576@TK2MSFTNGP15.phx.gbl...
> select  MAX(date_column_name
) from your_table_name
>
>
>
> "sck10" <sck10@online.nospam> wrote in message
> news:%23Ta80EIPGHA.2176@TK2MSFTNGP10.phx.gbl... 
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
sck10
03-01-06 01:23 AM


Re: Compare Dates Fields
On Tue, 28 Feb 2006 10:08:23 -0600, sck10 wrote:

>Hello,
>
>I have a table that has 3 date fields.  I need to compare the 3 against eac
h
>other and get the max date.  What would be the best way to do this,
>function, case statement in a stored procedure?  If there is any code
>available, I would greatly appreciate it.

Hi sck10,

The fact that you need to do this suggests that your dedsign may not be
optimal. You might want to consider using a design where these three
date values in three columns in one row are transformed into the same
three date values in one column in three rows. If you do, you can use
the code posted by ME to find the maximum date.

With the current design, you're stuck with using something ugly and
unwieldy such as

SELECT ID,
CASE WHEN Date1 > Date2 AND Date1 > Date3 THEN Date1
WHEN Date2 > Date3 THEN Date2
ELSE Date3
END
FROM   YourTable

or using a kludge such as

SELECT   ID, MAX(TheDate)
FROM    (SELECT ID, Date1 AS TheDate
UNION ALL
SELECT ID, Date2 AS TheDate
UNION ALL
SELECT ID, Date3 AS TheDate) AS Der
GROUP BY ID

--
Hugo Kornelis, SQL Server MVP

Report this thread to moderator Post Follow-up to this message
Old Post
Hugo Kornelis
03-01-06 01:23 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

MS SQL Server archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 08:06 AM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006