http://feeds.feedburner.com/SpendYourTimeHere-Once

Current Affiares

Showing posts with label SQL SERVER 2005 AND QUERY. Show all posts
Showing posts with label SQL SERVER 2005 AND QUERY. Show all posts

Monday, August 13, 2012

How to get the max value of a primary key field in java

How to get the max value of a primary key field in java?

// in java file


package testlab.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.jdbc.core.JdbcTemplate;


public class TestlabUtilsDb {

    private JdbcTemplate jdbcTemplate;

  
    public  void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
// maxquery could be select Max(id) as id from tablename

    public  int maxId(String maxquery) {
        int maxid = 0;
        PreparedStatement pstmt = null;
        Connection con = null;
        ResultSet rs;
        try {
            con = jdbcTemplate.getDataSource().getConnection();
            pstmt = con.prepareStatement(maxquery);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                maxid = rs.getInt(1);
            }
            if (maxid == 0) {
                maxid = 1;
            } else {
                maxid = (maxid + 1);
            }
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException ex) {
                    Logger.getLogger(TestlabUtilsDb.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(TestlabUtilsDb.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return maxid;

    }
}

Monday, July 16, 2012

how to rename table name?

sp_rename 'old_tablename' ,'new_tablename'

How to count no of duplicate record in a field of a table?

select count(*) as 'NoOfRecord',fname from test 
group by fname having count(fname)>1

Sunday, July 15, 2012

How to get a second largest salary from database?

How to get a second largest salary from database?

Below will work in all database----
select * from tablename t where 
2 =(select count(distinct id) from tablename  where t.id<=id)

Below will work in sql server and mysql only ---
select * from tablename where
 id = (select min(id) from 
(select top 3 id from tablename order by id desc) tb)

Wednesday, July 11, 2012

How to search date between two date?

select * from report_mstr where convert(varchar(10),modified_date,120)
between '2012-07-04' and '2012-07-11'
order by modified_date desc
;

How to get the last 7 days values of a datefield in sql server 2005?

Here is the last 7 days values query without including their time ---
report_mstr = table name
modified_date = datefield


select * from report_mstr 
where (convert(varchar(10),modified_date,120)
between convert(varchar(10),(getdate()-6),120) and convert(varchar(10),getdate(),120))
order by modified_date desc;


 --------
select getdate();
->>>> 2012-07-11 15:41:31.547
select getdate()-6; 
->>>>  2012-07-05 15:40:20.983
select convert(varchar(10),getdate(),120);
->>>> 2012-07-11

My Blog List

Popular Posts

All Rights Reserved To SYTHONCE. Ethereal theme. Powered by Blogger.