SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TRICKY SQL QUERY


TRICKY SQL QUERY

Author
Message
martin.edward
martin.edward
SSC Veteran
SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)

Group: General Forum Members
Points: 265 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,
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91030 Visits: 38945
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.



Cool
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)
ChrisM@Work
ChrisM@Work
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39731 Visits: 19995
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
Garadin
Garadin
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6695 Visits: 4107
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
martin.edward
martin.edward
SSC Veteran
SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)SSC Veteran (265 reputation)

Group: General Forum Members
Points: 265 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 ?
Garadin
Garadin
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6695 Visits: 4107
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
ChrisM@Work
ChrisM@Work
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39731 Visits: 19995
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
Garadin
Garadin
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6695 Visits: 4107
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search