April 11, 2012 at 5:18 am
Hi all,
CREATE TABLE TESTTABLE(VersionId int, EffectiveDate DateTime)
There are no records in this table currently and I want the below variable @test-2 to return getdate() if there is no version whose id is 2167.
How can I acheive this. The below sql is not helping.
declare @test-2 datetime
SELECT @test-2 = ISNULL(TT.EffectiveDate, GETDATE())
FROM testtable AS TT
WHERE TT.VersionID = 2167
select @test-2
Any help is really appreciated.
Thanks,
April 11, 2012 at 5:27 am
Plenty of ways. . .
Here's one: -
DECLARE @test-2 DATETIME;
SELECT @test-2 = ISNULL(b.EffectiveDate,a.EffectiveDate)
FROM (VALUES (GETDATE())) a(EffectiveDate)
OUTER APPLY (SELECT *
FROM testtable
WHERE VersionID = 2167) b;
SELECT @test-2;
And another: -
DECLARE @test-2 DATETIME;
SELECT @test-2 = EffectiveDate
FROM testtable
WHERE VersionID = 2167;
SET @test-2 = ISNULL(@test,GETDATE());
SELECT @test-2;
April 11, 2012 at 5:32 am
Thanks Suresh. That helped
April 12, 2012 at 1:33 am
Agreed. Many ways. Here's another.
DECLARE @TESTTABLE TABLE (VersionId int, EffectiveDate DateTime)
declare @test-2 datetime
SET @test-2 = ISNULL((
SELECT TOP 1 TT.EffectiveDate
FROM @testtable AS TT
WHERE TT.VersionID = 2167), GETDATE())
select @test-2
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply