简体   繁体   English

在SQL中将字符串yyyymmddhhmmss转换为hh:mm:ss格式

[英]Convert string yyyymmddhhmmss into hh:mm:ss format in SQL

With the string DATETIME yyyymmddhhmmss like 20160125173013 , I would like to convert this string into hh:mm:ss (17:30:13) as a new column called "Time" in a table with sql update statement. 使用像20160125173013这样的字符串DATETIME yyyymmddhhmmss ,我想将此字符串转换为hh:mm:ss (17:30:13)作为带有sql update语句的表中名为“ Time”的新列。 However I am only able to convert it into 17:30 using the stuff function. 但是我只能使用stuff函数将其转换为17:30。 Is there any possible solution to convert? 有什么可能的解决方案可以转换?

In my statement 在我的陈述中

UPDATE db
SET Time =convert(time, stuff(substring(DATETIME,9,6),3,2,':'))
FROM db
WHERE Time IS NULL

Real Output= 17:13:00.0000000 实际输出= 17:13:00.0000000

But my expected output is 17:13:00 但我的预期输出是17:13:00

Thanks a lot! 非常感谢!

这是mysql的奇迹:

SELECT time(str_to_date('20160125173013', '%Y%m%d%H%i%s'));

If you ar eusing SQL server then use Convert function 如果您正在使用SQL Server,请使用Convert函数

 Declare  @VarCharDate  varchar(max)
 Declare  @VarCharDate1 varchar(max)

--Declare - 宣布

set  @VarCharDate = '20160125173013' --- YYYYMMDDHHMMSS

--Convert - 兑换

 set @VarCharDate1 =(select SUBSTRING(@VarCharDate,0,5) + '/' +
SUBSTRING(@VarCharDate,5,2)  + '/' + SUBSTRING(@VarCharDate,7,2)
  +  ' ' + SUBSTRING(@VarCharDate,9,2)
  +':'+SUBSTRING(@VarCharDate,11,2) +':' + RIGHT(@VarCharDate,2))

   select @VarCharDate1



 select  Convert(varchar(8),convert(datetime, @VarCharDate1, 120),114)

Do you have an actual DATETIME field? 您有实际的DATETIME字段吗? If so you can use DATE_FORMAT() : 如果是这样,您可以使用DATE_FORMAT()

UPDATE mytable SET my_time=DATE_FORMAT(my_date, '%H:%i:%s')

If you don't have a native DATETIME field I hope you can convert it to one, as irregular, quirky formats cause trouble and introduce a lot of overhead when parsing to convert. 如果您没有本地DATETIME字段,我希望您可以将其转换为一个,因为不规则,古怪的格式会带来麻烦,并且在解析转换时会带来很多开销。 STR_TO_DATE() can do the opposite of DATE_FORMAT() and convert from arbitrary strings to native DATE or DATETIME values. STR_TO_DATE()可以执行与DATE_FORMAT()相反的操作,并将任意字符串转换为本地DATEDATETIME值。

Don't confuse STORAGE with PRESENTATION 不要将存储与演示相混淆

Declare @YourTable table (DateTime varchar(25),Time time)
Insert Into @YourTable values
('20160125173013',null)

Update @YourTable
   Set Time = stuff(left(right(DateTime,6),4),3,0,':')

Select * 
      ,FormatedTime = Format(cast(Time as datetime),'HH:mm')
 From @YourTable

Returns 退货

DateTime        Time                FormatedTime
20160125173013  17:30:00.0000000    17:30

Based on the use of the STUFF function I believe this is Microsoft SQL Server, and not MySql. 基于STUFF函数的使用,我相信这是Microsoft SQL Server,而不是MySql。 Therefor, you can do something like this: 因此,您可以执行以下操作:

UPDATE db
SET [Time] = CAST(SUBSTRING([DATETIME], 9, 2) +':'+ 
                  SUBSTRING([DATETIME], 11, 2) +':'+ 
                  RIGHT([DATETIME], 2) As Time)
WHERE [Time] IS NULL

Your string is no format, SQL Server will cast implicitly 您的字符串没有格式,SQL Server将隐式转换

DECLARE @YourDateTimeString VARCHAR(100)='20160125173013';

The following query will cut the first 8 digits and cast them to DATE , which works implicitly (unseparated format). 以下查询将剪切前8位数字并将其转换为DATE ,这将隐式工作(不分隔格式)。 The time is cut from the right side, then the two : -signs are stuffed into the right places: 时间从右侧减少,然后将两个:符号填充到正确的位置:

SELECT CAST(LEFT(@YourDateTimeString,8) AS DATE)
      ,CAST(STUFF(STUFF(RIGHT(@YourDateTimeString,6),5,0,':'),3,0,':') AS TIME);

The result 结果

2016-01-25  17:30:13.0000000

If you need this as string without the trailing .0000000 ( which is a pure output format question and should be done in your presentation layer! ) you can just use LEFT() . 如果您需要将此字符串作为字符串而没有尾随.0000000这是一个纯输出格式问题,应该在表示层中完成! ),则可以使用LEFT() The input of this function is string (again implicitly casted), the output is a text which looks like a time . 该函数的输入是字符串(同样是隐式转换的),输出是看起来像time的文本

SELECT CAST(LEFT(@YourDateTimeString,8) AS DATE)
      ,LEFT(CAST(STUFF(STUFF(RIGHT(@YourDateTimeString,6),5,0,':'),3,0,':') AS TIME),8);

The result 结果

2016-01-25  17:30:13

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM