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
Either
Cast
orConvert
:Syntax for
CAST
:Syntax for
CONVERT
:http://msdn.microsoft.com/en-us/library/ms187928.aspx
Actually since you asked for a specific format:
Try:
More on ms sql tips
You did not say which database, but with mysql here is an easy way to get a date from a timestamp (and the varchar type conversion should happen automatically):
For SQL Server 2008+ You can use CONVERT and FORMAT together.
For example, for European style (e.g. Germany) timestamp:
Here's some test sql for all the styles.
Here's the result
Make
nvarchar(max)
shorter to trim the time. For example:outputs:
You can convert your date in many formats, the syntaxe is simple to use :
In your case, i've just converted and restrict size by nvarchar(10) like this :
See more at : http://www.w3schools.com/sql/func_convert.asp
Another solution (if your date is a Datetime) is a simple CAST :