Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

TRICKY SQL QUERY Expand / Collapse
Author
Message
Posted Wednesday, March 3, 2010 6:42 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, April 11, 2011 12:49 AM
Points: 69, Visits: 179

Dear Experts ,

I would like to do the following :-

get the date out of a certain column in a table

select docdate from tablename

Check if the docdate is greater than todays date, if so display it

end.

I have tried using a while loop but it fails. It just picks one date only instead of all the dates in the table. This does not work

WHILE (SELECT docdate FROM tablename) <> ''

BEGIN
SELECT @BATCH=docdate FROM tablename
END




Kind Regards,
Post #875919
Posted Wednesday, March 3, 2010 6:49 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:34 AM
Points: 20,857, Visits: 32,871
martin.edward (3/3/2010)

Dear Experts ,

I would like to do the following :-

get the date out of a certain column in a table

select docdate from tablename

Check if the docdate is greater than todays date, if so display it

end.

I have tried using a while loop but it fails. It just picks one date only instead of all the dates in the table. This does not work

WHILE (SELECT docdate FROM tablename) <> ''

BEGIN
SELECT @BATCH=docdate FROM tablename
END





Kind Regards,


Try this:

select
docdate
from
tablename
where
docdate > getdate() -- includes time info
-- docdate > dateadd(dd, datediff(dd, 0, getdaate()), 0) -- drops time portion from consideration, use one or the other.




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #875928
Posted Wednesday, March 3, 2010 8:14 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 2:14 AM
Points: 6,890, Visits: 14,253
Hi Martin

Please don't take this the wrong way - we all have to learn somewhere. The query you posted is about as simple as it can get. Buy yourself this book and save yourself a lot of time. It's about ten quid in pounds sterling. Don't be put off by the name, it's one of a series of excellent startup guides, well written and researched.

Good luck!

ChrisM


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #876004
Posted Wednesday, March 3, 2010 9:46 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 1:20 PM
Points: 1,519, Visits: 4,085
I haven't read the one Chris mentioned, but I would recommend these:

http://www.amazon.co.uk/Microsoft-Server-T-SQL-Fundamentals-PRO-Developer/dp/0735626014/ref=pd_sim_b_7
http://www.amazon.co.uk/Inside-Microsoft-SQL-Server-2008/dp/0735626030/ref=pd_sim_b_3
http://www.amazon.co.uk/Inside-Microsoft-Server-2008-Pro-Developer/dp/0735626022/ref=pd_sim_b_3

That said, all of the books we just linked target SQL Server 2008. Although the fundamentals books will have good info applicable to all versions, many of the functions that people take for granted nowadays were not available in SQL 2000.

Windowed Functions, CTE's, Cross Apply, FOR XML Path... (the list goes on)... 2008 folks are spoiled.


Seth Phelabaum
Consistency is only a virtue if you're not a screwup.

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
Post #876114
Posted Friday, June 11, 2010 1:58 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, April 11, 2011 12:49 AM
Points: 69, Visits: 179


Thanks,

Am not sure which book you are suggesting : Is it understanding and using APPLY 1 ? Could you suggest where I could purchase it from ?
Post #935864
Posted Monday, June 14, 2010 3:56 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 1:20 PM
Points: 1,519, Visits: 4,085
Martin,

Chris was suggesting this book:

http://www.amazon.co.uk/Microsoft-SQL-Server-2008-Dummies/dp/0470224657 You can pick it up online. You can also get all of the books I listed on Amazon.

The Understanding and using APPLY (I) link is part of Chris' signature and not directly related to your question.


Seth Phelabaum
Consistency is only a virtue if you're not a screwup.

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
Post #937168
Posted Tuesday, June 15, 2010 2:27 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 2:14 AM
Points: 6,890, Visits: 14,253
Garadin (6/14/2010)
Martin,

Chris was suggesting this book:

http://www.amazon.co.uk/Microsoft-SQL-Server-2008-Dummies/dp/0470224657 You can pick it up online. You can also get all of the books I listed on Amazon.

The Understanding and using APPLY (I) link is part of Chris' signature and not directly related to your question.


Many thanks Seth, I missed this one yesterday.
Must admit I recommended the SQL for Dummies book without reading all of it, but it looks pretty good. The Excel, Statistics and Word equivalents have helped my better half no end.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #937311
Posted Wednesday, June 16, 2010 5:31 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 1:20 PM
Points: 1,519, Visits: 4,085
Anytime sir =)

Seth Phelabaum
Consistency is only a virtue if you're not a screwup.

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
Post #938596
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse