Tech Tip: SQL function CONVERT
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: January 21, 2009
Convert is a common SQL function that is used to convert an expression from one data type to another. This is similar to the functionality of the Cast function.
Here is an example of how to use the Convert function to extract a Datetime field from a SQL Server 2000 database.
The following code selects a field of type datetime 4 times and uses the Convert function to return the field in 4 different formats.
SQL LOGIN("ODBC Data Source";"username";"password";*) Begin SQL SELECT CONVERT(VARCHAR(40), myDate, 120) AS D1, CONVERT(VARCHAR(40), myDate, 121) AS D2, CONVERT(VARCHAR(40), myDate, 101) AS D3, CONVERT(VARCHAR(40), myDate, 108) AS D4 FROM myTable End SQL SQL LOGOUT |
Here is what the results will look like, given some sample data.
In this example, by just specifying different options within the Convert function call, we are able to return:
- the date
- the time
- the date and time together
- the date and time including milliseconds
Note - The syntax for the Convert function varies depending on which SQL server you are connecting to. Also the data types that you can use in the Convert function may be different. Please consult the documentation for the SQL server you are connecting to before using the Convert function.