Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
T-SQL (SS2K8)
»
Date in where clause
Date in where clause
Rate Topic
Display Mode
Topic Options
Author
Message
sql_reporting
sql_reporting
Posted Wednesday, October 24, 2012 7:26 AM
SSC Rookie
Group: General Forum Members
Last Login: Wednesday, May 01, 2013 2:42 PM
Points: 25,
Visits: 126
Hi I have something like this and the tables have no foreign keys. I am using date in where clause and i am not able to use id because it is deleting al the data.
DELETE t1 FROM table1 t1
INNER JOIN table2 t2 ON
t1.id= t2.id
and t1.code= t2.code
inner join table3 t3 on
t2.id=t3.id
t2.code=t3.code
WHERE t3.date IN
(
SELECT [DATE] FROM t3
WHERE CNTYCODE IN('A', 'B', 'BA', 'G', 'N')
AND [DATE] < '01/14/2010']
UNION ALL
-- Region-I
SELECT [DATE] FROM t3
WHERE CNTYCODE IN('c', 'd', 'e', 'f', 'k')
AND [DATE] < '01/14/2010']
)
can i use this way or no.
Post #1376461
malleswarareddy_m
malleswarareddy_m
Posted Wednesday, October 24, 2012 7:42 AM
SSCommitted
Group: General Forum Members
Last Login: Today @ 12:06 AM
Points: 1,846,
Visits: 974
Please post the sample data and table scripts
Malleswarareddy
I.T.Analyst
MCITP(70-451)
Post #1376477
Sean Lange
Sean Lange
Posted Wednesday, October 24, 2012 7:54 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 2:45 PM
Points: 8,567,
Visits: 8,218
raghuveer126 (10/24/2012)
Hi I have something like this and the tables have no foreign keys. I am using date in where clause and i am not able to use id because it is deleting al the data.
DELETE t1 FROM table1 t1
INNER JOIN table2 t2 ON
t1.id= t2.id
and t1.code= t2.code
inner join table3 t3 on
t2.id=t3.id
t2.code=t3.code
WHERE t3.date IN
(
SELECT [DATE] FROM t3
WHERE CNTYCODE IN('A', 'B', 'BA', 'G', 'N')
AND [DATE] < '01/14/2010']
UNION ALL
-- Region-I
SELECT [DATE] FROM t3
WHERE CNTYCODE IN('c', 'd', 'e', 'f', 'k')
AND [DATE] < '01/14/2010']
)
can i use this way or no.
Syntactically I don't see anything wrong but I highly doubt the logic is correct. You where clause is way more complicated than it needs to be. This should be the same thing.
WHERE t3.date > '01/14/2010'
and t3.CNTYCODE IN('A', 'B', 'BA', 'G', 'N', 'c', 'd', 'e', 'f', 'k')
The real problem is that you will delete anything from table1 that has a date greater than 1/14/2010 which I doubt is what you really want.
If you want some real help you need to post ddl, sample data and an explanation of what you want to happen. Take a look at the first link in my signature.
_______________________________________________________________
Need help? Help us help you.
Read the article at
http://www.sqlservercentral.com/articles/Best+Practices/61537/
for best practices on asking questions.
Need to split a string? Try Jeff Moden's
splitter
.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1376493
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.