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

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

select the record with the lowest datediff()

Posted By:      Posted Date: September 08, 2010    Points: 0   Category :Sql Server
Hello, I am trying to figure this out and can't quite get it. I have a subselect in my select that can of course only return one row. In some cases it will return more than one, to filter out the other rows we are going to only return the row with the least amount of days between 2 dates. I got it work as shown below but I hope that there is a better way to do this. select CreatedDate, ConfirmedDlv, inventtransid, min(DATEDIFF(DAY,CreatedDate, ConfirmedDlv)) from SalesLine where projId = '005124' and ItemId = '105732' and dataareaid = 'dfg' group by CreatedDate, ConfirmedDlv, inventtransid having min(DATEDIFF(DAY,CreatedDate, ConfirmedDlv)) = ( select min(DATEDIFF(DAY,CreatedDate, ConfirmedDlv)) from SalesLine where projId = '005124' and ItemId = '105732' and dataareaid = 'dfg' ) Anyone have any tips?   Thanks

View Complete Post

More Related Resource Links

select max record to join another table sybase

select a.pono,(select (user) from user where userid=a.userid having date=max(date)) as user from a inner join b on a.no=b.no  in the result , i have selected the same id and retrieve two records every thing are same except the date how can i select the record out of two record which date is max date as the where Clasuse to select correct user poid    date                name 1        12/08/2010      Mary 1        20/08/2010      Peter   now i would like to select name which id=1 and date is max and then use the name to join another table because name is foreign key  

Problem Select ancestors a Record

Hi; I have a table by three columns ("ID","Title","ParentID"),me want to select ancestors a Record by "ID" identifier. example: ID      Title       ParentID 1         Library       null 2         Section1      1 3        Section2       1 4        1.1              2   for example if record 4 selected return records by "ID" identifier 2,1 How can me do it?  

How to select bottom record if there is no id in table.

Hi All, I am trying to select the bottom record but there is no id in table. For Example A              B jo             john so             som mo            mon to              tom go              gone

Select updated record after edit

Hello!On one (aspx) page I have a GridView (GridView has multiple pages). After a user selects a row/record and clicks "edit" he's redirected to another (.aspx) page where he edits the selected record via DetailsView. After clicking update on DetailsView he's redirected back to the page containing the GridView.What is the simplest/shortest way that the updated record is automatically selected on the Gridview (e.g. at Page_Load event)?Thx in advance!M

How to select a master record plus related custom fields in the same row?


Consider this table of master records:

project_id  project_name

1               Important Project

2               More Important Project


My application allows users to create an arbitrary number of custom fields for projects. Here is the table of custom field values:

custom_field_value_id   project_id   custom_field_name   custom_field_value

1                                  1               Project Manager        Jim Smith

2                                  1               Due Date                  12-31-2010

3       &

select record based on / match selected date

with the sql statement below, i try to select the records based on the selected date, it return no rows from adapter filled datatable. 
however, when i run the query in sql query in sql server studio management, it returns records. 
date format = 09/28/2010

string[] splitDT = txtSelectedDate.Text.Split('/');

            string strRearrangeDT = splitDT[0] + "/" + splitDT[1] + "/" + splitDT[2];

using (SqlConnection sqlconn = new SqlConnection(connstring))
                DataTable dt = new DataTable();
                string strSQL = "SELECT userclaim_details3.id, userclaim_details3.userclaimid, userclaim_details3.userid, userclaim_details3.childcategorytableid, userclaim_details3.statusid, " +
                         "userclaim_details3.amount AS amountname, userclaim_details3.remark AS remarkname, userclaim_details3.datecreated AS datecreatedname, " +
                         "userclaim_details3.datemodified, approvaltable.id AS Expr1, approvaltable.typename AS statusname, usertable2.id AS Expr2, usertable2.name AS uname, " +
                         "childcategorytable.id AS Expr3, childcategorytable.categor

how to select the record when their is no record on the other table


Hi all,

 i am working on migration project  i have to select the record from one table which is not prescent in other depending on the condition

for example i have two table one is master table & other is transaction Table 

structure of master table is as follows

Sb_Type      Sb_acno            name                 Id -------------------------------------

 N              na/0005               aaa                   12 -------------------------------

M               Mb/0002              bbb                   15 -------------------------------------

--                  ----                   --     &nbs

select record where field = variable?


Hi, I'm very new to SQL so please excuse how simple this problem is:

I've got a web page which contains a form, the form holds the following boxes:

  • Latitude
  • Longitude
  • Accuracy
  • Update
  • UserName

I have also got an access database , containing one table (Table1) which has the fields:

  • Latitude
  • Longitude
  • Accuracy
  • Update
  • UserName

The "UserName" field is the primary key (containing an email address)

What I want to do is select the record that has the same email address as entered into the form and update the rest of the fields with the data entered into the form.

The problem I'm having, as I see it, is that I can't seem to compare a string to a record UserName?

When I enter the email address manually it works and updates the record but it won't pull the email address from the variable string?

Here is my .asp code:


'Dimension variables
Dim adoCon 'Holds the Database Connection Object
Dim rsUpdateEntry 'Holds the recordset for the record to be updated
Dim strSQL 'Holds the SQL query for the database
Dim email 'Holds the current users email address

email = (Request.Form("UserName"))

Set dbConn = Server.CreateObject(

What is the code to say if the select statement of sqldatasource1 control has no record, then..


What is the code to say:-

 if the select statement of sqldatasource1 control has no record, then..

Pls help... thanks




modifying the last record of a fiitered SELECT



I have a SQL data source control defined to filter data from a SQL database, sorted according to the date time in descending order. When i try and update a field of the  latest record, the entire column, related to the field of the filtered search gets modified. I want to only update a field of the last record i.e. the last entry of the filtered data. How can i achieve it?

AJAX GridView Checkbox Select All in ASP.Net

In the previous article GridView Select Multiple Rows you learnt the multiple row selection in GridView control. Here you will get the C# code sample to select all the gridview rows using single checkbox server control. You can highlight the selected rows with different color and Font state Bold. If you want to use the selection values on different ASP.Net

GridView inline Master/Detail record display

This code drop is part of a smash and grab series. If you're in a rush, you can just grab this code and insert it into your application, no understanding is required. When you have some time (ya, right) you may want to review the source code.

Get Top 1 Record from Dataset

Many of them in need of getting the Top 1 or Top 10 records from a Dataset without going to SQL query. It is possible to do from Code Behind. I have given the code below,
The below code is to get single record from a Dataset. Also I have given for getting 8 records at Green Color.

Enterprize library 4.1 getting output parameter after adding record in db, plz guide



I need to get output parameter (flag) from db after saving record in database. I am using Microsoft Enterprise Library 4.1 for DB. I am inserting record using I Data reader.

Please guide


Gridview Edit on Searching of Record


using Asp.net c#

sql server

i am searching a record from my database and displaying it in gridview now i want the comments should be updated as per the record which is being searched

For E.g if record No.3 is searched then only i can update record no.3 and i don't want rest of data to be shown

now what is happening when i click on edit my whole table is being displayed

this is bcuz i made one method gridfill() which i m calling everytime if i m doing grid row editing,row cancelling and row updating

how i can resolve this thing

my code

 protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        GridView2.EditIndex = e.NewEditIndex;


 protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
        string id = GridView2.Rows[e.RowIndex].Cells[0].Text;
        TextBox t;
        t = (TextBox)GridView2.Rows[e.RowIndex].Cells[2].Controls[0];
        string query = "update user1 set comments=@comments where id=@id";
        SqlConnection con = new SqlConnection(connstr);
        SqlCommand cmd = new SqlCommand(query, con);

Select value in gridview


Hi, I am new to gridview. I want to make a gridview. Its row when selected should direct me to a specific .aspx page.Each row should direct to a different page. Should I use querystrings for this? Please suggest. Regards, ap.

Does this look correct for saving a file/ new record to a database


So thank you for all your help so far, So I think i got this right,
but would like for you all to look it over to see if there is any
areas I might run into trouble with:

//using asp.net mvc 2
the table (sql server 2005, using entities framework)
table fields
"Pattern_Media_GUID  uniqueid 
"Pattern_GUID  uniqueid
"Media varbinary(max)  
"height int
"Width int
"Media_Type String (50)

the form:
<h2> Add Media to this pattern</h2>
<% Html.EnableClientValidation(); %>
<% Html.BeginForm("AddMedia", "Pattern", new { id= Model.Pattern_Guid} , FormMethod.Post, new {enctype = "multipart/form-data"}); %>
    <%: Html.Label("height") %>
    <%: Html.TextBox("height") %>

    <%: Html.Label("width") %>
    <%: Html.TextBox("width") %>

    <%: Html.Label("Media") %>
    <input type="file" id="Media" name="Media" />
    <input type="submit" name=

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