RSS

Convert Date

09 Sep

in this article will learn how to convert date :

  • 1- From AD to Hegira
  • 2- From Hegira to AD

AD = “01/01/2011”   & Hegira  = “01/01/1432”

  1. Convert Date From AD To Hegira

EXAMPLE NO 1

set dateformat dmy
DECLARE @DATEM DATETIME
DECLARE @ResultH NCHAR(10)
SET @DATEM = ’19/03/2011′
SET @ResultH = (SELECT CONVERT(NCHAR(10), @DATEM, 131))
SELECT @ResultH
PRINT ‘The Date Time IS’ + ‘ ‘ + CAST(@ResultH AS NVARCHAR(50))
PRINT ‘————————————-‘

—————————————————————————————————–

EXAMPLE NO 2

Create Table Convert_Date
(ID int,
[date] datetime)

—- data type of [date] column must be datetime but if you will insert Hegira date the column data type must be Nvarchar
GO
Insert  into Convert_Date values (3,’01/01/2011′),(3,’10/09/2011′)
GO
SET DATEFORMAT DMY
SELECT   ID,[DATE] ,CONVERT(NCHAR(10),CAST([DATE] AS DATETIME ) ,131)
AS  Hegira FROM Convert_Date

—————————————————————————————————-

2-Convert Date From Hegira  to AD

Example No 1
DECLARE @ResultM DATETIME
DECLARE @DateH NVARCHAR(50)
SET @DATEH = ’09/04/1405′
SET @ResultM = (SELECT CONVERT(DATETIME, @dateH, 130))
SELECT CONVERT(NCHAR(10),@ResultM,103)
PRINT ‘The Date Time IS ‘ + ‘ ‘ + CAST(@ResultM AS NVARCHAR(50))
PRINT ‘————————————-‘

 
Leave a comment

Posted by on September 9, 2011 in Script

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s