Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select max date by joining two columns

Author  Topic 

fralo
Posting Yak Master

161 Posts

Posted - 2010-12-08 : 08:25:59
I have two fields in the database.

DATE - datetime
TIME - varchar(5) sample data: 23:24

I need a query to get the most recent record inserted into the table. I have to combine these two fields together.

I've been playing with something like this but I think I need some convert functions.

SELECT MAX(DATE + ' ' + TIME)
FROM TABLE

Appreciate your help.

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-12-08 : 08:30:14
try
select max(date + convert(datetime,time))


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2010-12-08 : 08:31:12
I get 'Conversion failed when converting datetime from character string.'
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-12-08 : 08:42:34
check your time data - is it all nn:nn?

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

fralo
Posting Yak Master

161 Posts

Posted - 2010-12-08 : 08:45:04
Well I just scanned the data.

Some are like this:

nn:nn:

but most are like nn:nn
Go to Top of Page
   

- Advertisement -