Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to fetch data from sql server data base from C# code.

I have 2 columns of "datetime" data type.

but i want only date part of that field when feting data from C#.

When i am using same query in SSMS , it is showing only date part as per my requirement.

but when i am convert in C# and feting data from C# then it is showing full date time data.

What I have tried:

SQL query which is working is SSMS

SQL
SELECT tk.TaskLisiId, tk.JobNumber, tk.PM, tk.TM, 
tk.Description, CONVERT(Date, tk.Date) as Date,
CONVERT(Date, tk.CompletedByDate) as CompletedByDate, tk.Status 
FROM TaskList tk WHERE tk.TaskLisiId<>0




query using when feting data from database in C#

C#
string queryString = "SELECT tk.TaskLisiId, tk.JobNumber, tk.PM, tk.TM, tk.Description, CONVERT(Date, tk.Date) as Date," +
"CONVERT(Date, tk.CompletedByDate) as CompletedByDate, tk.Status FROM TaskList tk WHERE tk.TaskLisiId<>0";
Posted
Updated 1-Aug-21 21:58pm
Comments
Maciej Los 2-Aug-21 3:55am    
You are using CONVERT function wrong way. See: CAST and CONVERT (Transact-SQL) - SQL Server | Microsoft Docs[^]

1 solution

That's because when the date info arrives in C#, it comes in as a DateTime value - C~ has no concept of a Date without a Time component, unlike SQL.

What you need to do is format the DateTime value as "just the date" when you convert it to a string for presentation to the user.
Exactly how you do that will depend on the controls you are using and the environment in which you are using them, but Google will find you the formating info you need if you search for the controls / environment / date format combination you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900