Results 1 to 4 of 4

Thread: Dateadd

  1. #1
    Join Date
    Aug 2009
    Posts
    2

    Unanswered: Dateadd

    I have a table contain BulKewangan(char(10)) and ThnKewangan(int).
    Example of BulKewangan is 21/9 and ThnKewangan is 2009.
    So I want to combine both fields and then I want to add 4 months to that combination. How can I do that? I already do the query but still canot run

    --sql--
    select DateAdd(Month, 4, convert(datetime,TkhTK+'/'+convert(varchar,ThnKew),103)) as Hingga
    from statprosesaudit

  2. #2
    Join Date
    Jun 2009
    Location
    CA, USA
    Posts
    59
    declare @v char(10), @int int
    select @v = '21/9', @int = 2009
    SET dateformat DMY
    select convert (char(10), dateadd (mm, 4, convert(datetime, rtrim(@v)+'/'+ltrim(convert(char(5),@int) )) ), 103)
    MohammedU
    SQL Server MVP

  3. #3
    Join Date
    Aug 2009
    Posts
    2
    if I run query like this
    select convert (char(10), dateadd (mm, 4, convert(datetime, rtrim(TkhTK)+'/'+ltrim(convert(char(5),ThnKew) )) ), 103) from tablename.
    An error 'Conversion failed when converting datetime from character string.'
    appear. How can I fix it?

  4. #4
    Join Date
    Nov 2004
    Posts
    1,427
    Provided Answers: 4
    I have altered your script a little bit, by removing the LTRIM and casting ThnKew to a CHAR(4) instead of a CHAR(5)
    Code:
    select convert (char(10), dateadd (mm, 4, convert(datetime, rtrim(TkhTK)+'/'+convert(char(4),ThnKew) ) ), 103)
    Execute
    Code:
    select rtrim(TkhTK) + '/' + convert(char(4),ThnKew)
    Check if the results are all valid dates of the DD/MM/YYYY type.
    With kind regards . . . . . SQL Server 2000/2005/2012
    Wim

    Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
    Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

Posting Permissions

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