如何将DateTime转换为VarChar

时间:2008-09-16 16:44:22

标签: sql sql-server datetime sql-convert

我正在使用Sql Server 2005中的查询,我需要将DateTime变量中的值转换为varchar格式的yyyy-mm-dd变量(没有时间部分)。我该怎么做?

24 个答案:

答案 0 :(得分:355)

这是所有样式的测试sql。

DECLARE @now datetime
SET @now = GETDATE()
select convert(nvarchar(MAX), @now, 0) as output, 0 as style 
union select convert(nvarchar(MAX), @now, 1), 1
union select convert(nvarchar(MAX), @now, 2), 2
union select convert(nvarchar(MAX), @now, 3), 3
union select convert(nvarchar(MAX), @now, 4), 4
union select convert(nvarchar(MAX), @now, 5), 5
union select convert(nvarchar(MAX), @now, 6), 6
union select convert(nvarchar(MAX), @now, 7), 7
union select convert(nvarchar(MAX), @now, 8), 8
union select convert(nvarchar(MAX), @now, 9), 9
union select convert(nvarchar(MAX), @now, 10), 10
union select convert(nvarchar(MAX), @now, 11), 11
union select convert(nvarchar(MAX), @now, 12), 12
union select convert(nvarchar(MAX), @now, 13), 13
union select convert(nvarchar(MAX), @now, 14), 14
--15 to 19 not valid
union select convert(nvarchar(MAX), @now, 20), 20
union select convert(nvarchar(MAX), @now, 21), 21
union select convert(nvarchar(MAX), @now, 22), 22
union select convert(nvarchar(MAX), @now, 23), 23
union select convert(nvarchar(MAX), @now, 24), 24
union select convert(nvarchar(MAX), @now, 25), 25
--26 to 99 not valid
union select convert(nvarchar(MAX), @now, 100), 100
union select convert(nvarchar(MAX), @now, 101), 101
union select convert(nvarchar(MAX), @now, 102), 102
union select convert(nvarchar(MAX), @now, 103), 103
union select convert(nvarchar(MAX), @now, 104), 104
union select convert(nvarchar(MAX), @now, 105), 105
union select convert(nvarchar(MAX), @now, 106), 106
union select convert(nvarchar(MAX), @now, 107), 107
union select convert(nvarchar(MAX), @now, 108), 108
union select convert(nvarchar(MAX), @now, 109), 109
union select convert(nvarchar(MAX), @now, 110), 110
union select convert(nvarchar(MAX), @now, 111), 111
union select convert(nvarchar(MAX), @now, 112), 112
union select convert(nvarchar(MAX), @now, 113), 113
union select convert(nvarchar(MAX), @now, 114), 114
union select convert(nvarchar(MAX), @now, 120), 120
union select convert(nvarchar(MAX), @now, 121), 121
--122 to 125 not valid
union select convert(nvarchar(MAX), @now, 126), 126
union select convert(nvarchar(MAX), @now, 127), 127
--128, 129 not valid
union select convert(nvarchar(MAX), @now, 130), 130
union select convert(nvarchar(MAX), @now, 131), 131
--132 not valid
order BY style

这是结果

output                   style
Apr 28 2014  9:31AM          0
04/28/14                     1
14.04.28                     2
28/04/14                     3
28.04.14                     4
28-04-14                     5
28 Apr 14                    6
Apr 28, 14                   7
09:31:28                     8
Apr 28 2014  9:31:28:580AM   9
04-28-14                     10
14/04/28                     11
140428                       12
28 Apr 2014 09:31:28:580     13
09:31:28:580                 14
2014-04-28 09:31:28          20
2014-04-28 09:31:28.580      21
04/28/14  9:31:28 AM         22
2014-04-28                   23
09:31:28                     24
2014-04-28 09:31:28.580      25
Apr 28 2014  9:31AM          100
04/28/2014                   101
2014.04.28                   102
28/04/2014                   103
28.04.2014                   104
28-04-2014                   105
28 Apr 2014                  106
Apr 28, 2014                 107
09:31:28                     108
Apr 28 2014  9:31:28:580AM   109
04-28-2014                   110
2014/04/28                   111
20140428                     112
28 Apr 2014 09:31:28:580     113
09:31:28:580                 114
2014-04-28 09:31:28          120
2014-04-28 09:31:28.580      121
2014-04-28T09:31:28.580      126
2014-04-28T09:31:28.580      127
28 جمادى الثانية 1435  9:31:28:580AM    130
28/06/1435  9:31:28:580AM    131

缩短nvarchar(max)以缩短时间。例如:

select convert(nvarchar(11), GETDATE(), 0)
union select convert(nvarchar(max), GETDATE(), 0)

输出:

May 18 2018
May 18 2018  9:57AM

答案 1 :(得分:250)

使用Microsoft Sql Server:

--
-- Create test case
--
DECLARE @myDateTime DATETIME
SET @myDateTime = '2008-05-03'

--
-- Convert string
--
SELECT LEFT(CONVERT(VARCHAR, @myDateTime, 120), 10)

答案 2 :(得分:182)

尝试以下方法:

CONVERT(varchar(10), [MyDateTimecolumn], 20)

对于完整的约会时间而不仅仅是日期:

CONVERT(varchar(23), [MyDateTimecolumn], 121)

请参阅此页面了解转换样式:

http://msdn.microsoft.com/en-us/library/ms187928.aspx
OR
SQL Server CONVERT() Function

答案 3 :(得分:31)

SQL Server 2012有一个新功能FORMAT: http://msdn.microsoft.com/en-us/library/ee634924.aspx

您可以使用自定义日期时间格式字符串:http://msdn.microsoft.com/en-us/library/ee634398.aspx

这些页面暗示它也可以在SQL2008R2上使用,但我没有一个方便测试是否是这种情况。

使用示例(澳大利亚日期时间):

FORMAT(VALUE,'dd/MM/yyyy h:mm:ss tt')

答案 4 :(得分:9)

CastConvert

CAST的语法:

CAST ( expression AS data_type [ (length ) ])

CONVERT的语法:

CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

http://msdn.microsoft.com/en-us/library/ms187928.aspx

实际上,因为您要求特定的格式:

REPLACE(CONVERT(varchar(10), Date, 102), '.', '-')

答案 5 :(得分:9)

您可以使用DATEPART(DATEPART, VARIABLE)。例如:

DECLARE @DAY INT 
DECLARE @MONTH INT
DECLARE @YEAR INT
DECLARE @DATE DATETIME
@DATE = GETDATE()
SELECT @DAY = DATEPART(DAY,@DATE)
SELECT @MONTH = DATEPART(MONTH,@DATE)
SELECT @YEAR = DATEPART(YEAR,@DATE)

答案 6 :(得分:6)

- 这样的格式为'yyyy-mm-dd 00:00:00.000'


SELECT CAST( CONVERT(VARCHAR, GETDATE(), 101) AS DATETIME) ; 

答案 7 :(得分:5)

使用Microsoft SQL Server:

对CONVERT使用语法:

CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

示例:

SELECT CONVERT(varchar,d.dateValue,1-9)

对于样式,您可以在此处找到更多信息:MSDN - Cast and Convert (Transact-SQL)

答案 8 :(得分:3)

尝试以下方法:

CONVERT(VARCHAR(10),GetDate(),102)

然后你需要替换“。”用“ - ”。

这是一个有帮助的网站 http://www.mssqltips.com/tip.asp?tip=1145

答案 9 :(得分:3)

declare @dt datetime

set @dt = getdate()

select convert(char(10),@dt,120) 

我想要一个特定的字符串格式,我有char(10)的固定数据长度。

答案 10 :(得分:3)

尝试:

select replace(convert(varchar, getdate(), 111),'/','-');

更多关于ms sql tips

答案 11 :(得分:2)

我就是这样做的:CONVERT(NVARCHAR(10), DATE1, 103) )

答案 12 :(得分:2)

对于SQL Server 2008+,您可以同时使用CONVERT和FORMAT。

例如,对于欧洲风格的时间戳(例如德国):

CONVERT(VARCHAR, FORMAT(GETDATE(), 'dd.MM.yyyy HH:mm:ss', 'de-DE'))

答案 13 :(得分:2)

试试这个SQL:

select REPLACE(CONVERT(VARCHAR(24),GETDATE(),103),'/','_') + '_'+ 
       REPLACE(CONVERT(VARCHAR(24),GETDATE(),114),':','_')

答案 14 :(得分:2)

您可以使用多种格式转换日期,语法很简单:

map2(x, y, map2, `+`)

#[[1]]
#[[1]][[1]]
#[1] 11

#[[1]][[2]]
#[1] 22


#[[2]]
#[[2]][[1]]
#[1] 33

#[[2]][[2]]
#[1] 44
  • 代码是一个整数,这里3是没有世纪的第三个格式,如果你想要世纪只需将代码更改为103.

在你的情况下,我刚刚通过nvarchar(10)转换并限制大小:

CONVERT('TheTypeYouWant', 'TheDateToConvert', 'TheCodeForFormating' * )
CONVERT(NVARCHAR(10), DATE_OF_DAY, 103) => 15/09/2016

详情请见:http://www.w3schools.com/sql/func_convert.asp

另一种解决方案(如果您的日期是日期时间)是一个简单的 CAST

CONVERT(NVARCHAR(10), MY_DATE_TIME, 120) => 2016-09-15

答案 15 :(得分:2)

OP提到了 datetime 格式。对我来说,时间部分妨碍了我们 我认为在格式化之前删除时间部分(通过将日期时间转换为日期)会更清晰。

convert( varchar(10), convert( date, @yourDate ) , 111 )

答案 16 :(得分:1)

最短也是最简单的方法是:

DECLARE @now AS DATETIME = GETDATE()

SELECT CONVERT(VARCHAR, @now, 23)

答案 17 :(得分:1)

DECLARE @DateTime DATETIME
SET @DateTime = '2018-11-23 10:03:23'
SELECT CONVERT(VARCHAR(100),@DateTime,121 )

答案 18 :(得分:1)

您没有说明哪个数据库,但是使用mysql这是一种从时间戳获取日期的简单方法(并且varchar类型转换应该自动发生):

mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2008-09-16  | 
+-------------+
1 row in set (0.00 sec)

答案 19 :(得分:0)

CONVERT(VARCHAR, GETDATE(), 23)

答案 20 :(得分:0)

编写函数

/^#/d             # delete all lines starting with #
/sqlplus -s/d     # delete all lines that contain 'sqlplus -s'

答案 21 :(得分:0)

select REPLACE(CONVERT(VARCHAR, FORMAT(GETDATE(), N'dd/MM/yyyy hh:mm:ss tt')),'.', '/')

结果将给出05/05/2020 10:41:05 AM

答案 22 :(得分:0)

简单地使用“转换”然后使用“格式”来获得您想要的日期格式

df <- structure(list(time = c("2021-04-02 23:40:20", "2021-04-02 23:41:15", 
"2021-04-02 23:42:10", "2021-04-02 23:43:05", "2021-04-02 23:44:55", 
"2021-04-02 23:45:50", "2021-04-02 23:46:45", "2021-04-02 23:47:40", 
"2021-04-02 23:48:35", "2021-04-02 23:49:30", "2021-04-02 23:50:25", 
"2021-04-02 23:52:15", "2021-04-03 00:36:15", "2021-04-03 00:37:10", 
"2021-04-03 00:39:00", "2021-04-03 00:39:55", "2021-04-03 00:56:25", 
"2021-04-03 00:57:20", "2021-04-03 00:58:15", "2021-04-03 00:59:10", 
"2021-04-03 01:00:05", "2021-04-03 01:01:00", "2021-04-03 01:02:50", 
"2021-04-03 01:03:45", "2021-04-03 01:04:40", "2021-04-03 01:05:35", 
"2021-04-03 01:06:30", "2021-04-03 01:23:54", "2021-04-03 01:24:49", 
"2021-04-03 01:25:44", "2021-04-03 01:26:39", "2021-04-03 01:28:29", 
"2021-04-03 01:29:24", "2021-04-03 01:30:19", "2021-04-03 01:31:14", 
"2021-04-03 01:32:09", "2021-04-03 01:33:04", "2021-04-03 01:33:59", 
"2021-04-03 01:35:49", "2021-04-03 01:36:44", "2021-04-03 01:37:39", 
"2021-04-03 01:38:34", "2021-04-03 01:39:29", "2021-04-03 01:48:39", 
"2021-04-03 01:49:34", "2021-04-03 01:50:29", "2021-04-03 01:58:44", 
"2021-04-03 01:59:39", "2021-04-03 02:00:34", "2021-04-03 02:01:29", 
"2021-04-03 02:11:34", "2021-04-03 02:12:29", "2021-04-03 02:18:54", 
"2021-04-03 02:19:49", "2021-04-03 02:20:44", "2021-04-03 02:21:39", 
"2021-04-03 02:22:34", "2021-04-03 02:23:29", "2021-04-03 02:24:24", 
"2021-04-03 02:25:19", "2021-04-03 02:26:14", "2021-04-03 02:27:09", 
"2021-04-03 02:28:04", "2021-04-03 02:28:59"), jar = c("blank", 
"blank", "blank", "blank", "blank", "blank", "blank", "1", "1", 
"1", "1", "1", "2", "2", "2", "2", "2", "blank", "blank", "blank", 
"blank", "blank", "blank", "blank", "3", "3", "3", "3", "3", 
"3", "3", "blank", "blank", "blank", "blank", "blank", "blank", 
"blank", "1", "1", "1", "1", "1", "1", "1", "1", "2", "2", "2", 
"2", "2", "2", "blank", "blank", "blank", "blank", "blank", "3", 
"3", "3", "3", "3", "3", "3"), measurement_type = c("a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b"), new_column = c("blank1", 
"blank1", "blank1", "blank1", "blank1", "blank1", "blank1", "1", 
"1", "1", "1", "1", "2", "2", "2", "2", "2", "blank2", "blank2", 
"blank2", "blank2", "blank2", "blank2", "blank2", "3", "3", "3", 
"3", "3", "3", "3", "blank1", "blank1", "blank1", "blank1", "blank1", 
"blank1", "blank1", "1", "1", "1", "1", "1", "1", "1", "1", "2", 
"2", "2", "2", "2", "2", "blank2", "blank2", "blank2", "blank2", 
"blank2", "3", "3", "3", "3", "3", "3", "3")), class = "data.frame", row.names = c(NA, 
-64L))

答案 23 :(得分:-3)

您没有说出什么语言,但我假设C#/.NET,因为它具有原生DateTime数据类型。在这种情况下,只需使用ToString方法转换它,并使用格式说明符,如:

DateTime d = DateTime.Today;
string result = d.ToString("yyyy-MM-dd");

但是,我会警告不要在数据库查询中使用它或连接成SQL语句。数据库需要使用特定的格式化字符串。最好将时间部分归零并使用DateTime作为SQL参数,如果这是您要完成的任务。