Solved

Exctract info from a date

Posted on 2007-11-30
4
172 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 143

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parsing the XML data to SQL Server 4 67
how many extra RAM for SQL server is needed 22 43
Query 14 57
How do I subtract date and time within a same column in SQL 4 41
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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