I am working on a query in Sql Server 2005 where I need to convert a value in DateTime
variable into a varchar
variable in yyyy-mm-dd
format (without time part). How do I do that?
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- What is the best way to cache a table from a (SQL)
- php PDO::FETCH_ASSOC doesnt detect select after ba
SQL Server 2012 has a new function , FORMAT: http://msdn.microsoft.com/en-us/library/ee634924.aspx
and you can use custom date time format strings: http://msdn.microsoft.com/en-us/library/ee634398.aspx
These pages imply it is also available on SQL2008R2, but I don't have one handy to test if that's the case.
Example usage (Australian datetime):
You don't say what language but I am assuming
C#/.NET
because it has a nativeDateTime
data type. In that case just convert it using theToString
method and use a format specifier such as:However, I would caution against using this in a database query or concatenated into a SQL statement. Databases require a specific formatting string to be used. You are better off zeroing out the time part and using the DateTime as a SQL parameter if that is what you are trying to accomplish.
This is how I do it:
CONVERT(NVARCHAR(10), DATE1, 103) )