Solved

Exctract info from a date

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

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…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

17 Experts available now in Live!

Get 1:1 Help Now