how do i get the date alone from datetime field from sql server.
eg
2015-01-29 10:17:37.000 from this i want only the date 2015-01-29
Loading
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
Michal HabalcikRank #902 · 2.3K reputationPosted Jan 28, 2015, 9:03 PM EST
SELECT CONVERT(date, getdate())
SELECT CAST(getdate() as date)
Suraj SahooRank #382 · 6.6K reputationPosted Jan 28, 2015, 7:35 PM EST
If you are not having the data type for the column as only "date" and the datatype is "datetime", then you will be saving the datetime object in the column.
And anticipating you have c# used. C# only has DateTime class. Even if you fetch date or datetime object c# understand datetime only. So you can have a viewmodel having the date property as string in C# and while retrieving and setting the value from SQL server, Use
Convert.ToDateTime(FETCHED DATE).ToShortDateString();
The ToShortDateString() returns only a date.
I hope this helps
Thanks
santhosh kumar61 reputationPosted Jan 28, 2015, 7:42 PM EST
Jignesh TrivediRank #16 · 50.3K reputationPosted Jan 28, 2015, 9:29 PM EST
you can casting this datetime field to date if you are using SQL server 2008 and above.
try...
select CAST('2015-01-29 10:17:37.000' as date)
select CAST(yourdatetimefiled as date) from table1
hope this will help you.
Ghanshyam Baravaliya137 reputationPosted Aug 13, 2015, 2:06 AM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Aug 13, 2015, 3:18 AM EDT
Upendra Pratap ShahiRank #329 · 7.5K reputationPosted Aug 13, 2015, 3:24 AM EDT
select CONVERT(VARCHAR(10),GETDATE(),110);
Sumit JoshiRank #1,252 · 1.4K reputationPosted Aug 13, 2015, 3:27 AM EDT