sql server - SQL Statement to Get The Minimum DateTime from 2 String FIelds -


i've got bit of messy table on hands has 2 fields, date field , time field both strings. need minimum date fields, or record if there no date/time attached it. here's sample data:

id     first     last     date         time 1      joe       smith    2013-09-06   04:00 1      joe       smith    2013-09-06   02:00 2      jack      jones             3      john      jack     2013-09-05   06:00 3      john      jack     2013-09-15   15:00 

what want query following:

id     first     last     date         time 1      joe       smith    2013-09-06   02:00 2      jack      jones             3      john      jack     2013-09-05   06:00 

the min date/time id 1 , 3 , id 2 because doesn't have date/time. cam following query gives me id's 1 , 3 want them:

select * test t  cast(t.date + ' ' + t.time datetime ) = (select top 1 cast(p.date + ' ' + p.time datetime ) dtime test p t.id = p.id order dtime) 

but doesn't return row number 2 @ all. imagine there's better way go doing this. ideas?

you can row_number():

select id, first, last, date, time (select t.*,              row_number() on (partition id order date, time) seqnum       test t      ) t seqnum = 1; 

although storing dates , times strings not recommended, @ least right. values use iso standard format (or close enough) alphabetic sorting same date/time sorting.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -