SQL日期时间转为字符串,如20111214084541997
如: select GETDATE()
结果:2012-04-18 15:24:27.920
如何转化成17位字符,类似20111214084541997呢!
请高手指点!
另附带请教:20111214084541997 如何转成 2012-04-18 15:24:27.920 ?
[解决办法]
SELECT REPLACE(REPLACE(REPLACE(REPLACE(convert(varchar(50),GETDATE(),121),'-',''),':',''),' ',''),'.','')
[解决办法]
字符串转化到时间格式,先创建个函数,然后调用函数就行了
CREATE FUNCTION ChangeStrToDate(@Str VARCHAR(100)) RETURNS VARCHAR(100) AS BEGIN DECLARE @Date VARCHAR(10) DECLARE @Time VARCHAR(100) DECLARE @Month VARCHAR(10) DECLARE @Min VARCHAR(10) SET @Date = LEFT(@Str,8) SET @Time = RIGHT(@Str,9) SET @Month = SUBSTRING(@Date,5,2) SET @Date = STUFF(@Date,5,2,'-' + @Month + '-') SET @Min = SUBSTRING(@Time,3,2) SET @Time = STUFF(STUFF(@Time,3,2,':' + @Min + ':'),9,0,'.') RETURN @Date + ' ' + @Time END GO SELECT DBO.ChangeStrToDate('20120418045858977')