Tuesday, July 08, 2014

Get Month part from date

The following sql queries can give month part from date:

SELECT DATEPART(MONTH, GETDATE()) AS [month]
SELECT DATEPART(MM, GETDATE()) AS [month]
SELECT DATEPART(M, GETDATE()) AS [month]

| month |
|   7   |

The following sql queries can give month (mm) from date:

SELECT RIGHT('00' + CAST(DATEPART(MONTH, GETDATE()) AS VARCHAR(2)),2) AS [month]
SELECT RIGHT('00' + CAST(DATEPART(MM, GETDATE()) AS VARCHAR(2)),2) AS [month]
SELECT RIGHT('00' + CAST(DATEPART(M, GETDATE()) AS VARCHAR(2)),2) AS [month]

| month |
|   07  |

The following sql queries can give month name from date:

SELECT DATENAME(MONTH, GETDATE()) AS [month]
SELECT DATENAME(MM, GETDATE()) AS [month]
SELECT DATENAME(M, GETDATE()) AS [month]

| month |
|  July |

The following queries gives month name(mmm) from date:

SELECT LEFT(DATENAME(MONTH, GETDATE()),3) AS [month]
SELECT LEFT(DATENAME(MM, GETDATE()),3) AS [month]
SELECT LEFT(DATENAME(M, GETDATE()),3) AS [month]

| month |
|  Jul  |