running :
SELECT CONVERT(VARCHAR(20), GETDATE(), 100)
will result :
Jan 4 2012 1:25PM
Ive been knowing this for years.
However , yesterday, while I was driving , I thought to my self :
Hey! I gave him the format of 103 which is for datetime format ,
But I didnt actually TOLD the convert that Im passing also a datetime
object ! ( e.g. getdate()))
So ive tried :
SELECT CONVERT(VARCHAR(20), 'lalala', 100)
And the result was :
lalala
so , now Im trying to convert ‘lalala’ to string via datetime format (103).
which is ( by logic) should give me exception.
but it is not.
Any reasonable explanation ?
CONVERTtakes into account both the target type and the expression’s type to see whether the third argument should be considered. If you are converting a datetime to a string or the other way round, the third argument is considered. In your example, a string is converted to a string and so the third argument is ignored.Consider this:
Here are the results:
As you can see, the last two ‘conversions’ didn’t change anything, because both the source and the target type were
varchar, even though the expression did look like adate/datetime.Reference: