Removing and Manipulating Time portion from DateTime in SQLServer

One of the common transformation we would have to do as part of ETL would be manipulating Time portion. Please see some of below code, which would help to resolve some of the time related transformation

Case1 - Getting only Date portion from DateTime

/* Removing Time portion from DateTime */ 

Select
-- Standard date with time
 getdate() As DateWithTime,
-- Returns Date Portion Only

 dateadd(d, 0, datediff(d, 0, getdate())) As DateOnly


Result as below:















Case2 - /* Removing Seconds from DataTime */

/* Removing Seconds from DataTime */
declare @t datetime
set @T = GETDATE()
SELECT @T as Original
,DATEadd(ss,-datepart(ss,@t),@t) as wihtout_sec -- if no milisecs
,DATEADD(mi,DateDiff(mi,0,@t),0) as OneMore_Variation









Case3 - We want to Keep Hours but remove Minutes and Seconds from DateTime

/* Keep Hours but remove Minutes and Sec from DateTime */
select Dateadd( hh, DATEDIFF(hh,0,getdate()),0) AS DateWithHours_No_MinutesAndSeconds










Comments

Popular posts from this blog

Calculating Age of the person in T-SQL

How to Troubleshoot Subscription issue in Reporting Services

MDX Queries - Current Day - Month and Year