all groups > sql server programming > november 2006 >
You're in the

sql server programming

group:

How to encode a date?



How to encode a date? Stijn Verrept
11/24/2006 6:09:27 PM
sql server programming: A function I seem to be missing in TSQL is to encode a date.

For example: EncodeDate(2006, 8, 23)

How can I best do this?

--
Thanks in advance,

Re: How to encode a date? Arnie Rowland
11/24/2006 7:22:30 PM
Roll you own...

Perhaps something like this (NO error handling included.)

CREATE FUNCTION dbo.EncodeDate
( @Year char(4),
@Month char(2),
@Day char(2)
)
RETURNS datetime
AS
BEGIN
RETURN cast( @Year + '/' + @Month + '/' + @Day AS datetime )
END
GO

SELECT dbo.EncodeDate(2006, 8, 23)

---------------------------=20
2006-08-23 00:00:00.000

--=20
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.=20
Most experience comes from bad judgment.=20
- Anonymous

You can't help someone get up a hill without getting a little closer to =
the top yourself.
- H. Norman Schwarzkopf


[quoted text, click to view]
Re: How to encode a date? David Portas
11/25/2006 3:18:38 AM
[quoted text, click to view]

Don't rely on implicit date conversion from strings if you can avoid
it. Here's an alternative:

CREATE FUNCTION dbo.EncodeDate
( @Year SMALLINT,
@Month TINYINT,
@Day TINYINT
)
RETURNS DATETIME
AS
BEGIN;
RETURN
DATEADD(DAY,@Day-1,
DATEADD(MONTH,@Month-1,
DATEADD(YEAR,@Year-2000,'20000101')));
END;
GO

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--
Re: How to encode a date? David Portas
11/25/2006 3:21:18 AM
[quoted text, click to view]

Note that '20000101' is a "safe" date format. It will work irrespective
of the server's regional settings. The same is not true of the format
using / as a separator.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--
RE: How to encode a date? ML
11/25/2006 4:50:01 AM
Perhaps this example could be of help:
http://milambda.blogspot.com/2006/09/datetime-constructor-sql-style.html


ML

---
Re: How to encode a date? Robert Klemme
11/25/2006 9:00:18 AM
[quoted text, click to view]

I believe this approach has some flaws: you do not explicitly use a date
format so interpretation may vary according to locale. Then you use
char as parameter where these should really be integers.

Kind regards

Re: How to encode a date? Arnie Rowland
11/25/2006 11:12:22 AM
Thanks for the clarification David.

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous

You can't help someone get up a hill without getting a little closer to the
top yourself.
- H. Norman Schwarzkopf


[quoted text, click to view]

Re: How to encode a date? JXStern
11/26/2006 11:20:25 AM
On Fri, 24 Nov 2006 18:09:27 -0600, "Stijn Verrept"
[quoted text, click to view]

cast('2006-08-23' as datetime)
AddThis Social Bookmark Button