简体   繁体   中英

sql query: select all users where timeMade was more than hoursNeeded

well I have a table

create table usersnew
(
user1 varchar (50) primary key,
names varchar(50),
lastname varchar(50),
hoursneeded float)

create table hoursusers
(
user1 varchar(50),
timeMade varchar(50),
foreign key (user1) references usersnew(user1)
)

hoursneede is how time the user need to make for finish his service.. "social service" it can be 400 hours.. now timeMade is how time he has made.. it is varchar because i save something as this "30:00:00" it is 30 hours '23:30:00' it is 23 hours and 30 minutes so.. i convert it in C# with timespan and another things.. now I need to make a query where I select to all users where timeMade was more than Hoursneede... if he need to make 400 and he has made '400:00:00' or plus I am going to select him.. but not all user need to make the same time.. another users need to make 450 or 300 I don't know... but I want to select to users than has finished with his time needed.. how can I do it? I can use sql for this query and c # with loops-for, for do it... some idea?

someidea?

Store the data with a type that represents the type of data that needs to be stored. Then all of a sudden your problem is solved!

  • Using the TIME data type in SQL Server 2008 ; failing that:
  • You can try storing the data as a FLOAT (which you do in the other table); although it gets ugly if you start needing per-second granularity:
  • If you need per-second granularity try storing the number of seconds in an INT column.

Summary: you want to store the number of hours in the database, so do that. Don't store a string just because your application passes it around frequently. You should deal with that in your application's DAL.

Use minutes instead, with numeric data type

you can easily convert minutes to hours by dividing to 60

and numeric data is easier to query

You should not store like varchar for better output. Lets store them in some numeric or date format with your logic. For ex. you can use datetime field to compare it. Also you can write some stored procedures for this, but never recomend to use loops or conditions from .NET code to retrieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM