We help IT Professionals succeed at work.

HOw to format date in SQL server ('yyyy-mm')

dkim18 asked
Last Modified: 2008-01-09
How can format this date field into yyyy-mm?
I am doing monthly report which counts items by date field.
ID  DateReceived Items Status
2   2002-10-20       3      active
3   2006-02-22      4        inactive

From this I do monthly counting and get something like this:
Month   TotalItem   Totalactive TotalInactive
2002-02    20             3                4
2006-10    33            3                   3

The reason I use this 'yyyy-mm' is to sort them in montly order.

I already have working sql for the excel/access but I migrate the data to the sql server now.
I used this format(DateReceived, 'yyyy-mm') but sql doesn't allow this function.

I tried this,
convert(varchar(4),month()) + '-' + convert(varchar(2),month())
but this give 2002-2....instead of one i need like 2002-02.

I also tried Left(Datereceived, 7) but this gave out October 20 instead of 2007-10.

Any idea??

Watch Question

Doug BishopDatabase Developer

CONVERT(CHAR(7), myDate, 121)


Found it.
CONVERT(CHAR(7), myDate, 120)

Anyway, do you know if IS NOT NULL can be used in sql server 2000??
I think it is OK in sql server 2005 but I am getting an error from 2000

Doug BishopDatabase Developer

Should be okay. Are you stating:


I was getting errors from my excel sql but I think IS Not Null is ok.
I have something like this:

Sum(IIF(Item='a',1,0)) as ItemCount.

How can I change this so it would work in SQL server?

I tried to use the case but didn't quite make it working.

Database Developer
Unlock this solution and get a sample of our free trial.
(No credit card required)
Unlock this solution and get a sample of our free trial.
(No credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.