Results 1 to 2 of 2

Thread: Adding new columns to a table using the datepart function SQL

  1. #1
    Join Date
    Feb 2013
    Posts
    1

    Adding new columns to a table using the datepart function SQL

    Morning Everyone,

    I have just started training on SQL and was wondering if you could help with a problem I have encountered.

    I have populated a column with a date via the CREATE TABLE Function

    USE Occupancy
    CREATE TABLE Time
    (Date date not null)

    DECLARE @StartDate Date,
    @EndDate Date

    SET @StartDate = '01 Jan 2010'
    SET @EndDate= '31 March 2015'
    WHILE @StartDate <= @EndDate

    BEGIN
    INSERT INTO Time
    VALUES (@StartDate)
    SET @StartDate = Dateadd(dd,1,@StartDate)
    END

    But what I would like to do now is create 4 Extra columns called CalendarYear, CalendarMonth, FinancialYear and finally FinancialMonth, I know I should use the Datepart function but not matter how many time I have written it, it doesn't seem to work.

    All help is much appreciated.

    Thanks

    Wayne

  2. #2
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    What kind of error?. As you said you have to use DATE functions to get date part or date calculations.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •