Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Format Date in SQL Server Query

Posted on 2016-10-04
5
Medium Priority
?
101 Views
Last Modified: 2016-10-04
I'm sure this is simple but I can't seem to figure it out. I need to format my date field to just mm/dd. How can I do this in my SQL Query?
0
Comment
Question by:Lawrence Salvucci
5 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 41827900
You could just use:

select right('0' + cast(month(getdate()) as nvarchar(2)), 2) + '/' + right('0' + cast(day(getdate()) as nvarchar(2)), 2)

Open in new window

0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 2000 total points
ID: 41827903
Or relying on truncation:

select convert(nvarchar(5), getdate(), 101)

Open in new window

0
 
LVL 35

Expert Comment

by:Pawan Kumar
ID: 41827904
Please try below. Really easy..

SELECT FORMAT ( GETDATE() , 'MM/dd' ) [MonthDay]

Open in new window

0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 41827906
FORMAT function is available since SQL Server 2012. If you're using 2012 version or superior then you can use SELECT FORMAT( @d, 'MM/dd', 'en-US' )
0
 
LVL 1

Author Closing Comment

by:Lawrence Salvucci
ID: 41827909
Thank you very much for your quick response. This was the easiest solution and worked exactly how I wanted it to. Thank you again.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question