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


Date in where clause


Date in where clause

Author
Message
sql_reporting
sql_reporting
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 169
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.
malleswarareddy_m
malleswarareddy_m
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2207 Visits: 1189
Please post the sample data and table scripts

Malleswarareddy
I.T.Analyst
MCITP(70-451)
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26195 Visits: 17539
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 Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
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