sql server将字符串转换为datetime

时间:2022-07-26 23:43:20

I have this string: '30/05/2010', and I would like to enter it to a smallDatetime field. In the database it should look something like this 2010-05-30 15:33:25 Any Idea how?

我有这个字符串:“30/05/2010”,我想将它输入到一个smallDatetime字段。在数据库中,它应该是这样的:2010-05-30 15:33:25有什么想法吗?

TY

4 个解决方案

#1


5  

use

使用

select convert(smalldatetime,'30/05/2010',103)

#2


1  

SET DATEFORMAT DMY 
SELECT CAST('30/05/2010' as smalldatetime)

Where do you want the time aspect to come from? The convert above will append 00:00 (midnight) for smalldatetime because:

你希望时间方面来自哪里?上面的转换将为smalldatetime附加00:00(午夜),因为:

  • the string has no time information
  • 该字符串没有时间信息。
  • smalldatetime resolves to a minute resolution
  • smalldatetime解析为分钟分辨率

#3


0  

You need to use the datetime field type if you want in this format 2010-05-30 15:33:25. If you want only the date, use the date type only.

如果需要使用这种格式2010-05-30 15:33:25,则需要使用datetime字段类型。如果您只想要日期,请只使用日期类型。

#4


0  

You can use cast('05/30/2010' as smalldatetime).

您可以使用cast('05/30/2010'作为smalldatetime)。

If you need to have exact 15:33:25 time then you can use several dateadd calls, e.g. select dateadd(hh, 15, cast('05/30/2010' as smalldatetime)) returns 2010-05-30 15:00:00.

如果您需要有确切的15:33:25时间,那么您可以使用几个dateadd调用,例如选择dateadd(hh, 15, cast('05/30/2010' as smalldatetime))返回2010-05-30 15:00:00。

#1


5  

use

使用

select convert(smalldatetime,'30/05/2010',103)

#2


1  

SET DATEFORMAT DMY 
SELECT CAST('30/05/2010' as smalldatetime)

Where do you want the time aspect to come from? The convert above will append 00:00 (midnight) for smalldatetime because:

你希望时间方面来自哪里?上面的转换将为smalldatetime附加00:00(午夜),因为:

  • the string has no time information
  • 该字符串没有时间信息。
  • smalldatetime resolves to a minute resolution
  • smalldatetime解析为分钟分辨率

#3


0  

You need to use the datetime field type if you want in this format 2010-05-30 15:33:25. If you want only the date, use the date type only.

如果需要使用这种格式2010-05-30 15:33:25,则需要使用datetime字段类型。如果您只想要日期,请只使用日期类型。

#4


0  

You can use cast('05/30/2010' as smalldatetime).

您可以使用cast('05/30/2010'作为smalldatetime)。

If you need to have exact 15:33:25 time then you can use several dateadd calls, e.g. select dateadd(hh, 15, cast('05/30/2010' as smalldatetime)) returns 2010-05-30 15:00:00.

如果您需要有确切的15:33:25时间,那么您可以使用几个dateadd调用,例如选择dateadd(hh, 15, cast('05/30/2010' as smalldatetime))返回2010-05-30 15:00:00。