Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2003
    Posts
    163

    Unanswered: Help request with UPDATE/CASE

    Hi,

    I have a table as follows:

    CREATE TABLE [MyTable] (
    [SaleYear] [smallint] ,
    [SaleMonthNum] [smallint] ,
    [SaleMonthName] [nvarchar] (20) ,
    [TotalSale] [money] NULL
    ) ON [PRIMARY]
    GO

    What I want is to update the field 'SaleMonthName' based on the 'SaleMonthNum' field using the CASE expression. I want something like this (pseudo-code):

    update Mytable
    set SaleMonthName
    Case:
    IF SaleMonthNum =1, then := 'January'
    IF SaleMonthNum =2, then := 'February'
    ...
    IF SaleMonthNum =12, then := 'December'


    Many TIA.

  2. #2
    Join Date
    Feb 2004
    Location
    Poland
    Posts
    96
    update Mytable
    set SaleMonthName = case when salemonthnum =1 then 'jan'
    when salemonthnum =2 then 'feb'
    when salemonthnum =3 then 'march'
    ......
    else '????' end

  3. #3
    Join Date
    Jan 2004
    Posts
    49
    Code:
    = case SaleMonthNum when 1 then 'January' when .... else '' end
    SQL Server 2000 Books Online (Updated 2004)

  4. #4
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    declare @MonthName varchar(20)
    set @MonthName = 'March'
    select month(cast(@MonthName + ' 1, 2004' as datetime))
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  5. #5
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Granted that I'm a lazy bum, but I'd use:
    Code:
    update Mytable
    set SaleMonthName = DateName(month, DateAdd(month, [SaleMonthNum], '2000-12-01'))
    -PatP

  6. #6
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Oops. Looks like I had the conversion requirements back asswards.


    Lazy is good! A busy DBA is a bad DBA.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  7. #7
    Join Date
    Oct 2003
    Posts
    163
    Thanks all. Many thanks

Posting Permissions

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