| Author |
Topic |
|
IBoonZ
Yak Posting Veteran
53 Posts |
Posted - 2009-03-13 : 09:01:04
|
| Hi:)I wonder, can someone pls give me the TSQL code, for following problem.I have a Date like : 2007-01-19 11:25:32.093But I only need the 2007-01-19.I know its something with Datediff, but I am a beginner :)Ty in advanced |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-03-13 : 09:07:21
|
| [code]SELECT Dateadd(dd,0,Datediff(dd,0,'2007-01-19 11:25:32.093')) [/code] |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-13 : 09:07:30
|
| [code]select dateadd(dd,datediff(dd,0,current_timestamp),0)[/code] |
 |
|
|
heavymind
Posting Yak Master
115 Posts |
Posted - 2009-03-13 : 09:08:16
|
| select convert(varchar(10), getdate(), 110)Thanks, VadymMCITP DBA 2005/2008Chief DBA at http://www.db-staff.com |
 |
|
|
darkdusky
Aged Yak Warrior
591 Posts |
Posted - 2009-03-13 : 09:15:25
|
| heavymind - that gives date - I think you meant select convert(varchar(5), getdate(), 114) --truncated to show just hours and minutesselect convert(varchar(12), getdate(), 114) --full timehttp://www.sql-server-helper.com/tips/date-formats.aspx |
 |
|
|
heavymind
Posting Yak Master
115 Posts |
Posted - 2009-03-13 : 09:20:32
|
quote: Originally posted by darkdusky heavymind - that gives date - I think you meant select convert(varchar(5), getdate(), 114) --truncated to show just hours and minutesselect convert(varchar(12), getdate(), 114) --full timehttp://www.sql-server-helper.com/tips/date-formats.aspx
quote: But I only need the 2007-01-19.
Thanks, VadymMCITP DBA 2005/2008Chief DBA at http://www.db-staff.com |
 |
|
|
darkdusky
Aged Yak Warrior
591 Posts |
Posted - 2009-03-13 : 10:32:42
|
| My mistake! |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2009-03-13 : 10:42:37
|
| The dateadd variants given earlier are more efficient. Use them.________________________________________________If it is not practically useful, then it is practically useless.________________________________________________ |
 |
|
|
darkdusky
Aged Yak Warrior
591 Posts |
Posted - 2009-03-13 : 11:18:47
|
| Blindman - I tested the 2 methods and convert is faster. I tried looping through outputing SELECT, INSERT to new table and populating a variable.CONVERT is faster every time. Try it yourself:declare @D datetimeset @D = getdate()SET nocount ON DECLARE @rows INT , @row INT, @count INT , @Str varchar (max)SELECT @rows = 10000000SELECT @row = 0 SELECT @count = 0 set @Str=''WHILE @row < @rows BEGIN Set @Str= convert(varchar(10), @D,110) --38 seconds --Set @Str= Dateadd(dd,0,Datediff(dd,0,@D)) --40 seconds SELECT @row = @row + 1 END SELECT @Str |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2009-03-13 : 12:08:20
|
<Sigh.....>That is because you are doing an additional implicit convert when you are assigning a valid datetime value to a string variable. Try this, and you will get very different results:declare @D datetimeset @D = getdate()SET nocount ONDECLARE @rows INT , @row INT, @count INT , @NewDate datetimeSELECT @rows = 10000000SELECT @row = 0SELECT @count = 0WHILE @row < @rowsBEGINSet @NewDate= convert(varchar(10), @D,110) --36 seconds--Set @NewDate = Dateadd(dd,0,Datediff(dd,0,@D)) --33 secondsSELECT @row = @row + 1END Accurate results require accurate tests.________________________________________________If it is not practically useful, then it is practically useless.________________________________________________ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
darkdusky
Aged Yak Warrior
591 Posts |
Posted - 2009-03-13 : 12:22:38
|
| Blindman you are right though I applied your logic and did not force the varchar back into a datetime either. So to be fair I put the CONVERT into a varchar and the DATEADD into a datetime.You still win (grrr). The times on my PC are different but the margin closed from 22sec(Convert): 14sec(DATEADD) to 17sec(Convert) : 14sec(DATEADD) declare @D datetimeset @D = getdate()SET nocount ONDECLARE @rows INT , @row INT, @count INT , @NewDate datetime, @NewVar varchar (50)SELECT @rows = 10000000SELECT @row = 0SELECT @count = 0WHILE @row < @rowsBEGINSet @NewVar= convert(varchar(10), @D,110) --17 seconds--Set @NewDate = Dateadd(dd,0,Datediff(dd,0,@D)) --14 secondsSELECT @row = @row + 1END |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2009-03-13 : 12:26:17
|
| Granted, there is not a huge difference.________________________________________________If it is not practically useful, then it is practically useless.________________________________________________ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-03-13 : 12:30:31
|
In the link above Itzik showed the DATEADD/DATEDIFF is three times faster than CONVERT. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|