Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Code Shortening

Author  Topic 

bd528
Starting Member

23 Posts

Posted - 2011-09-05 : 10:54:05
Hello,

A sample of code I am running is below :-

delete from INVOICE where Acct_link = '9061' and invdate = '2011-06-30 00:00:00.000'
or Acct_link = '9062' and invdate = '2011-06-30 00:00:00.000'
or Acct_link = '9063' and invdate = '2011-06-30 00:00:00.000'
or Acct_link = '9064' and invdate = '2011-06-30 00:00:00.000'
or Acct_link = '9065' and invdate = '2011-06-30 00:00:00.000'
or Acct_link = '9066' and invdate = '2011-06-30 00:00:00.000'

Is there a way to shorten this, considering that the date is always the same?

Thanks in advance!

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-09-05 : 11:14:54
looks like your code missed out the parenthesis ( ) around the condition

delete from INVOICE
where (Acct_link = '9061' and invdate = '2011-06-30 00:00:00.000')
or (Acct_link = '9062' and invdate = '2011-06-30 00:00:00.000')
or (Acct_link = '9063' and invdate = '2011-06-30 00:00:00.000')
or (Acct_link = '9064' and invdate = '2011-06-30 00:00:00.000')
or (Acct_link = '9065' and invdate = '2011-06-30 00:00:00.000')
or (Acct_link = '9066' and invdate = '2011-06-30 00:00:00.000')


to simplify your code, you can change to

where invdate = '2011-06-30'
and Acct_link in ('9061' , '9062', '9063', ....)




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

gwilson67
Starting Member

42 Posts

Posted - 2011-09-05 : 11:24:22
Using the IN statement is about as short as it gets.

Greg
http://www.freewebstore.org/tsqlcoderepository
Powerful tool for SQL Server development
Go to Top of Page

bd528
Starting Member

23 Posts

Posted - 2011-09-05 : 11:31:52
Thanks khtan.

Are the parenthesis critical? I've run this code in the past and it seems to have worked...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-09-05 : 11:36:55
quote:
Originally posted by bd528

Thanks khtan.

Are the parenthesis critical? I've run this code in the past and it seems to have worked...



yes. Unless I misunderstood your logic.

In your previous query, as long as the Acct_link is any of the 906x even if the date is not 2011-06-30, the records will still be deleted


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

bd528
Starting Member

23 Posts

Posted - 2011-09-05 : 12:38:58
Excellent. Thanks again for your help.
Go to Top of Page
   

- Advertisement -