Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i want to convert dd/mm/yyyy hh:mm:ss to yyyy-mm-dd hh:mm:ss.000


i am using this-


convert(datetime,'dd/mm/yyyy hh:mm:ss')

&

convert(varchar(24),'dd/mm/yyyy hh:mm:ss',121)

both gives me that error-
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
C#
string totalDateStart = txtStartDate.Text.Trim() + " " + txtSHr.Text.Trim() + ":" + txtSMin.Text.Trim() + ":00" + " " + ddlAM1.SelectedValue; 
DateTime StrDate = Convert.ToDateTime(totalDateStart); 

i want to check whether the same time in time is entered or not. i am using this code-

i get startdate. user insert in dd/mm/yyyy hh:mm:ss format. and in database i use startdate as datetime format so it automatically take yyyy-mm-dd hh:mm:ss.000 format.

Thanks
Posted
Updated 14-Mar-12 19:56pm
v2
Comments
Ajith The Great 14-Mar-12 2:27am    
Hi ,

It Would Be Better if You Convert it in C# Code instead of Sql Query.
mayankshrivastava 14-Mar-12 2:47am    
Ok,any suggestion how to convert it in c# code ?

Try the below query. It converts a string in dd/mm/yyyy hh:mm:ss format to yyyy-mm-dd hh:mm:ss.000
SQL
DECLARE @DatetimeString VARCHAR(25) = '23/10/2016  10:12:12.000'

SELECT CONVERT(VARCHAR(24), CONVERT(DATETIME, @DatetimeString, 103), 121) Date

You get the result as

SQL
Date
------------------------
2016-10-23 10:12:12.000


But still I am not very convinced in the way in which you are comparing the dates. Dates should be compared after converting both to datetime.
 
Share this answer
 
Comments
mayankshrivastava 15-Mar-12 3:20am    
Thanks ...
Use this:
SQL
SELECT CONVERT(VARCHAR(23), GETDATE(), 121)
 
Share this answer
 
Comments
mayankshrivastava 14-Mar-12 2:24am    
same error...
Prasad_Kulkarni 14-Mar-12 3:22am    
mate, can u post code so i can get what exactly prob is
uttam2010 14-Mar-12 3:17am    
create a function ,return varchar with same code & use it in your select query
this is help you.

SELECT CONVERT(VARCHAR(23), GETDATE(), 121)
mayankshrivastava 15-Mar-12 1:54am    
i want to check whether the same time in time is entered or not.
i am using this code-
string totalDateStart = txtStartDate.Text.Trim() + " " + txtSHr.Text.Trim() + ":" + txtSMin.Text.Trim() + ":00" + " " + ddlAM1.SelectedValue;
DateTime StrDate = Convert.ToDateTime(totalDateStart);

i get startdate.
user insert in dd/mm/yyyy hh:mm:ss format.
and in database i use startdate as datetime format so it automatically take
yyyy-mm-dd hh:mm:ss.000 format.

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