Ada 3 metode berbeda tergantung pada apa yang saya butuhkan dan versi mana yang saya gunakan.
Berikut metodenya ..
1) Menggunakan Convert
DECLARE @DateTime DATETIME = GETDATE();
SELECT
CONVERT(NVARCHAR, @DateTime,120) AS 'myDateTime'
,CONVERT(NVARCHAR(10), @DateTime, 120) AS 'myDate'
,RIGHT(CONVERT(NVARCHAR, @DateTime, 120),8) AS 'myTime'
2) Menggunakan Cast (SQL Server 2008 dan seterusnya)
SELECT
CAST(@DateTime AS DATETIME2) AS 'myDateTime'
,CAST(@DateTime AS DATETIME2(3)) AS 'myDateTimeWithPrecision'
,CAST(@DateTime AS DATE) AS 'myDate'
,CAST(@DateTime AS TIME) AS 'myTime'
,CAST(@DateTime AS TIME(3)) AS 'myTimeWithPrecision'
3) Menggunakan tipe data karakter panjang tetap
DECLARE @myDateTime NVARCHAR(20) = CONVERT(NVARCHAR, @DateTime, 120);
DECLARE @myDate NVARCHAR(10) = CONVERT(NVARCHAR, @DateTime, 120);
SELECT
@myDateTime AS 'myDateTime'
,@myDate AS 'myDate'