Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange syntax in a Where clause

Tags:

sql-server

The following is a simplied version of a query that a reporting tool is sending to our database. I have never seen this syntax before in the Where clause. Can someone tell me what the brackets are doing? And, I assume the 'd' acts as a date cast?

Select
    ch.ContainerID, 
    ch.WorkItemHistoryEventTypeEnumID,
    ch.EventTime,
    ch.ContainerBinName,
    ch.WorkItemSerialNumber,
    ch.Closed
From Wip.vwContainerHistory ch
Where   
   ch.EventTime >= {d '2010-08-09'} 
like image 741
Randy Minder Avatar asked Aug 12 '10 14:08

Randy Minder


People also ask

What is the syntax of WHERE clause?

Basic Syntax of an SQL Query With a WHERE ClauseWHERE <conditions>; The WHERE clause follows the SELECT and the FROM clauses. While the SELECT clause specifies the columns to be returned from the table(s), the WHERE clause contains the conditions that must evaluate to true for a row to be returned as a result.

What is the syntax for WHERE clause in SQL?

SQL WHERE Clause Syntax You write the WHERE clause like this: SELECT column1, column2... FROM table_name WHERE condition; Note that here I've written it using the SELECT statement, but its use is not limited to SELECT .

Which conditions can we use with WHERE clause?

The SQL WHERE clause is used to specify a condition while fetching the data from a single table or by joining with multiple tables. If the given condition is satisfied, then only it returns a specific value from the table. You should use the WHERE clause to filter the records and fetching only the necessary records.


2 Answers

See "Supported String Literal Formats for datetime" section in MSDN datetime article.

Your {d 'XXXX-XX-XX'} is ODBC datetime format. ODBC timestamp escape sequences are of the format: { literal_type 'constant_value' }:

literal_type specifies the type of the escape sequence. Timestamps have three literal_type specifiers:

  • d = date only
  • t = time only
  • ts = timestamp (time + date)

'constant_value' is the value of the escape sequence. constant_value must follow these formats for each literal_type.

d > yyyy-mm-dd  
t > hh:mm:ss[.fff]  
ts > yyyy-mm-dd hh:mm:ss[.fff]
like image 92
Pavel Morshenyuk Avatar answered Nov 15 '22 04:11

Pavel Morshenyuk


This is an ODBC escape sequence for a date type. See http://msdn.microsoft.com/en-us/library/ms187819.aspx

  • d = date only
  • t = time only
  • ts = timestamp (time + date)
like image 37
Joe Stefanelli Avatar answered Nov 15 '22 05:11

Joe Stefanelli