开发者

Between Date Literals

开发者 https://www.devze.com 2023-01-12 13:02 出处:网络
How do I make this work in SQL Server (2005)? The error message is \'Invalid column name\' on the literals.

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)开发者_如何学Go AS datetime) BETWEEN "01/01/2009 00:00:00" 
                                             AND "01/01/2010 00:00:00"


Change the double quotes to single.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号