There may be various scenarios wherein you need only Date part of an SQL Server DateTime value, rather than full DateTime value.
This is very much a requirement in case of comparing dates (only date and no datetime).
SQL Server 2008 offers a new datatype DATE which holds only Date.
So you can simply use Convert(Date, GetDate()) to convert your full DateTime into Date. Its as simple as that.
But if you are using a version prior to SQL Server 2008, you need to do a workaround. There are lot many ways to achieve that. Following is one of them which is being used too frequently.
Say your DateTime value is "2012-04-20 21:45:00.938"
First convert date into a varchar in format such as (dd/mm/yyyy or mm/dd/yyyy) depending upon the regional settings of the SQL Server's host environment.
SELECT CONVERT(varchar(10), GETDATE(), 101) -- 101 stands for mm/dd/yyyy (mostly in case of US)
SELECT CONVERT(varchar(10), GETDATE(), 103) -- 103 stands for dd/mm/yyyy
So, it will result into - "04/20/2012", "20/04/2012" respectively
But this is a varchar type, so you need to convert it back to DateTime type to make it able to be used in Date time operations.
Use either of this depending upon the settings in SQL server's host environment.
SELECT CONVERT(DATETIME, CONVERT(varchar(10), GETDATE(), 101) )
or
SELECT CONVERT(DATETIME, CONVERT(varchar(10), GETDATE(), 103) )
This will now result to "2012-04-20 00:00:00.000"
It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command
ReplyDeleteData Science Training in Indira nagar
Data Science Training in btm layout
Data Science Training in Kalyan nagar
Data Science training in Indira nagar
Data science training in bangalore
Very awesome!!! When I searched for this I found this website at the top of all blogs in search engines.Best Digital Marketing Courses in Hyderabad
ReplyDeletewow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated.
ReplyDeletedata scientists training
I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.
ReplyDeletedata scientist training and placement
Thank you for the information you post and hopefully useful to readers
ReplyDeletePython Training in Hyderabad
Python Course in Hyderabad
Informative blog. Useful to many people. Thanks for sharing with us.
ReplyDeleteData Science Training in Hyderabad
Hi buddies, it is a great written piece entirely defined, continuing the good work constantly.
ReplyDeletedata science online training in hyderabad
betmatik
ReplyDeletekralbet
betpark
mobil ödeme bahis
tipobet
slot siteleri
kibris bahis siteleri
poker siteleri
bonus veren siteler
UKH
شركة عزل اسطح
ReplyDeleteعزل اسطح
شركة عزل اسطح
ReplyDeleteعزل اسطح
شركة مكافحة بق الفراش بالجبيل 51ckbNDJY0
ReplyDelete