xxxxxxxxxx
-- 2 ways
SELECT CONVERT(datetime, '2021-01-01');
SELECT CAST('2021-01-01' AS datetime);
xxxxxxxxxx
select Cast( '2021-09-22' as date ) as castedDate;
select convert(date,'20210922') as convertedDate;
xxxxxxxxxx
-- Assuming we want to convert a datetime value to a specific format, such as 'YYYY-MM-DD HH:MI:SS'
-- Using the CONVERT function
SELECT CONVERT(varchar, GETDATE(), 120) AS converted_datetime;
-- Using the FORMAT function (available from SQL Server 2012 onwards)
SELECT FORMAT(GETDATE(), 'yyyy-MM-dd HH:mm:ss') AS converted_datetime;
xxxxxxxxxx
-- Create test case
DECLARE @myDateTime DATETIME
SET @myDateTime = '2008-05-03'
-- Convert string
SELECT LEFT(CONVERT(VARCHAR, @myDateTime, 120), 10)
xxxxxxxxxx
DECLARE @datetimeValue datetime;
DECLARE @dateValue date;
SET @datetimeValue = GETDATE(); -- Replace with your actual datetime value
SET @dateValue = CONVERT(date, @datetimeValue);
xxxxxxxxxx
declare @Existingdate datetime
Set @Existingdate=GETDATE()
Select CONVERT(varchar,@Existingdate,103) as [DD/MM/YYYY]