all groups > sql server (microsoft) > november 2005 >
You're in the

sql server (microsoft)

group:

Composing a date from date parts


Re: Composing a date from date parts Uri Dimant
11/23/2005 12:00:00 AM
sql server (microsoft):
Or for fun


DECLARE @y INT,@m INT,@d INT
SET @y=2005
SET @m=2
SET @d=27
select cast(rtrim(@y*10000+@m*100+@d) as datetime)



"Tibor Karaszi" <tibor_please.no.email_karaszi@hotmail.nomail.com> wrote in
message news:uK5YPUC8FHA.3880@TK2MSFTNGP12.phx.gbl...
[quoted text, click to view]

Composing a date from date parts aroraamit81 NO[at]SPAM gmail.com
11/23/2005 3:44:37 AM
I have three date parts namely,

Year
Month
Date/Day

as integer values stored in one column each in a table in a SQL Server
2000 database. I need a function to serialize/compose/create a datetime
type out of them so I could use that in a query (pseudosyntax) as
below:


SELECT CreateDate(iYear, iMonth, iDay) As TheDateIWant FROM ThatTable


and then later on, I probably want to use an aggregation/computation on
that like, the MAX function, may be:


SELECT MAX(CreateDate(iYear, iMonth, iDay)) As TheDateIWant FROM
ThatTable


Thanks!
Re: Composing a date from date parts aroraamit81 NO[at]SPAM gmail.com
11/23/2005 4:41:40 AM
Thanks a tonne, mate.
Re: Composing a date from date parts Raymond D'Anjou
11/23/2005 9:15:27 AM
"Tibor Karaszi" <tibor_please.no.email_karaszi@hotmail.nomail.com> wrote in
message news:uK5YPUC8FHA.3880@TK2MSFTNGP12.phx.gbl...
[quoted text, click to view]

....or
select dateadd(d, @day-1, dateadd(m, @month-1, dateadd(yyyy, @year % 1900,
'19000101')))

Re: Composing a date from date parts Tibor Karaszi
11/23/2005 12:57:18 PM
DECLARE @Year int
DECLARE @Month int
DECLARE @Day int

SET @Year = 2005
SET @Month = 02
SET @Day = 27

SELECT
CAST(
CAST(@Year AS char(4))
+RIGHT('0' + CAST(@Month AS varchar(2)), 2)
+RIGHT('0' + CAST(@Day AS varchar(2)), 2)
AS datetime)

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


[quoted text, click to view]

AddThis Social Bookmark Button