question

maurya avatar image
maurya asked

in sql, why date is written in single quotes rather varchar data type is written in single quotes?

set doj='06-jan2015' here date is written in single quotes bt we write varchar data type in single quotes, so can we say that date is also varchar date type?
date
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

nidheesh.r.pillai avatar image nidheesh.r.pillai commented ·
Single quotes delimit a string constant or a date/time constant. It does not mean that DATE is VARCHAR. See [String Literal Date and Time Formats][1] for more information. [1]: https://msdn.microsoft.com/en-us/library/ms180878(v=sql.105).aspx#UsingDateandTimeFormats
5 Likes 5 ·
Alvin Ramard avatar image
Alvin Ramard answered
Why? Because Microsoft decided the same delimiter would be used for both. Can we say date is also varchar type? No. Just because they use the same delimiter does not make them the same data type.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Tom Staab avatar image
Tom Staab answered
I assume your variable is one of the date/time data types like this: DECLARE @mydate date; SET @mydate = '06-jan-2015'; In that example, the assignment is implicitly converting the string literal to a date. I added the second dash because I got an error without it since the string was not in a recognized date format. The date is not stored as a string. The explicit way to write this would be: SET @mydate = CAST('06-jan-2015' AS date); If you want to specify the date string format, use CONVERT. There are various style options to use for the 3rd parameter, but here is 1 example: SET @mydate = CONVERT(date, 'Jan 06, 2015', 107);
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.