Results 1 to 8 of 8

Thread: Date again

  1. #1
    Join Date
    Sep 2003
    Location
    Sweden
    Posts
    19

    Unanswered: Date again

    I changed my datefunction to look like this instead, but I stil want to add a parameter so that the user can decide if it shoulb be 2003 or 03. I've been trying back and forward but can't get anywhere. Please se I YOU can help!

    create function SvensktDatum3(@indate datetime)
    returns nchar(20)
    as
    begin
    return
    CONVERT(CHAR(10), @indate, 126)
    end

    select dbo.SvensktDatum3(getdate())

  2. #2
    Join Date
    Feb 2003
    Location
    @ home
    Posts
    163

    Re: Date again

    Can you explain a little bit more what you want to do?
    From what i realise it seams that you can put a flag on the function tha could be 0 and 1. 0 to use a four digit year and 1 to use a two digit year.

    Paulo

    Originally posted by buggirl
    I changed my datefunction to look like this instead, but I stil want to add a parameter so that the user can decide if it shoulb be 2003 or 03. I've been trying back and forward but can't get anywhere. Please se I YOU can help!

    create function SvensktDatum3(@indate datetime)
    returns nchar(20)
    as
    begin
    return
    CONVERT(CHAR(10), @indate, 126)
    end

    select dbo.SvensktDatum3(getdate())

  3. #3
    Join Date
    Sep 2003
    Location
    Sweden
    Posts
    19
    I want to be able to show the year with 2 or 4 digits depending on a value I give when I call the function.

  4. #4
    Join Date
    Feb 2003
    Location
    @ home
    Posts
    163
    Use a bit flag.
    You save the date with four digit year in the table database, then you pass the parametter 1 or 0 and do the right cast for the date.

    Paulo

    Originally posted by buggirl
    I want to be able to show the year with 2 or 4 digits depending on a value I give when I call the function.

  5. #5
    Join Date
    Jul 2002
    Location
    Village, MD
    Posts
    621

    Re: Date again

    Try this:

    alter function SvensktDatum3(@indate datetime,@cent tinyint)
    -- @cent<>1 - 2 digits
    -- @cent=1 - 4 digits
    returns nchar(20)
    as
    begin
    return CONVERT(CHAR(10), @indate, case @cent when 1 then 102 else 2 end)
    end

  6. #6
    Join Date
    Sep 2003
    Location
    Sweden
    Posts
    19
    this worked just fine, but I want the format to be 2003-05-15 not 2003.05.15.
    How do I do that?

  7. #7
    Join Date
    Jul 2002
    Location
    Village, MD
    Posts
    621
    Originally posted by buggirl
    this worked just fine, but I want the format to be 2003-05-15 not 2003.05.15.
    How do I do that?
    If you want to see this format in application - change format of date on application level or change settings on computer.
    if you want to see this in QA or EM - it is possible to do but why you need it.

  8. #8
    Join Date
    Sep 2003
    Location
    Sweden
    Posts
    19

    I've solved the problem.

    This is my solution:

    create function SwedishDate(@indate datetime, @separator char(1), @year_digits int)
    returns nchar(20)
    as
    begin
    declare @manad nvarchar(20), @dag nvarchar(20), @year nvarchar(10)
    set @manad = right('00' + cast((datepart(mm, @indate)) as varchar(2)), 2) + @separator
    set @dag = right('00' + cast((datepart(dd, @indate)) as varchar(2)), 2)
    if @year_digits = 2
    set @year = reverse(cast(reverse(ltrim(convert(nvarchar(20), datepart(yyyy, @indate)))) as char(2)))
    else
    set @year = convert(nvarchar(20), datepart(yyyy, @indate))
    return @year + @separator + @manad + @dag
    end

    call it like this:

    select dbo.SwedishDate(getdate(), '-', 2)

    Thanks for all the help on the way!

    Originally posted by snail
    If you want to see this format in application - change format of date on application level or change settings on computer.
    if you want to see this in QA or EM - it is possible to do but why you need it.

Posting Permissions

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