Tuesday, February 14, 2012

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())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())|||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.|||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.|||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|||this worked just fine, but I want the format to be 2003-05-15 not 2003.05.15.
How do I do that?|||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.|||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.

No comments:

Post a Comment