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


Syntax error related to "WHERE"


Syntax error related to "WHERE"

Author
Message
nazaninahmady_sh
nazaninahmady_sh
SSC Veteran
SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)

Group: General Forum Members
Points: 295 Visits: 952
Hi
I have a store procedure, it works fine, but I want to make a change in it, I want to have only the records for last month( 30 days ago), when I add this condition in WHERE a syntax error occure , would you please help me?



ALTER PROCEDURE [dbo].[NewsTimeBroadCastTopicProc]
@VarChannel_ID int
AS
BEGIN
select NewsTimeBroadCast_ID, NewsTimeBroadCast.NewsTime_ID, Movie_URL, DateTimeSystem,LEFT(DatePakhsh,10) as DatePakhsh, DownloadCount,NewsTime.NewsTime_ID
,[Channel_ID]
,[TehranTime]
,[Name]
,[VienTime]
,[LondonTime]
,[NewyorkTime]
,[LosanjlesTime]
,[SidneyTime]
,[KoalalampoorTime]
,[EstabnulTime]
,[CentralAmericaTime]
,[TokyoTime]
,[DehliTime]
,[Record_Time]
from NewsTimeBroadCast inner join NewsTime
on NewsTimeBroadCast.NewsTime_ID=NewsTime.NewsTime_ID
where (( NewsTimeBroadCast.DateTimeSystem,-30, getdate()) AND ( NewsTime.Channel_ID=@VarChannel_ID order by NewsTimeBroadCast.DatePakhsh desc))


END
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97705 Visits: 33014
You've got the function in place, but it's not comparing it to anything. That's why you're getting an error. Assuming you want exactly 30 days ago, you can do this;
SELECT...
FROM...
WHERE NewsTimeBroadCast.DateTimeSystem = dateadd('d',-30,getdate())



But if you want all the entries from the last 30 days, do something like this:

SELECT...
FROM...
WHERE NewsTimeBroadCast.DateTimeSystem > dateadd('d',-30,getdate())



Where possible, avoid running functions against columns because that will prevent index use and lead to scans.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
nazaninahmady_sh
nazaninahmady_sh
SSC Veteran
SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)

Group: General Forum Members
Points: 295 Visits: 952
Thank you but again there is a error "invalid parameter 1 specified for dateadd!"
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97705 Visits: 33014
Oops. My bad. I typed it from memory. No quotes around the d. You can look up more about DATEADD here.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
nazaninahmady_sh
nazaninahmady_sh
SSC Veteran
SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)

Group: General Forum Members
Points: 295 Visits: 952
Thanks alot
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62055 Visits: 17954
I prefer to type the name of the datepart. It really helps avoid confusion and you don't have to resort to the documentation very often to figure out some of the lesser used dateparts.


dateadd(day, -30, getdate())



_______________________________________________________________

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