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
 Cast and convert time.

Author  Topic 

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-28 : 15:27:12
In my GCSOSUBJECTS table, i need to do an update to the SVCTIME column based on data in the Servetime column. SVCTIME is a datetime value (column is all NULL at this time). The values in Servetime look like:
1654
1021
1628
2310
all represent time of day. How do i (or do i even) cast those values in my update (Update GCSOSUBJECTS set SVCTIME=CAST(ServeTime
as DATETIME)? <-----obviously not working

Thanks

j

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-28 : 15:29:52
My error reads:
Msg 242, Level 16, State 3, Line 1
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.

when using

UPDATE gcsosubjects SET ROLE='R', subjecttype='P', comments=generalcomments+'. '+typeservicecomments,
DBA=substring(CorpOrDBA,1,40), entered=servedate, feesearned=servicecost, svcdate=servedate, servedby=SERVER, svctime=CAST(ServeTime AS DATETIME)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-28 : 15:30:23
convert(datetime,STUFF(Servetime,3,0,':'))

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-28 : 15:31:16
thank you
Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-28 : 15:33:13
Ok, plz clarify what the "stuff" is doing. i can see that it is being told to put the ":" in the 3rd position, but what is the "0," for?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-28 : 15:35:26
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-28 : 15:37:41
Dude, Do you know what books online is? If you have SQL Server Client Tools Installed, it will be under programs>SQL Server 2008R@>Documentation and Tutorials



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-28 : 15:41:48
Here:



CREATE TABLE #t(ServerTime int)
GO

INSERT INTO #t(ServerTime)
SELECT 1654 UNION ALL
SELECT 1021 UNION ALL
SELECT 1628 UNION ALL
SELECT 2310
GO

SELECT ServerTime, CONVERT(datetime,STUFF(Servertime,3,0,':'))
FROM #t
GO

DROP TABLE #t
GO



Results"



ServerTime
----------- -----------------------
1654 1900-01-01 16:54:00.000
1021 1900-01-01 10:21:00.000
1628 1900-01-01 16:28:00.000
2310 1900-01-01 23:10:00.000

(4 row(s) affected)




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-28 : 15:45:09
quote:
Originally posted by WJHamel

Ok, plz clarify what the "stuff" is doing. i can see that it is being told to put the ":" in the 3rd position, but what is the "0," for?


third argument states number of characters it needs to delete from string
i've given 0 as we dont need to delete anything but just needs to insert ':' at 3rd position

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-03-28 : 15:58:32
thanks again. much appreciated.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-28 : 16:08:33
welcome
As suggested by Brett...you can get these information easily by refering to books online

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -