Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 grouping datetime by single days

Author  Topic 

sqlchiq
Posting Yak Master

133 Posts

Posted - 2008-08-14 : 16:27:10
I have a bunch of entries that are like


8/8/2008 5:45:33 PM
8/8/2008 5:45:41 PM
8/8/2008 5:45:50 PM
8/8/2008 5:45:41 PM
8/8/2008 5:45:59 PM

But are essentially 8/8/2008 (in nvarchar datatype)

I was wondering if there was a simple way to change all of it into

smalldatetime

8/8/2008 0:00:00

or whatever

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-08-14 : 16:57:48
dateadd(dd, datediff(dd, 0, YourDateHere), 0)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-18 : 04:26:01
dateadd(dd, datediff(dd, 0, cast(YourDateHere as datetime)), 0)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-18 : 04:36:22
quote:
Originally posted by madhivanan

dateadd(dd, datediff(dd, 0, cast(YourDateHere as datetime)), 0)

Madhivanan

Failing to plan is Planning to fail


just a doubt wont the nvarchar value be implicitly casted? is there a need for explicit casting?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-18 : 05:08:32
quote:
Originally posted by visakh16

quote:
Originally posted by madhivanan

dateadd(dd, datediff(dd, 0, cast(YourDateHere as datetime)), 0)

Madhivanan

Failing to plan is Planning to fail


just a doubt wont the nvarchar value be implicitly casted? is there a need for explicit casting?


Once I got convertion failed error but I am not able to reproduce it now

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-18 : 05:24:37
quote:
Originally posted by madhivanan

quote:
Originally posted by visakh16

quote:
Originally posted by madhivanan

dateadd(dd, datediff(dd, 0, cast(YourDateHere as datetime)), 0)

Madhivanan

Failing to plan is Planning to fail


just a doubt wont the nvarchar value be implicitly casted? is there a need for explicit casting?


Once I got convertion failed error but I am not able to reproduce it now

Madhivanan

Failing to plan is Planning to fail


Ok. just checked as i didnt get one
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-18 : 06:44:13
UPDATE Table1
SET Col1 = LEFT(Col1, CHARINDEX(' ', Col1)) + '00:00:00 AM'




E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-08-18 : 08:36:08
quote:
Originally posted by visakh16
just a doubt wont the nvarchar value be implicitly casted? is there a need for explicit casting?


I personally like to do the explicit casts/converts if I want the result in a specific datatype. One reason is I don't want to count different/future versions to behave the same way, and another reason is for the benefit of other developers so they can easily see what my intentions were. It is not necessary but it is a convention that I started using. When I remember to :)

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -