Wednesday, July 29, 2015

Convert datetime to MMDDYY format in sql server

If you ever needed to convert datetime field to MMDDYY format in sql server, read on. I am going to present you a quick tips on how to get date or datetime field in sql server in MMDDYY format. Let's do it step by step.

First of all I would like to remind you to learn how to convert date to different date string using Convert function of ms sql server. This will prepare background for the technique used in this article.

For the sake of easiness, I will use current date and time as the date field we would like to convert to MMDDYY format. As you all know, getdate() returns you current date and time, as below:

select getdate() as Today
Output is: 2015/07/29 05:00:47.563

First we will get this date in mm/dd/yyyy format. Here is how:
select convert(varchar(10),getdate(),1)
Output is: 07/29/15

Now we are almost there! Let replace those forward slashes (/) with empty string. And voila! We have MMDDYY. Here is the script:

select replace(convert(varchar(10),getdate(),1),'/','')
Output is: 072915

All the steps are put together below.
















Now we have successfully converted datetime field in sql server into MMDDYY format.

0 comments:

Post a Comment

Hope you liked this post. You can leave your message or you can put your valuable suggestions on this post here. Thanks for the sharing and cooperation!

Popular Posts

Recent Articles