Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

GetDateTime Function Expand / Collapse
Author
Message
Posted Thursday, July 23, 2009 10:56 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, March 21, 2013 10:59 AM
Points: 22, Visits: 75
Comments posted to this topic are about the item GetDateTime Function
Post #758439
Posted Thursday, July 30, 2009 7:45 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, June 3, 2013 9:38 PM
Points: 139, Visits: 148
Hi,

Try this instead, works just as well. Basically I insert a space between the Date and the time and a : between the hours and the minutes


declare @DtStr varchar(20)
set @DtStr='200907011020'
select convert(datetime,substring(@DtStr,1,8)+' '+substring(@DtStr,9,2)+':'+substring(@DtStr,11,2))


Post #762408
Posted Thursday, July 30, 2009 9:58 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:02 AM
Points: 35,267, Visits: 31,761
Ummm... CONVERT is relatively expensive and so are all of the substrings not to mention the use of a UDF...

SELECT CAST(STUFF(STUFF('200907111400',11,0,':'),9,0,' ') AS DATETIME)


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #762545
Posted Thursday, July 30, 2009 11:18 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, March 21, 2013 10:59 AM
Points: 22, Visits: 75
tks to all. both suggestions are good.
Post #762611
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse