简体   繁体   English

如何将hh:mm:ss格式的nvarchar字符串列永久转换为表中的时间数据类型?

[英]How to convert a column of nvarchar string in hh:mm:ss format into datatype of time permanently in a table?

I want to convert a table column like 我想转换一个表列

Departure_time 出发时间

06:44:00 (<- these are just NVARCHAR strings) 06:44:00(<-这些只是NVARCHAR字符串)

06:45:00 06:45:00

06:45:00 06:45:00

06:46:00 06:46:00

06:47:00 06:47:00

06:47:00 06:47:00

06:48:00 06:48:00

06:49:00 06:49:00

06:50:00 06:50:00

06:50:00 06:50:00

06:50:00 06:50:00

06:51:00 06:51:00

06:51:00 06:51:00

06:52:00 06:52:00

06:52:00 06:52:00

(sorry guys I really do want to post a screen-shot here which would made the post looks way much better but it says I need a reputation of 10. So, I just make one table column) (对不起,我真的很想在此发布屏幕截图,这会使该帖子看起来好多了,但它说我的信誉为10。所以,我只列出一个表格列)

which in NVARCHAR datatype, into TIME type in MSSQL 2012? NVARCHAR数据类型中将哪个转换为MSSQL 2012中的TIME类型?

I tried Time(0) and Time(7) and all threw an error of 我尝试了时间(0)和时间(7),都抛出了错误

"Conversion failed when converting date and/or time from character string." “从字符串转换日期和/或时间时转换失败。”

and I do want to change the type of that column of the table permanently. 我想永久更改表的该列的类型。 Not only select them and present them in the time format. 不仅选择它们并以时间格式显示它们。

So... anyone can help me on how to do this please? 所以...有人可以帮助我吗?

Thank you! 谢谢!

select convert(varchar(32),getdate(),8)

OR 要么

DECLARE @DateNow smalldatetime
SET @DateNow = GETDATE()

SELECT CONVERT(CHAR(5), @DateNow, 8) 

OR 要么

 UPDATE your_tablename
    SET columnName = CONVERT(NVARCHAR(255),CONVERT(SMALLDATETIME, columnName,105))
    ALTER TABLE myTable
    ALTER COLUMN columnName SMALLDATETIME

解决方案非常简单:

select cast (Departure_time as time) from <you_table>

The following works as (I) would expect and ends up with a table consisting of a single time(7) column: (I)预期的以下工作并最终得到一个包含单个time(7)列的表:

create table T (Departure_time nvarchar(10) not null)
insert into T(Departure_time) values
('06:44:00'),('06:45:00'),('06:45:00'),('06:46:00'),('06:47:00'),
('06:47:00'),('06:48:00'),('06:49:00'),('06:50:00'),('06:50:00'),
('06:50:00'),('06:51:00'),('06:51:00'),('06:52:00'),('06:52:00')

alter table T alter column Departure_Time time(7) not null

I suspect you have some rows which don't have valid values in them. 我怀疑您有一些行中没有有效值。 These may be more or less easy to discover, depending on how close to the required format the invalid values are. 这些可能或多或少容易发现,具体取决于无效值与所需格式的接近程度。

select * from t where not Departure_time like '[0-2][0-9]:[0-5][0-9]:[0-5][0-9]'

Finds values that are definitely wrong. 查找绝对错误的值。 But it wouldn't find eg 27:34:32 但找不到27:34:32

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

相关问题 如何在mssql中将NVARCHAR值转换为dd-mm-yyyy hh:mm:ss AM / PM格式 - How to convert NVARCHAR value to dd-mm-yyyy hh:mm:ss AM/PM format in mssql 使用hh:mm:ss:ms格式将varchar字段转换回time数据类型字段 - Convert varchar field back to time datatype field with hh:mm:ss:ms format MS SQL dataType,格式为HH.MM.SS - MS SQL dataType for time in this format HH.MM.SS 如何从hh:mm:ss中的“时间”数据类型列中求和? - How to sum times from Time datatype columns in hh:mm:ss? 如何将HH:MM:SS转换为HH:MM - How to convert HH:MM:SS to HH:MM 如何将日期时间列格式从 yyyy-dd-MM HH:mm:ss 转换为 yyyy-MM-dd HH:mm:ss - How do I convert a datetime column format from yyyy-dd-MM HH:mm:ss to yyyy-MM-dd HH:mm:ss 如何在SQL Server中将时间从dd:hh:mm:ss格式化为仅hh:mm:ss? - How to format time from dd:hh:mm:ss to only hh:mm:ss in SQL server? 如何将 YYMMDDhhmm 格式的 NVARCHAR 转换为 YYYY-MM-DD hh:mm:ss 格式的 DATETIME? - How to convert YYMMDDhhmm formatted NVARCHAR into YYYY-MM-DD hh:mm:ss formatted DATETIME? SQL SERVER 如何将包含“dd/MM/yyyy HH:mm:ss”的 nvarchar 字段转换为日期时间 - SQL SERVER how to convert a nvarchar field that contains 'dd/MM/yyyy HH:mm:ss' to datetime 如何将日期时间格式从'YYYY-MM-DD'转换为'MM / DD / YYYY HH:MM:SS AM' - How to convert Date Time format from 'YYYY-MM-DD' to 'MM/DD/YYYY HH:MM:SS AM'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM