Declare @date_and_time varchar(100)= '04/29/23 01:29:55'
Loading
Declare @date_and_time varchar(100)= '04/29/23 01:29:55'
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajkiran SwainRank #165 · 14.4K reputationPosted May 6, 2023, 9:56 AM EDT
You can use the `CONVERT` function in SQL Server to convert the string to a datetime data type. Here's an example:
```
DECLARE @date_and_time varchar(100) = '04/29/23 01:29:55'
DECLARE @datetime datetime
SET @datetime = CONVERT(datetime, @date_and_time, 101)
SELECT @datetime
```
In this example, the third parameter of the `CONVERT` function, which is set to 101, specifies the style of the input date string. Style 101 represents the format `mm/dd/yyyy`. The converted datetime value can then be assigned to a datetime variable or used in a query.
Sachin SinghRank #66 · 26.2K reputationPosted May 6, 2023, 1:28 PM EDT
also like this
Declare @date_and_time varchar(100)= '04/29/23 01:29:55' select Cast(@date_and_time as datetime)Tuhin PaulRank #7 · 71.9K reputationPosted May 6, 2023, 11:12 PM EDT
string date_and_time = "04/29/23 01:29:55"; DateTime datetime; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand("SELECT CONVERT(datetime, @date_and_time, 101)", connection)) { command.Parameters.AddWithValue("@date_and_time", date_and_time); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { datetime = reader.GetDateTime(0); } } } }Raj BhattRank #513 · 4.8K reputationPosted May 7, 2023, 10:53 AM EDT
you can try below code :-
SELECT CONVERT(datetime, '2023-05-07 12:34:56', 120) As Todaydate SELECT CAST('2023-05-07 12:34:56' AS datetime) As TodaydateAnandu G NathRank #681 · 3.5K reputationPosted Dec 27, 2023, 3:29 AM EST
Declare @date_and_time varchar(100); set @date_and_time= '04/29/23 01:29:55' select Cast(@date_and_time as datetime) print Cast(@date_and_time as datetime)