How to return only the Date from a SQL Server DateTime?
One thing to remember is that when you cast a DateTime to a string, it returns a character string with the format of yyyy-mm-dd hh:mm:ss.fffffff. If you need only the date, then I believe it's enough to have only the four-digit year and month -- but if you need only the time, then the number of digits must be increased by one (to include the seconds).
convert date to datetime sql
-- 2 ways SELECT CONVERT(datetime, '2021-01-01'); SELECT CAST('2021-01-01' AS datetime);
sql server convert string to date
select Cast( '2021-09-22' as date ) as castedDate; select convert(date,'20210922') as convertedDate;
cast datetime to date in sql
SELECT CONVERT(date, getdate());
sql datitime to date
sql datetime as date
SELECT CONVERT(DATE, GETDATE()) date;
There is no need to return the time part of the DateTime variable from your database, you can simply use only the date and adjust the SQL Server data type accordingly.