How to get the result as 00 format for month or date of a datetime from SQL ?
Sometimes we need to fetch the month field from a date time with a sql query. At that time we use the datepart method of sql.
But it gives us the result like 1,2,3,4....
Suppose we need to get the result as 01,02,03,04.... Then we can use right method with replicate or simple right method to do so.
Take a look on the following sql query which give the proper result.
SELECT RIGHT(REPLICATE('0', 2) + CAST(datepart(MONTH,[Date]) AS VARCHAR(2)), 2) FROM Tablename
SELECT RIGHT('0' + convert(varchar(2),datepart(MONTH,[Date])), 2) FROM TableName