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

x
?
Solved

Exctract info from a date

Posted on 2007-11-30
4
Medium Priority
?
180 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
[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
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

636 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