2012年2月26日星期日

first date

I wrote 03.20.2005. datetime, and I want from Select to find first date that
is smaller then curent date.
Can I do that from Select?SELECT dt
FROM YourTable
WHERE dt < '20050320'
David Portas
SQL Server MVP
--|||By "first" I take it you mean the earliest
SELECT MIN(dt)
FROM YourTable
WHERE dt < '20050320'
David Portas
SQL Server MVP
--|||This i need to modify:
SELECT MAX(Dat) AS Dat, km
FROM tblZap
GROUP BY ZapID, km
HAVING (ZapID = 1) AND (MAX(Dat) < CONVERT(DATETIME, '2005-03-07
00:00:00', 102))
I need to get km from first date that is smaller then this date:
'2005-03-07.
2005-03-05
2005-03-04
2005-03-03
I need to get 2005-03-05
'

> SELECT MIN(dt)
> FROM YourTable
> WHERE dt < '20050320'
> --
> David Portas
> SQL Server MVP
> --
>|||SELECT DISTINCT dat, km
FROM tblZap
WHERE zapid = 1
AND dat =
(SELECT MAX(dat)
FROM tblZap
WHERE dat < '20050307'
AND zapid = 1)
Note that if you format your date literals without a hyphen then you
won't need to use CONVERT. The string '20050307' will be reliably
converted to a DATETIME.
David Portas
SQL Server MVP
--|||Thanks, this works but I have one more problem. If I have two equal date,
this select gave me two records, but i need only one record.
ID;Date;km
122;2005-03-05;10
123;2005-03-05;27
In this case, I need last one with km = 27. Can I search MAX ID in select
bellow?
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1110809018.766783.256520@.o13g2000cwo.googlegroups.com...
> SELECT DISTINCT dat, km
> FROM tblZap
> WHERE zapid = 1
> AND dat =
> (SELECT MAX(dat)
> FROM tblZap
> WHERE dat < '20050307'
> AND zapid = 1)
> Note that if you format your date literals without a hyphen then you
> won't need to use CONVERT. The string '20050307' will be reliably
> converted to a DATETIME.
> --
> David Portas
> SQL Server MVP
> --
>

没有评论:

发表评论