DECLARE @day CHAR(2)
SET @day = DATEPART(DAY, GETDATE())
PRINT @day
If today was the 9th of December, the above would print \"9\".
I want
For SQL Server 2012 and up , with leading zeroes:
SELECT FORMAT(GETDATE(),'MM')
without:
SELECT MONTH(GETDATE())
SELECT RIGHT('0'
+ CONVERT(VARCHAR(2), Month( column_name )), 2)
FROM table
select right('0000' + cast(datepart(year, GETDATE()) as varchar(4)), 4) + '-'+ + right('00' + cast(datepart(month, GETDATE()) as varchar(2)), 2) + '-'+ + right('00' + cast(datepart(day, getdate()) as varchar(2)), 2) as YearMonthDay
Pad it with 00 and take the right 2:
DECLARE @day CHAR(2)
SET @day = RIGHT('00' + CONVERT(NVARCHAR(2), DATEPART(DAY, GETDATE())), 2)
print @day
DECLARE @day CHAR(2)
SET @day = right('0'+ cast(day(getdate())as nvarchar(2)),2)
print @day
Leading 0 day
SELECT FORMAT(GetDate(), 'dd')