Archive

Archive for April, 2011

Convert DateTime to Different Date Format in SQL Server

April 13, 2011 1 comment

Hi all,

try this example to Convert DateTime to Different Date Format in SQL Server like dd/MM/yyyy , MM/dd/yyyy,…etc

Open SQL Server and Open a new SQL Query :-


DECLARE @DateTime DATETIME
SELECT @DateTime =getdate()

/*For mm/dd/yyyy format*/
Select CONVERT(VARCHAR(10),@DateTime ,101) as Date

/*For yyyy.mm.dd format*/
Select CONVERT(VARCHAR(10),@DateTime ,102) as Date

/*For dd/mm/yyyy format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,103) AS Date

/*For dd.mm.yyyy format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,104) AS Date

/*For dd-mm-yyyy format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,105) AS Date

/*For hh:mi:ss format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,108) AS Date

/*For mm-dd-yyyy format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,110) AS Date

/*For yyyy/mm/dd format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,111) AS Date

/*For yyyymmdd format*/
SELECT CONVERT(VARCHAR(10),@DateTime ,112) AS Date

Hope this helps

Good Luck

Advertisements
Categories: SQL Server

Make Response.Redirect() open in New Blank Window

April 12, 2011 2 comments

Hi all,

try this example to Make Response.Redirect() open in New Blank Window

1) Open VS2008 and create a new Website Project

2) Add new Web Page and from ToolBox add Button Control

<div>
        <asp:Button ID="btn_Go" runat="server" Text="Button" onclick="btn_Go_Click" OnClientClick ="document.forms[0].target = '_blank';" />
</div>

and In Code behind :-

protected void btn_Go_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://www.google.com");
    }

Hope this helps

Good Luck

Categories: ASP.Net

Calculate Age in Day-Month-Year using SQL Server Query

April 7, 2011 7 comments

Hi all,

try this example to create SQL Server Stored Procdure to Calculate  Age in Day-Month-Year

create proc CalculateAge
(
@dayOfBirth datetime
)
as
begin
DECLARE @today datetime, @thisYearBirthDay datetime
DECLARE @years int, @months int, @days int 
SELECT @today = GETDATE() 
SELECT @thisYearBirthDay = DATEADD(year, DATEDIFF(year, @dayOfBirth, @today), @dayOfBirth) 
SELECT @years = DATEDIFF(year, @dayOfBirth, @today) - (CASE WHEN @thisYearBirthDay > @today THEN 1 ELSE 0 END) 
SELECT @months = MONTH(@today - @thisYearBirthDay) - 1 
SELECT @days = DAY(@today - @thisYearBirthDay) - 1 
SELECT @years as years, @months as months, @days as days
end

and to use this Stored Procedure

exec CalculateAge '01/21/1984'

Hope this helps

Good Luck

Categories: SQL Server