-4

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?

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nurf
  • 5
  • 4
  • I disagree that this is a duplicate. I select a column from a table. While the other person asks simply from a table? I think this post makes it easier to understand. If this is to be deleted it should at least be merged with the other post. – Nurf Dec 06 '17 at 07:11

4 Answers4

0

Just use 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.

lucky
  • 12,734
  • 4
  • 24
  • 46
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)

Serkan Arslan
  • 13,158
  • 4
  • 29
  • 44
0

You can use FORMAT like this:

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

Here is the syntax for the convert() function.

CONVERT(data_type(length), expression, style)

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.

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

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

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

refer the below query to your answer,

   select CONVERT(date,BDATE) from PersonData

Refer the below link to various type of Convert Formation.

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