.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

SELECT Random Records in term of time

Posted By:      Posted Date: September 27, 2010    Points: 0   Category :ASP.Net


I want to return random records in term of time whithout NEWID

View Complete Post

More Related Resource Links

Select random 10 records in sql server 2005 in terms of time

Hello,i have a query that select random records in access database in terms of time : "SELECT TOP 10 * FROM Tbl ORDER BY Rnd(-10000000*TimeValue(Now())*[ID])"now i wish to convert this query for using in sql server 2005 such that return random records in term of time 

select records within 30 days of previous records

I have a table with a list of patient id's and there begin date and end dates as follows: ID     begin_date  end_date 1234 10/09/2009 10/16/2009 1234 12/16/2009 12/22/2009 1234 12/28/2009 12/30/2009 1234 01/13/2010 01/19/2010 1234 04/29/2010 05/05/2010 what i need to do is select the records that have an end_date within 30 days of a begin_date. So in the above example, i would need to get the following records: ID     begin_date   end_date 1234 12/28/2009   12/30/2009 1234 01/13/2010   01/19/2010 Because 12/28/2009 is 30 days within 12/22/2009 (previous end_date) and 01/13/2010 is within 30 days of 12/30/2009 (previous end_date) and 12/22/2009 (2nd previous end_date). is there a good way to do this using T-SQL?

Running time of select

hi i have the select query. how can i find the time of its running in sql code Best Regards. morteza

Updating bunch of records at time

Hi, If I want to update 1000 records at a time in the database what is the best approach.Guide me Pls.

Updating Multiple records at 1 time.


You got to love management, they can never make their minds up on what they want. 

I'm currently working on a asp.net project using a datagridview that acts for the most part like a spreadsheet.  What I have to do now is update all the values in the table from the selected date, hour, min, max and cost forward.  There are 7 days worth of data in the table, when the user alters either the min, max or cost I need all the values in that field from that point onward to have the new altered value.

Here's an example:

In hour 10 of the day the user want to increase the max from 5 to 19, the min stays the same and so does the cost.  Mind you any or all of these values could change.  The only once that will remain as-is is the Date and Hour field:


Date Hour

regarding random records


Hi all,

            I am developing a College exam system where  Teacher will upload questions, students will login and solve the answers.

            Here suppose, faculty(any) uploads 500 questions. When exam starts then students will login. Ofcourse not on the SAME TIME. There is a button start my exam. When he/she press the button exam will start for particular student. All  500 questions are uniqe for every students. None of question repeated to anyone. So, If Student 'B' logs in first then he'll get 1st question only and after that if student 'A' logs in then he will get 2nd question. Like same for every student.(First come first service based) When any question will finish he'll press next button  then again random question will genrate.

       Here, if question is delivered to any student then this question will not be displayed again to anyone.

       I become confuse how to do that. Just tell me the idea regarding this.



select records within a week


i try to use select statement to select records within a week from orderdate. like,


"select * from order where orderdate <= DATEADD(day,7,orderdate)";


correct me if wrong.


Select Statement - Random and Percentage


 I have a question about a select statement.  Currently I have a select statement that does what I want it to do, but it needs to be altered, giving my WHERE options an equal chance to show.


SELECT TOP 1 *, NEWID() as Random

It seems to work pretty well for the purpose.  Often I will need 3 to 6 different Code types.  What I need it to do is give each CODE type an equal oportunity to display.

For instance:


I'd like them to, in this instance, have a 33% to show.  More or less, I'd like, 1 of AB, ABC, ABCD, then I want them to randomize agian. 

If I have  4 Code types, I'd like a Select statement to grab 1 of each, and give them a 25% chance to show, by randomizing those 4.

Please Advise.


Select unique records, take the largest


Suppose, I have a table with the following data.   I need to be able to select the unique records from this table, on the LogicalHash column.  However, I need the select the largest by FileSize.   I can't quite figure out the correct SQL for this.   If I didn't have to worry about taking the largest file, I would just do this:

WHERE FileID in (
SELECT max(FileID) FROM files GROUP BY LogicalHash)


How do pick the largest file?  The file size is not going to be unique throughout the table, I can't select based on that.  Using cursors sounds like a bad idea.  I also need the SQL to be somewhat backwards compatible to MS Access.

Table: files

Insert on button event is inserting 2 records at a time


I have a form that is doing a simple insert, then a sub insert of a certain drop down selection is made. What i dont get is why the main insert is inserting duplicates and the sub works fine inserting 1 record per text as expected. I click the button 1 time, and when i check the table, there are 2 new records.. As you can see the insert is not wrapped with any conditions, once that is inserted i check if a slection was made and runs the sub insert.

My full code can be found in this post(http://forums.asp.net/t/1608619.aspx), but here is what i have for my button event.

    protected void btnSaveServConfig_Click(object sender, EventArgs e)   
        dac.insertServerConfigintosql(txtEmailLogin, txtEmailPass, Convert.ToInt32(ddlNumEmail.SelectedValue));   
            if (Convert.ToInt32(ddlNumEmail.SelectedValue) > 0)   
                 Dictionary<string, string> camValues = new Dictionary<string, string>();   
                    foreach (HtmlTableRow row in table1.Rows)   
                        foreach (HtmlTableCell cell in row.Cells)   
                            foreach (Control control in cell.Controls)   

Finding records using time datatype


I need to find rows in a table that have StartTime and EndTime that fall between sent start and end time parameters.  Below is the design of the table I want to search and below it is an example of a SELECT statement.  I need to add to the SELECT another condition (e.g. AND StartTime BETWEEN @StartTime AND @EndTime OR EndTime BETWEEN @StartTime AND @EndTime).  Any help is appreciated.

CREATE TABLE [dbo].[CareScheduleDetail](
 [CareScheduleDetailID] [int] IDENTITY(1,1) NOT NULL,
 [CareScheduleID] [int] NOT NULL,
 [WorkDay] [tinyint] NOT NULL,
 [StartTime] [time](7) NULL,
 [EndTime] [time](7) NULL,
 [SvcType] [varchar](4) NULL,
 [Effective] [date] NULL,
 [Ended] [date] NULL)


SELECT     WorkDay, StartTime, EndTime
FROM         dbo.CareScheduleDetail
WHERE     (Ended IS NULL)

select certian records linq to sql


I have a database with a GUID and images,

one function

I want to get the first record (if exists) and return results


 second function

i want to get the second record only if a second record exists


I guess I have to return the records frmo the table then just movenext so to speak in a loop to select the second record but guessing there is a better faster way to do that in linq

I see some example for .take(1)  I assume that will always take the first record and return nothing else. I dont need this to be random. and what happens if there are no records return?

for the second function

I gues its  .skip(1).take(1)   will that for sure skip the first record and return the  second one to my var? again what if thier is no second record i would not want any results return if this is not the case (no scond record then want nothing back and no error)



Select Query to get records within last 24 hours in Nhibernate


Hi All

How to Retrive records within last 24 hrs using Nhibernate.
I want to retrieve the 10 records within last 24 hours.

How can i achieve this using Nhibernate.

Please Help.

Strange time issue on simple select statement


Hi, I have a strange time out issue on a simple select statement.
This is my select statement:

Select * from DyGeJournal where ord>=4609635 and Fracsec>=31

The ord and Fracsec is the primary key.
When I ask for the actual excecution plan, he is using a an index seek.
It takes more than 1,5 minutes to get a result back.

When I shrink my database, everyting is running as expected, the query above runs in milliseconds.
After a while (2 weeks) it start agian running very slow.

I have the same logic on other tables, but I don't have the problem on those tables.
What can cause this problem?

select random if multiple rows are the same?



i have a select statement which returns a column value.

id like to display the lowest column value there is, but if there are multiple rows with the same (and lowest) value id like it to be random which one of the rows is selected.. how would i do this? my select statement is currently:

SELECT     TOP (1) url, ABS(r1 - @r1) + ABS(r2 - @r2)  AS difference
FROM         type
ORDER BY difference

this picks the one lowest result for me, but if there are multiple duplicate lowest results id like to select them, and show one at random..?

code SQL to retrieve records from multiple random selection list box


Having problems coding a query.

Got a list box that allows for multiple selections (holding down the control key) the selection quantity is basically unlimited, but should never be more than 50 to 100 items from the listing of about 3000 items.

How can I code the SQL statement to select the choosen record data? I think I have the selection list worked out by populating an array but I am not clear on how to build the SQL query.

Any help would be appreciated.


Thanks again

SELECT INSERT DELETE UPDATE happening at the same time


Hi All,
I have three table and SELECT DELETE INSERT UPDATE happening at the same time.
Join query is used for SELECT and DELETE.
INSERT and UPDATE no join query.
What are the actions to be taken to avoid deadlock. Will indexing help to resolve the dead lock.
Appreciate somebodys help immediately.



FileID FileName LogicalHash DateModified FileSize
1 test.htm 00683b8eb1249ab232f83ab76148127a 11/17/2009 15002
2 document1.doc 00ebb79d95e13e71a0e93e9b82c2edd2 1/5/2008 55623
3 webpage.xml 00de966c7ae802ac9e508b4e0c15f893 10/2/2007
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend