Reputation: 510
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
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)
:
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
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
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