[英]Convert TEXT to Date in SQL Server 2012
I have a TEXT in this format 31/10/15
. 我有一个格式为
31/10/15
的TEXT。
How do I convert this into a DATE format? 如何将其转换为DATE格式?
As I need to let the user search from data using a date range. 因为我需要让用户使用日期范围从数据中搜索。
example: From 15/7/13
to 31/10/15
例如:从
15/7/13
到31/10/15
Or is there a way to so without converting to date? 还是有一种方法可以不转换日期呢?
You can use CONVERT()
for this: 您可以为此使用
CONVERT()
:
DECLARE @d VARCHAR(50) = '31/10/50'
SELECT CONVERT(DATE, @d,3)
Note that with a 2-digit year SQL Server will make the year start with '19' for 50 and up, and 49 and below will be '20' 请注意,如果使用两位数的年份,则SQL Server将以“ 19”开头的年份开始等于或等于50,大于等于49的年份则等于“ 20”。
Storing as a DATE
field will allow easier comparisons, otherwise you'll have to perform this conversion at each step. 将其存储为
DATE
字段将使比较更加容易,否则,您将必须在每个步骤中执行此转换。
Use CONVERT
; 使用
CONVERT
; example: 例:
SELECT [Date] = CONVERT(date, '31/10/15', 3);
And yes, it's possible to search dates in the same format as the examples you provide, but don't do that – use the proper data types in both your queries and your table columns. 是的,可以使用与您提供的示例相同的格式来搜索日期,但不要这样做–在查询和表列中都使用正确的数据类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.