sql server yyyyMMddHHmmss格式转换为yyyy-MM-dd HH:mm:ss
时间: 2024-05-12 10:13:37 浏览: 23
可以使用 SQL Server 中的 CONVERT 函数将日期时间格式从 yyyyMMddHHmmss 转换为 yyyy-MM-dd HH:mm:ss。例如:
```sql
SELECT CONVERT(VARCHAR(19), CONVERT(DATETIME, '20211231120123'), 120) AS ConvertedDateTime
```
输出结果为:
```
ConvertedDateTime
--------------------
2021-12-31 12:01:23
```
其中,第一个 CONVERT 函数将字符串转换为 DATETIME 类型,第二个 CONVERT 函数将 DATETIME 类型转换为 VARCHAR 类型,并使用格式代码 120 指定输出格式为 yyyy-MM-dd HH:mm:ss。
相关问题
sql server 转换yyyyMMddHHmmss格式为yyyy-MM-dd HH:mm:ss
你可以使用 CONVERT 函数将格式从yyyyMMddHHmmss转换为yyyy-MM-dd HH:mm:ss。以下是示例代码:
```sql
SELECT CONVERT(varchar(19), CONVERT(datetime, '20211231120000'), 120) AS formatted_date
```
这将把字符串'20211231120000'转换为日期时间格式,然后将其转换为yyyy-MM-dd HH:mm:ss格式的字符串。输出将是:
```
formatted_date
-------------------
2021-12-31 12:00:00
```
在这个示例中,120是 CONVERT 函数中与yyyy-MM-dd HH:mm:ss格式相对应的样式代码。
yyyyMMddHHmmss转yyyy-MM-dd HH:MM:SS java
你可以使用Java中的SimpleDateFormat类来格式化日期和时间。以下是将yyyyMMddHHmmss格式转换为yyyy-MM-dd HH:mm:ss格式的示例代码:
```java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
String inputDate = "20220101120000";
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = inputFormat.parse(inputDate);
String outputDate = outputFormat.format(date);
System.out.println(outputDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
```
在上面的示例中,我们首先定义输入日期的格式为"yyyyMMddHHmmss",输出日期的格式为"yyyy-MM-dd HH:mm:ss"。然后,我们使用`parse()`方法将输入日期字符串解析为Date对象,并使用`format()`方法将Date对象格式化为输出日期字符串。最后,我们打印输出日期字符串"2022-01-01 12:00:00"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)