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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 date and time

Author  Topic 

kallu
Starting Member

6 Posts

Posted - 2007-09-11 : 05:27:13
hi,
As per my project requirement ,i have to store only time format data ( Ex 6:00 AM).My table column data type is small date and time. plz help me.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-09-11 : 05:31:38

declare @d smalldatetime
set @d='6:00:00 AM'
select @d

Madhivanan

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

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2007-09-11 : 05:37:23
that won't work Madhi... this is still 2005

Store it as string type or int type in minutes



--------------------
keeping it simple...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-09-11 : 05:51:35
quote:
Originally posted by jen

that won't work Madhi... this is still 2005

Store it as string type or int type in minutes



--------------------
keeping it simple...


Well. I didnt see the Forum name

Madhivanan

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

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2007-09-11 : 05:53:16
does it work in 2008? Amazing!

--------------------
keeping it simple...
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-11 : 05:55:57
Why? Always use proper datatype.
Why even bother about FORMATTING in the database? It is the client, not database, that should format datetime value, not database.



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

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2007-09-11 : 05:59:20
it's all about user requirements
and customer is always right... so they think...
give them something better and they'd say... that's not what i asked for

--------------------
keeping it simple...
Go to Top of Page

kallu
Starting Member

6 Posts

Posted - 2007-09-11 : 06:07:00
is there any function that shows difference between two times. here time format Ex is : 6:00 Am - 8 :00 AM.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-11 : 06:10:00
Yes. DATEDIFF function.
http://www.sqlteam.com/article/datediff-function-demystified


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

kallu
Starting Member

6 Posts

Posted - 2007-09-11 : 06:16:18

thank you peso, i have small doubt

For example :
my table column data type is varchar , the data stored in this column as 6:00 Am format.

with explict cast is it possible get diffents between two times in mints
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-11 : 06:21:31
Yes you can, but all comparisons will be much harder than necessary!
How to sort, for example? 9:00 AM is earlier than 4:00 PM, but 4:00 PM will come first if sorted ascending!

You have to do ORDER BY CAST(Col1 AS SMALLDATETIME)
which means you force a table scan and you can't use any index for that column! Which means your query will be slower than necessary.

If you use SMALLDATETIME daattype, all sorts and other comparisons will happen as you think they would.


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

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-11 : 06:22:12
SELECT DATEDIFF(SECOND, CAST(Col1 AS SMALLDATETIME), CAST(Col2 AS SMALLDATETIME)) / 60.0 AS Minutes
FROM Table1



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

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2007-09-11 : 06:34:29
maybe he has a date field, since he has a time field, logically the time is irrelevant/useless if you store data on different dates without saving the date?

--------------------
keeping it simple...
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-11 : 06:43:35
Even worse approach.

You suggest he is having Date1, Time1 and Date and Time2 columns? Nice...

Now try to calculate the difference in minutes between to two times!

DATEDIFF(MINUTE, CAST(Date1 AS DATETIME) + CAST(Time1 AS DATETIME), CAST(Date2 AS DATETIME) + CAST(Time2 AS DATETIME))

Instead of s simple DATEDIFF(MINUTE, DateTime1, DateTime2)



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

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2007-09-11 : 06:49:47
, well he can't really expect to just have the time? even for scheduling purpose, you still need to store the date
unless....

he is actually refreshing the table on a daily basis just keeping one day's worth of whatever needs to have time stamped?

--no i'm not the devil's advocate but it might be a fun job

--------------------
keeping it simple...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-09-11 : 08:27:31
Note that OP specified My table column data type is small date and time.

Madhivanan

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

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-09-11 : 08:36:00
As Peso says, if you need to store just times, simply use a DateTime data type.

http://weblogs.sqlteam.com/jeffs/archive/2007/08/29/SQL-Dates-and-Times.aspx

Never, ever store data as a VARCHAR that has a better, more appropriate data type.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -