different SQL Server date formats

  Below is a list of formats and an example of the output.  The date used for all of these examples is “2006-12-30 00:38:54.840”. DATE ONLY FORMATS Format # Query Sample 1 select convert(varchar, getdate(), 1) 12/30/06 2 select convert(varchar, getdate(), 2) 06.12.30 3 select convert(varchar, getdate(), 3) 30/12/06 4 select convert(varchar, getdate(), 4) 30.12.06

[Solved]: Suspect database in sql server 2012

To solve this problem you need microsoft sql server management studio. Open with administrator access. Now open new query and use the below query. Replace my_db with your database. EXEC sp_resetstatus my_db; ALTER DATABASE my_db SET EMERGENCY DBCC checkdb(my_db) ALTER DATABASE my_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE DBCC CheckDB (my_db , REPAIR_ALLOW_DATA_LOSS) ALTER DATABASE my_db

Set The Gridview’s Datasource Property And Simply Call The Databind Method

SqlConnection Conn = new SqlConnection(“Data Source=Localhost\\SQLEXPRESS;Initial Catalog=Flash2;Integrated Security=True;”); SqlDataReader rdr = null; string commandString = “SELECT * FROM USER_MASTER”; try {        Conn.Open();        SqlCommand Cmd = new SqlCommand(commandString, Conn);        rdr = Cmd.ExecuteReader();        MasterCustView.DataSource = rdr;        MasterCustView.DataBind(); } catch (Exception ex) {      // Log error } finally {     if (rdr != null)     {         rdr.Close();     }     if (Conn != null)     {         Conn.Close();     }  }

2nd Highest Salary in SQL Server

2nd Highest Salary in SQL Server select MAX(ID) from Employees where ID NOT IN (Select MAX(ID) from Employees) select MAX(ID) from Employees where ID < (Select MAX(ID) from Employees ) select top 1 ID from (select top 2 ID from Employees order by id desc) as tblEmployee order by ID ASC  Using dense_rank to retrieve 2nd Highetst salary if we have duplicate rows: with CTETable as ( select ID, Name, DENSE_RANK() over (order by ID desc) as Rnk from Employees ) select Name, ID from CTETable where rnk=2

Reset table auto increment column in ms sql server

Reset table auto increment column in ms sql server If we want to reset the auto increment value of any table to any other value than default value, we need to use DBCC query. To use DBCC query you must open sql server management studio with Administrator permission. Check the below query: DBCC CHECKIDENT(‘tblUser‘, RESEED, 1000)

Fetching data from two or more tables(Joining of tables)

Selecting data from two or more tables(Joining of tables) Hello Friends, today we will learn how to select data(records) from more than one table. For this purpose we will use joining of tables. Joining simply join one or two or more than two tables by some conditions as per need. One there is a joining between one

create a cursor in ms sql server

create a cursor in ms sql server Cursor is used to fetch records(rows) from any table sequentially and records can be manipulated, calculated as per your need. To create a cursor use this sql query: declare cursor_name cursor for select id from tbl_Category open cursor_name fetch next from cursor_name into @Id while @@fetch_status=0 begin insert into table_name(id) values

datagridview in desktop application-showing data

Datagridview in desktop application-showing data Hello friends, how are you? And how is your programming? Today we will try to fetch some data from database and will show in the windows form. To do so, we need to use a Data Control.  We will use datagridview control by expanding Data and then  drag the control

introduction of store procedure in mssql server

Hello friend, It is very bad habit and difficult to write separate sql query for each operation in sql query. The best solution for this problem is procedure . Procedure is similar to function which we will learn in next chapter, is collection of code which can be executed at any time. To create a procedure open

insert, delete and update queries using mssql server

Insert Query insert into table_name(column1,column2,column3,column4) values(value1,value2,value3,value4) We can also use design view to insert values into table. If you dont know how to create a table please follow the previous post create a table in mssql server . Just Right click on the Table_name in which you need to insert the table and select open table. Now put the