CONVERT is a scalar function that returns the input expression translated to a specified data type. In SQL Server it behaves like CAST but also accepts an optional style argument that controls the textual representation of date-time and monetary values. When length is supplied, character and binary types are truncated or padded to that size. CONVERT obeys implicit-conversion rules first, then applies explicit conversion; if the target type is incompatible, the statement fails with error 245. Using style 0-130 formats dates per ISO, US, or regional standards, while 0 keeps default session format. Unlike CAST, CONVERT can be used inside SELECT, WHERE, ORDER BY, INSERT, UPDATE, and procedural code. It is deterministic except when style affects string output. Large object types (TEXT, NTEXT, IMAGE) are deprecated; convert them to varchar(max), nvarchar(max), or varbinary(max).
target_data_type
(data type) - Required. The desired data type of the result.length
(integer) - Optional. Size for char, nchar, varchar, nvarchar, binary, or varbinary.expression
(any) - Required. The value to convert.style
(integer) - Optional. Date/time or float style code (0-130).CAST, TRY_CONVERT, TRY_CAST, PARSE, FORMAT, ISNULL
Sybase SQL Server 4.x (carried into Microsoft SQL Server 6.0)
CONVERT includes a style parameter for formatting output, while CAST follows the ANSI standard and provides no formatting options.
Use TRY_CONVERT when the input may not convert cleanly. TRY_CONVERT returns NULL instead of throwing an error, keeping your query running.
Call CONVERT with style 126 or 127:SELECT CONVERT(varchar(23), GETDATE(), 126);
Yes, but Microsoft discourages it because those types are deprecated. Convert them to varchar(max) or nvarchar(max) for future-proof code.