tags:

views:

158

answers:

1

I want to display max time and min time for a day in grid control using visual basic from sql database. table column are:- UserID,UserName,Date,Time 1 Shanks 30/1/2009 10:11:22 1 Shanks 30/1/2009 10:15:22 1 Shanks 30/1/2009 12:15:22 1 Shanks 30/1/2009 13:15:22

output must be in grid 1 Shanks 30/1/2009 10:11:22 13:15:22

A: 

I'm going to assume your table structure is something like this

CREATE TABLE mytable (UserID integer, UserName varchar(20), [Date] datetime, [Time] varchar(8))

your time is stored as a varchar field because there is no time type in sql2005. This will display the min and max time per each user and date. There are two options, the first if your times are HH:MM:SS then you can just use the convert function. The second shows you an example of parsing it and building the date yourself.

SELECT
   UserID,
   UserName,
   [Date],
   CONVERT(varchar, MIN(CONVERT(datetime, [Time], 108)), 108),
   CONVERT(varchar, MAX(CONVERT(datetime, [Time], 108)), 108)
FROM mytable
GROUP BY UserID, UserName, [Date]
ORDER BY UserID, [Date]


SELECT
   UserID,
   UserName,
   [Date],
   CONVERT(varchar, MIN(DATEADD(second, CAST(SUBSTRING(Time, 7, 2) AS integer), DATEADD(minute, CAST(SUBSTRING(Time, 4, 2) AS integer), DATEADD(hour, CAST(SUBSTRING(Time, 1, 2) AS integer), 0)))), 108),
   CONVERT(varchar, MAX(DATEADD(second, CAST(SUBSTRING(Time, 7, 2) AS integer), DATEADD(minute, CAST(SUBSTRING(Time, 4, 2) AS integer), DATEADD(hour, CAST(SUBSTRING(Time, 1, 2) AS integer), 0)))), 108)
FROM mytable
GROUP BY UserID, UserName, [Date]
ORDER BY UserID, [Date]
Will Rickards
Thanks Will Rickards