2012年的SQL查询工作,2005年没有

时间:2014-11-10 19:22:38

标签: sql sql-server sql-server-2005 sql-server-2012

我有以下查询,我需要帮助将其转换为MS SQL 2005可以理解的语法,因为旧版本不支持 FORMAT 等内容。

"SELECT " & _
     "TMP.*," & _
     "COUNT(*) OVER () AS rCount " & _
"FROM (" & _
     "SELECT venueID, " & _
         "venueName AS venueName, " & _
         "venueAddress + ', ' + venueCity + ', ' + venueState + ' ' + venueZip AS venueAddress, " & _
         "venueLatLong AS coordinates, " & _
         "FORMAT(venueEventDate, 'MM/dd/yyyy', 'en-US') + ' @ ' + CONVERT(VARCHAR,venueTime) AS dateAndTime, " & _
         "SUBSTRING(venueLatLong, 1, CHARINDEX(',', venueLatLong)-1) AS Lat, " & _
         "SUBSTRING(venueLatLong, CHARINDEX(',', venueLatLong) + 1, 1000) AS Lng, " & _
         "(round(" & _
             "3959 * acos " & _
               "(" & _
                   "cos(radians('" & center_lat & "')) " & _
                   "* cos(radians(SUBSTRING(venueLatLong, 1, CHARINDEX(',', venueLatLong)-1))) " & _
                   "* cos(radians(SUBSTRING(venueLatLong, CHARINDEX(',', venueLatLong) + 1, 1000)) " & _
                   "- radians('" & center_lng & "')) " & _
                   "+ sin(radians('" & center_lat & "')) " & _
                   "* sin(radians(SUBSTRING(venueLatLong, 1, CHARINDEX(',', venueLatLong)-1)))" & _
               ")" & _
        ", 1, 1)) AS distance " & _
        "FROM meetUpMarkers) " & _
     "TMP " & _
"WHERE distance < " & radius & " " & _
"ORDER BY venueName,distance DESC;"

我尝试用 CONVERT 替换 FORMAT ,但它似乎仍然不正确。

当我将 FORMAT 更改为 CONVERT 时,我收到错误消息:

  

类型venueEventDate不是已定义的系统类型。

更新

Uggg ...现在,当我在本地运行时,我得到了这个:

现在即使我在本地运行它也会收到此错误???!??!和它的2012年:

  

Msg 8114,Level 16,State 5,Line 2   将数据类型varchar转换为float时出错。

这是否意味着第2行,第5行有错误?

非常感谢帮助。

1 个答案:

答案 0 :(得分:1)

FORMAT()CONVERT()的参数顺序不同。错误消息表明您将字段名称放入数据类型参数位置。它&#39; S:

FORMAT ( value, format [, culture ] )

VS

CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

尝试用此替换FORMAT(venueEventDate, 'MM/dd/yyyy', 'en-US')

CONVERT(VARCHAR, venueEventDate, 101)`