king yau
king yau

Reputation: 510

How to select a value of type date as "DD-MM-YYYY" in SQL Server

I have a column creationDate which of type date in a SQL Server database table. I want to select it as DD-MM-YYYY format.

However, there is an error when I try to use

SELECT CONVERT(varchar(10), creationDate, 105) 
FROM tableName

in my C# application.

The error message is:

A field or property with the name 'creationDate ' was not found on the selected data source.

Upvotes: 0

Views: 676

Answers (2)

RBT
RBT

Reputation: 25907

SQL Server doesn't provide any default alias name to output columns. Your current query SELECT CONVERT(varchar(10), creationDate, 105) FROM tableName doesn't output creationDate column name as shown in the snapshot below. It says - (No column name):

enter image description here

You must give a column alias name in your SQL query for C# to capture on application side as shown in the snapshot below:

SELECT CONVERT(varchar(10), creationDate, 105) AS [creationdate] FROM tableName

enter image description here

This should solve your problem but it is always advised that you should do such conversions on application layer as many a times such conversions will require taking locale and time-zone of your client computer (where the application is running) into consideration. This happens as developers generally store date time data in database in GMT in 24 hour date-time format.

Upvotes: 4

krish
krish

Reputation: 881

Set an alias name for that column in your query to resolve your issue

SELECT CONVERT(varchar(10), creationDate, 105) [creationDate] FROM tableName

Upvotes: 3

Related Questions