Click here to Skip to main content
15,896,489 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
i am saving 'patientadmissiondate' field as DateTime.Now;. When i view table data i see the values in this column in this format '11/24/2015 10:46:05 AM'.. 
however when i do select * from tbpatientadmissiondate i see the values in this column as '2015-11-24 10:46:05.443'
Due to this i am having issue returning the records which match a particular 'patientadmissiondate' .Please guide!
Posted

Hi, Try this,

SQL
select CONVERT(VARCHAR(50),GETDATE(),22)


It will gie the result as
11/24/15 12:39:11 PM




Please see the links for furture formats which us can use for your requirement.

http://www.sql-server-helper.com/sql-server-2012/format-function-vs-convert-function.aspx[^]

http://www.sqlusa.com/bestpractices/datetimeconversion/[^]
 
Share this answer
 
Comments
Member 12156793 24-Nov-15 2:15am    
plz explain.. in sql table patientadmissiondate field in datetime format..
deepankarbhatnagar 24-Nov-15 2:27am    
select CONVERT(VARCHAR(50),patientadmissiondate ,22)

Hope this will solve your query
Hi ,


By Default Sql server saving datetime in yyyy-mm-dd hh:mm:ss formate.

if you have issue in date formate while retrieving  you can change date formate and compare with patientadmissiondate.


e.g.

SQL
select CONVERT(varchar(10), CONVERT(datetime, GETDATE(), 101), 101)



in C# Side :-

C#
Please try Below code

  var rep = from s in ctx.tbreports
           where s.dateofreport.ToString("yyyy-MM-dd") ==                  date.ToString("yyyy-MM-dd")
                      select s;
 
Share this answer
 
v3
Comments
Member 12156793 24-Nov-15 1:35am    
Please guide how can i keep data in 11/24/2015 10:46:05 AM format in sql field
sanjay243657 24-Nov-15 1:42am    
i have updated the solution please check..
Member 12156793 24-Nov-15 1:49am    
thanks for the update. However, i still am not able to do the comparison as using the above update i now get the date in this format i.e 11/24/2015.. i need it in this format 11/24/2015 10:46:05 AM. what change is required for that and Do i change the query where i insert date in database?
sanjay243657 24-Nov-15 1:55am    
where you are comparing date in sql side or C# side...
Member 12156793 24-Nov-15 2:07am    
c#(MVC) var rep = from s in ctx.tbreports
where s.dateofreport == date
select s;

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