MS SQL Tips & Tricks

How to replace date part in datetime variable

The simpliest way of replacing the date in datetime variable without changeing the actual time is to do a trick as bellow:

declare @cDateTime datetime;
declare @cYear integer;
declare @cMonth integer;
declare @cDay integer;

set @cDateTime=getdate();
set @cYear = 2009;
set @cMonth = 1;
set @cDay = 1;

select  (@cDateTime-
	dateadd(dd,0, datediff(dd,0,@cDateTime)))+
	dateadd(month,((@cYear-1900)*12)+@cMonth-1,@cDay-1);	

See also


    Learn more tricks

    Add Comment

    Comments

    27th August 2021

    viagra ring

    Invorne

    30th August 2021

    Priligy 30 Mg O 60 Mg

    viagra blood pressure medication

    31st August 2021

    Propecia

    Gursefs

    4th September 2021

    Order Direct Legally Levaquin Website Saturday Delivery Overseas

    Propecia

    8th September 2021

    propecia timeline

    Gursefs

    8th September 2021

    https://buytadalafshop.com/ - buy cialis online with a prescription

    ceagods

    8th September 2021

    Cialis

    AwasmaB

    14th September 2021

    https://buylasixshop.com/ - Lasix

    nertoorne

    14th September 2021

    Lasix

    Huncalm

    20th September 2021

    Cephalexin Antibiotics

    buy priligy in the usa

    20th September 2021

    Acheter Baclofene Internet

    plaquenil cost walmart