在 SQL Server 中将 Varchar 转换为时间

2022-11-04数据库问题
374

本文介绍了在 SQL Server 中将 Varchar 转换为时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我如何将时间格式 " 10:02:22 PM " 转换为 SQL Server 日期时间格式.

How do i convert the Time format " 10:02:22 PM " into an SQL Server DateTime Format.

我有一列这样的数据.我从 CSV 文件导入了此列,现在想将其转换为日期时间格式,以便我能够使用日期时间函数.

I have a column of such data. I imported this column from a CSV File and now want to convert it into a DateTime Format so that i can be able to use Date Time Functions.

我希望能够将此列插入到另一个具有正确日期时间格式的表中.

I want to be able to insert this column into another table with correct DateTime format.

推荐答案

使用此链接:http://msdn.microsoft.com/en-us/library/ms187928.aspx.

仅用于时间转换...

SELECT CONVERT( TIME, '10:00:22 PM' );

给出以下输出...

22:00:22.000000

带有日期和时间的时间转换...

Time conversion with date and time...

SELECT CONVERT( DATETIME, '10:00:22 PM' );

给出以下输出...

1900-01-01 22:00:22.0000 

注意:对于datetime,您需要将特定日期作为输入,否则会考虑默认世纪日期.

Note : For datetime you need to have specific date as input otherwise it consider default century date.

这篇关于在 SQL Server 中将 Varchar 转换为时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End
SQLServer SQL Server

相关推荐

如何在 sql server 2012 中部署现有的 SSIS 包?
How to deploy a existing SSIS Package in sql server 2012?(如何在 sql server 2012 中部署现有的 SSIS 包?)...
2024-04-16 数据库问题
11

“无法连接到本地 MySQL 服务器"在 docker-compose 中
quot;Can#39;t connect to local MySQL serverquot; in docker-compose(“无法连接到本地 MySQL 服务器在 docker-compose 中)...
2024-04-16 数据库问题
51

在 SQL Server 中设计 1:1 和 1:m 关系
Designing 1:1 and 1:m relationships in SQL Server(在 SQL Server 中设计 1:1 和 1:m 关系)...
2024-04-16 数据库问题
2

主键的 Sql 数据类型 - SQL Server?
Sql Data Type for Primary Key - SQL Server?(主键的 Sql 数据类型 - SQL Server?)...
2024-04-16 数据库问题
4

SQL Server:如何限制表包含单行?
SQL Server: how to constrain a table to contain a single row?(SQL Server:如何限制表包含单行?)...
2024-04-16 数据库问题
3

SQL Server 中数据库范围的唯一但简单的标识符
Database-wide unique-yet-simple identifiers in SQL Server(SQL Server 中数据库范围的唯一但简单的标识符)...
2024-04-16 数据库问题
4