views:

13

answers:

2

How do I make this work in SQL Server (2005)? The error message is 'Invalid column name' on the literals.

SELECT tstamp, GVEA_Load_Net_MWH 
  FROM DA.dbo.Oplog
 WHERE CAST(LEFT(tstamp,18) AS datetime) BETWEEN "01/01/2009 00:00:00" 
                                             AND "01/01/2010 00:00:00"
+3  A: 

Change the double quotes to single.

RedFilter
Thanks, it's always something simple. I'll accept when the timer runs out.
Lance Roberts
+2  A: 

Like this for your example

SELECT tstamp, GVEA_Load_Net_MWH FROM DA.dbo.Oplog
WHERE CAST(LEFT(tstamp,18) AS datetime) BETWEEN '20090101' AND '20100101'

Notice the ISO safe and language safe YYYYMMDD format? Keep in mind that this query is not SARGable and will not use an index

Also take a look How Does Between Work With Dates In SQL Server? to understand why between might sometimes not give you the results you epect

SQLMenace
Thanks, will the dates in that format come with an implicit 00:00:00, and be able to handle all the times inbetween?
Lance Roberts
yes, see examples at link
SQLMenace
yep, good article.
Lance Roberts