Solved

Exctract info from a date

Posted on 2007-11-30
4
169 Views
Last Modified: 2010-04-21
I'm very used to the MySQL and PHP environment, but recently, I started a new job where MSSQL is the norm. I'm currently trying to figure out if there is a function similar to MySQL's date_format or PHP's date. I need to extract the month and year number from a date. For example, if I'm given Jan 2008, I would like to obtain 1-2008. Thanks!
0
Comment
Question by:horalia
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20386155
in mssql server, you have the function DATEPART() which will be very helpful...
CAST(DATEPART(month, yourfield) AS VARCHAR(2)) + '-' + CAST(DATEPART(year, yourfield) as varchar(4))


you could consider checking out the CONVERT() function, it has also some interesting things...

CONVERT(VARCHAR(10), yourdatefield, 120)

will return YYYY-MM-DD formatted date string value for example
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 20386165
Select CAST(DATEPART(Month, YourDate) as varchar(2)) + '-' + CAST(DATEPART(year, YourDate) as char(4))
0
 
LVL 1

Expert Comment

by:wizengamot
ID: 20386168
The best solution would be to use the Year, Month and Day Functions.  Lets say I have a field called currentDate in my database and want to return it in the format you specified 1-2008.  Here is the code that would do that.  The cast function is required because you are building a string and values that come out of the Month,Day and Year functions are numbers and do not work well with the + operator unless casted.
SELECT currentDate, Cast(Month(currentDate) As Varchar) + '-' + Cast(Year(currentDate) As Varchar) As "NewDate" FROM myTable

Open in new window

0
 

Author Closing Comment

by:horalia
ID: 31412058
Excellent, fast and accurate!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now