Getting correct results back from variable

  • Hi there,

    I'm using the expression below to return only the month and year. I'm implementing this code all over my sproc and would like to make it more readable.

    I was wondering if it was possible to create a variable and adding the expression below so that it returned the month and year only:

    CONVERT(CHAR(4), getdate(), 100) + CONVERT(CHAR(4), @getdate-2(), 120)

    If I declare the variable as either datetime or varchar I don't get the desired results back:

    Datetime returns the day and Varchar returns N.

    Essentially what I'm trying to do is to return the month and year together but with the least code so when I implement it, the code doesn't have long expressions.

    If anyone could help me I'd be very grateful.

    Many thanks

  • What would be the correct format for today's date? (Need a sample of what exactly you want in order to help out here.)

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Hey,

    For example, for todays date, I'd like:

    Nov 2009

  • What's wrong with this:

    declare @DateFormatted char(8);

    select @DateFormatted = CONVERT(CHAR(4), getdate(), 100) + CONVERT(CHAR(4), getdate(), 120);

    select @DateFormatted;

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • I'm a moron do you know that!

    Of course the reason I was returning N when using Varchar is becuase I never specified how many characters I wanted back

    Thanks for your help:)

  • You're welcome. It's usually the simplest things that cause the most confusing problems.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply