Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

return month and day from a date

Posted on 2016-10-07
7
Medium Priority
?
45 Views
Last Modified: 2016-10-07
I want to return month and date to look like this

02/17

What would the syntax be?

So how would i modify

SELECT GetDate() to return what i need?
0
Comment
Question by:vbnetcoder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 43

Expert Comment

by:Eugene Z
ID: 41833787
SELECT GetDate() ,convert(varchar(5),getdate(),101)
more
BOL
https://msdn.microsoft.com/en-us/library/ms187928.aspx
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 41833790
This works on my 2012 box
Declare @dt date = '2016-02-17'
SELECT FORMAT(@dt, 'MM/dd')

Open in new window

Keep in mind this returns a varchar and not a date data type.

If you're running 2008 and FORMAT is not available, you may have to do this (not tested as I don't have a 2008 box handy)
Declare @dt date = '2016-02-17'
SELECT RIGHT('0' + CAST(MONTH(@dt) as varchar(2)), 2)  + '/' + RIGHT('0' + CAST(DAY(@dt) as varchar(2)),2) 

Open in new window

0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 41833791
Try this:
SELECT RIGHT(CONVERT(VARCHAR, GETDATE(), 10),5)

Open in new window

0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:vbnetcoder
ID: 41833794
On other option is to make the date the first day

So, I found this syntax that should work.  Do you think there is a better way of doing this?

   SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, getdate()), 0) AS [year_month_date_field]
0
 
LVL 43

Expert Comment

by:Eugene Z
ID: 41833804
you Q was "I want to return month and date to look like this:
the above post will give what you need
if you have 2nd Q " get date the first day of this Month"
the one of options
you code is the one of the most populars   SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, getdate()), 0)
0
 

Author Closing Comment

by:vbnetcoder
ID: 41833807
ty
0
 
LVL 43

Expert Comment

by:Eugene Z
ID: 41833811
you can try if you'r on sql 2012
SELECT DATEADD(DAY,1,EOMONTH(GETDATE(),-1))
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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

609 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