SQL Server仅获取日期和时间的日期表单列[重复]

时间:2023-01-17 19:07:24

This question already has an answer here:

这个问题在这里已有答案:

I have been trying to get only date from a column with date and time.

我一直试图从日期和时间的列中获取日期。

Here is a sample:

这是一个示例:

Select BDATE 
from PersonData

Current output:

1977-12-05 15:40:54.000 

Desired output:

1977-12-05

How can I use the CONVERT date from datetime?

如何使用日期时间的CONVERT日期?

Thanks in advance

提前致谢

4 个解决方案

#1


0  

Just use Cast;

只需使用Cast;

Select cast(BDATE as Date) BDATE from PersonData 

Also, you can use Convert function

此外,您可以使用转换功能

Select convert(Date,BDATE) BDATE from PersonData 

If you don't want special conversation styles, I suggest you to use Cast instead of Convert for simple type conversations.

如果您不想要特殊的会话样式,我建议您使用Cast而不是Convert来进行简单的类型对话。

#2


0  

You can use this.

你可以用它。

SELECT CONVERT(VARCHAR(10), BDATE , 120) from PersonData

style : 120 for ODBC canonical yyyy-mm-dd hh:mi:ss(24h)

风格:120为ODBC规范yyyy-mm-dd hh:mi:ss(24h)

#3


0  

You can use FORMAT like this:

您可以像这样使用FORMAT:

SELECT FORMAT([BDATE],'yyy-MM-dd') AS 'BDATE' FROM PersonData;

#4


0  

Here is the syntax for the convert() function.

这是convert()函数的语法。

CONVERT(data_type(length), expression, style)

CONVERT(data_type(长度),表达式,样式)

The datatype to convert expression to. Can be one of the following: bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real, datetime, smalldatetime, char, varchar, text, nchar, nvarchar, ntext, binary, varbinary, or image. Required filed.

要将表达式转换为的数据类型。可以是以下之一:bigint,int,smallint,tinyint,bit,decimal,numeric,money,smallmoney,float,real,datetime,smalldatetime,char,varchar,text,nchar,nvarchar,ntext,binary,varbinary,or图片。要求提交。

The length of the resulting data type (for char, varchar, nchar, nvarchar, binary and varbinary).Its and Optional.

结果数据类型的长度(对于char,varchar,nchar,nvarchar,binary和varbinary).Its和Optional。

The Expression value to convert to another data type. Required filed.

要转换为其他数据类型的Expression值。要求提交。

The Style format used to convert between data types, such as a date or string format.Its an Optional.

Style格式用于在数据类型之间进行转换,例如日期或字符串格式。它是可选的。

refer the below query to your answer,

请参考以下查询到您的答案,

   select CONVERT(date,BDATE) from PersonData

Refer the below link to various type of Convert Formation.

请参阅以下链接,了解各种类型的转换形成。

https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql

#1


0  

Just use Cast;

只需使用Cast;

Select cast(BDATE as Date) BDATE from PersonData 

Also, you can use Convert function

此外,您可以使用转换功能

Select convert(Date,BDATE) BDATE from PersonData 

If you don't want special conversation styles, I suggest you to use Cast instead of Convert for simple type conversations.

如果您不想要特殊的会话样式,我建议您使用Cast而不是Convert来进行简单的类型对话。

#2


0  

You can use this.

你可以用它。

SELECT CONVERT(VARCHAR(10), BDATE , 120) from PersonData

style : 120 for ODBC canonical yyyy-mm-dd hh:mi:ss(24h)

风格:120为ODBC规范yyyy-mm-dd hh:mi:ss(24h)

#3


0  

You can use FORMAT like this:

您可以像这样使用FORMAT:

SELECT FORMAT([BDATE],'yyy-MM-dd') AS 'BDATE' FROM PersonData;

#4


0  

Here is the syntax for the convert() function.

这是convert()函数的语法。

CONVERT(data_type(length), expression, style)

CONVERT(data_type(长度),表达式,样式)

The datatype to convert expression to. Can be one of the following: bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real, datetime, smalldatetime, char, varchar, text, nchar, nvarchar, ntext, binary, varbinary, or image. Required filed.

要将表达式转换为的数据类型。可以是以下之一:bigint,int,smallint,tinyint,bit,decimal,numeric,money,smallmoney,float,real,datetime,smalldatetime,char,varchar,text,nchar,nvarchar,ntext,binary,varbinary,or图片。要求提交。

The length of the resulting data type (for char, varchar, nchar, nvarchar, binary and varbinary).Its and Optional.

结果数据类型的长度(对于char,varchar,nchar,nvarchar,binary和varbinary).Its和Optional。

The Expression value to convert to another data type. Required filed.

要转换为其他数据类型的Expression值。要求提交。

The Style format used to convert between data types, such as a date or string format.Its an Optional.

Style格式用于在数据类型之间进行转换,例如日期或字符串格式。它是可选的。

refer the below query to your answer,

请参考以下查询到您的答案,

   select CONVERT(date,BDATE) from PersonData

Refer the below link to various type of Convert Formation.

请参阅以下链接,了解各种类型的转换形成。

https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql