Yasserzaid’s Weblog

June 3, 2009

Get Month Name from SQL

Filed under: SQL Server — yasserzaid @ 7:46 am

Hi

try this example to get month name from SQL Server

SELECT DATENAME(MONTH,’08/15/1947′)

where date should be in format MM/dd/yyyy

Hope this helps

Good Luck

May 16, 2009

Easy SQL If Record Exists, Update It. If Not, Insert It.

Filed under: SQL Server — yasserzaid @ 12:43 pm

Hi

try this example :-

CREATE PROCEDURE dbo.spAddUser 
(    @UserID AS int,
     @FirstName AS varchar(50),
     @LastName AS varchar(50)    
)
AS
     BEGIN
          DECLARE @rc int
            UPDATE [Users]           
     SET FirstName = @FirstName, LastName = @LastName
           WHERE UserID = @UserID    
      /* how many rows were affected? */
         SELECT @rc = @@ROWCOUNT 
           IF @rc = 0 
             BEGIN    
               INSERT INTO [Users]                              
  (FirstName, LastName)                        
  VALUES(@FirstName, LastName)           
  END           
 END

Hope this helps

Good Luck

May 15, 2009

Renaming a Stored Procedure

Filed under: SQL Server — yasserzaid @ 11:40 pm

Hi

try this :-

Syntax:
sp_rename ‘procedure_name1′, ‘procedure_name2′
procedure_name1
The current name of the stored procedure
procedure_name2
The new name of the stored procedure.
A stored procedure can be renamed. The new name should follow the rules for identifiers.
Examples
Code:
EXEC sp_rename ’spGetAvgGrade’, ’spNewAvgGrade’;
Output:
Caution: Changing any part of an object name could break scripts and stored procedures.
The object was renamed to ’spNewAvgGrade’.
Explanation:
In the above example we change the name of the stored procedure spGetAvgGrade to spNewAvgGrade

Hope this helps

Good Luck

January 20, 2009

Get Month Name from SQL

Filed under: SQL Server — yasserzaid @ 7:46 pm

Hi

try this SQL query to get month name

SELECT ID,datename(month,PostDate) as Month FROM yourTable

where PostDate is field in my table of datetime type

Good Luck

January 8, 2009

Insert Null Value in DateTime Column SqlServer 2005 using asp.net

Filed under: ASP.Net, SQL Server — Tags: — yasserzaid @ 11:24 pm

Hi

try this example:

steps:

1) Create Simple web application (asp.net with C#).

2) create below table in the database. here i made table (table_1 in test database).

DataBase Name :Test
Table Name : Table_1
ColumnName       ColumnDataType
id               int (Auto Increment & Primary Key)
code             varchar(50) (Allow Null True)
date             datetime (Allow Null True)
 3) put the below control on the form.3.1) Calendar (id –> Calendar1)

3.2) Button (id –> btnInsert)

3.3) Button (id –> btnGet)

3.4) Label (id –> lblDataValue)

4) put the below code in to C# page.

Add below name space first :

using System.Data.SqlClient;
copy the below code and check. //connection string
    //please change as per your server and database
    string constr = @”Data Source=(local);Initial Catalog=test;Integrated Security=True”;
    public int insertedId
    {
        get { return Convert.ToInt32(ViewState["inserttedid"]); }
        set { ViewState["inserttedid"] = value; }
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        //if user is not selected any value from calendar at that time null is inserted
        //otherwise selected date is inserted
        string datetime = Calendar1.SelectedDate.Date.Equals(DateTime.MinValue) == true ? “Null” : “‘” + Calendar1.SelectedDate + “‘”;
        string strins = “insert into table_1 values ” +
            “(‘amit’,” + datetime + “);select scope_identity();”;
        SqlConnection sqlcon = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand(strins, sqlcon);
        sqlcon.Open();
        //get the id of inserted row using Scope_Identity function
         insertedId = Convert.ToInt32(cmd.ExecuteScalar());
        sqlcon.Close();
    }
    protected void btnGet_Click(object sender, EventArgs e)
    {
        //get the inserted record
        string strSelect = “Select * from table_1 where id =” + insertedId;
        SqlConnection sqlcon = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand(strSelect, sqlcon);
        sqlcon.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            //set the value in the label for display purpose 
            lblDataValue.Text = dr["date"] == System.DBNull.Value ? DateTime.MinValue.ToShortDateString() : Convert.ToString(dr["date"]);
        }
        sqlcon.Close();
    }
Thanks.

January 5, 2009

Display age from Birthday

Filed under: SQL Server — yasserzaid @ 7:50 pm

Hi

try this SQL Query to display age from Birthday

SELECT BirthDate, DATEDIFF(YYYY,BirthDate,GETDATE()) – CASE WHEN 100 * MONTH(BirthDate)+DAY(BirthDate) < 100 * MONTH(GETDATE())+DAY(GETDATE()) THEN 0 ELSE 1 END AS Age FROM [Some Table]

Hope it helps

Good Luck

December 25, 2008

Select top and last five record from Database

Filed under: SQL Server — yasserzaid @ 9:28 pm

In Query try this:

FOR TOP 5
select top 5 * from table

//——————-

FOR LAST 5
select top 5 * from table order by primarykey_columnname desc

Good Luck

Copy Table from Database to another

Filed under: SQL Server — yasserzaid @ 8:44 pm

Hi

try this example:

—————————————————-
Method : 1
—————————————————-

USE PropertyIdea
GO
—-Create Table
CREATE TABLE Country (CountryID int, Country nvarchar(50),Code int)

—-INSERT INTO Country using SELECT
INSERT INTO Country (CountryID , Country ,Code)
SELECT CountryID , Country ,Code
FROM [Kunj.Corp].[dbo].[Country]

—-Verify that Data in Country
SELECT * FROM Country

—————————————————-
Method : 2
—————————————————-
USE PropertyIdea
GO

—-Create Table

CREATE TABLE Country (CountryID
int, Country nvarchar(50),Code int)

—INSERT INTO Country using SELECT

INSERT INTO Country (CountryID, Country ,Code)

SELECT CountryID , Country ,Code

FROM [Kunj.Corp].[dbo].[Country]

—-Verify that Data in Country

SELECT * FROM Country

Good Luck

Blog at WordPress.com.