Here is the logic to create a date using two dates:
For eg:
We have 2 dates Tdate1 and Tdate2. And we want to create a new datetime using these 2 dates, in which we will take date part from Tdate1 and time part from Tdate2. So we can do this using below query:
Convert(datetime, CONVERT(VARCHAR(500), Tdate1, 110)
+ ' '
+ CONVERT(VARCHAR(50), Tdate2, 108) )
Compare dates :
if we have to check interval(day,month,year,min,hour) between 2 dates than we can use logic defined below:
MONTH:
Tdate1: 7 June 2013 00:00:00
Tdate2: 5 July 2013 00:00:00
select 'Difference',DATEDIFF(month, convert(datetime, Tdate1,Tdate2)
Result: Difference,1
MONTH:(-ve)
Tdate1: 7 June 2013 00:00:00
Tdate2: 5 May 2013 00:00:00
select 'Difference',DATEDIFF(month, convert(datetime, Tdate1,Tdate2)
Result: Difference,-1
YEAR:
Tdate1: 7 June 2013 00:00:00
Tdate2: 5 July 2013 00:00:00
select 'Difference',DATEDIFF(Year, convert(datetime, Tdate1,Tdate2)
Result: Difference,0
HOUR:
Tdate1: 7 June 2013 00:00:00
Tdate2: 7 June 2013 05:06:07
select 'Difference',DATEDIFF(Hour, convert(datetime, Tdate1,Tdate2)
Result: Difference,5
MINUTE:
Tdate1: 7 June 2013 00:00:00
Tdate2: 7 June 2013 05:06:07
select 'Difference',DATEDIFF(Minute, convert(datetime, Tdate1,Tdate2)
Result: Difference,6
SECOND:
Tdate1: 7 June 2013 00:00:00
Tdate2: 7 June 2013 05:06:07
select 'Difference',DATEDIFF(Second, convert(datetime, Tdate1,Tdate2)
Result: Difference,7
Let me know if i am missing anything or we have to make any change in this Post.
Happy Living....
Happy Coding.....
Happy Concepts.....
For eg:
We have 2 dates Tdate1 and Tdate2. And we want to create a new datetime using these 2 dates, in which we will take date part from Tdate1 and time part from Tdate2. So we can do this using below query:
Convert(datetime, CONVERT(VARCHAR(500), Tdate1, 110)
+ ' '
+ CONVERT(VARCHAR(50), Tdate2, 108) )
Compare dates :
if we have to check interval(day,month,year,min,hour) between 2 dates than we can use logic defined below:
MONTH:
Tdate1: 7 June 2013 00:00:00
Tdate2: 5 July 2013 00:00:00
select 'Difference',DATEDIFF(month, convert(datetime, Tdate1,Tdate2)
Result: Difference,1
MONTH:(-ve)
Tdate1: 7 June 2013 00:00:00
Tdate2: 5 May 2013 00:00:00
select 'Difference',DATEDIFF(month, convert(datetime, Tdate1,Tdate2)
Result: Difference,-1
YEAR:
Tdate1: 7 June 2013 00:00:00
Tdate2: 5 July 2013 00:00:00
select 'Difference',DATEDIFF(Year, convert(datetime, Tdate1,Tdate2)
Result: Difference,0
HOUR:
Tdate1: 7 June 2013 00:00:00
Tdate2: 7 June 2013 05:06:07
select 'Difference',DATEDIFF(Hour, convert(datetime, Tdate1,Tdate2)
Result: Difference,5
MINUTE:
Tdate1: 7 June 2013 00:00:00
Tdate2: 7 June 2013 05:06:07
select 'Difference',DATEDIFF(Minute, convert(datetime, Tdate1,Tdate2)
Result: Difference,6
SECOND:
Tdate1: 7 June 2013 00:00:00
Tdate2: 7 June 2013 05:06:07
select 'Difference',DATEDIFF(Second, convert(datetime, Tdate1,Tdate2)
Result: Difference,7
Let me know if i am missing anything or we have to make any change in this Post.
Happy Living....
Happy Coding.....
Happy Concepts.....
No comments:
Post a Comment