Both cast and convert are often used. Specially extracted as an article, convenient to find.
Both cast and convert can perform data type conversions. In most cases, the two perform the same function, but the convert also provides some special date format conversions, and cast does not have this function.
Since convert includes all the functions of cast, and convert has a date conversion, why do I need to use cast? In fact, this is for ansi/iso compatibility. Cast is ANSI-compliant, and convert is not.
Grammar:
[]) [[] ]
Example:
SELECT‘Ab‘+1 --this statement error, converting varchar value ' AB ' to data type int failed. select ab ' ab "+ convert (varchar,1) --output AB1
Both cast and convert can convert time:
CONVERT (DateTime,'2011-07-11--Output 2011-07-11 00:00:00.000 CAST ('2011-07-11--Output 2011-07-11 00:00:00.000
But time-to-string, cast does not convert so many tricks:
SELECTCONVERT (varcharGETDATE (),--output 01-07-13 select convert (varchar, getdate (), --output 2013/07/01 select convert (varchar,getdate (), 1) --output 07/01/13 select cast ( getdate () varchar" Span style= "color: #008000;" >--Output 1 9:56pm
SQL Statement convert Format function cast, convert