p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

sql_language thread: Splitting a Date/Time field


Message #1 by "Nathan Crawford" <nathan.crawford@f...> on Tue, 12 Dec 2000 17:26:46 +0900
Hmmm... just happened to do that earlier this week...

declare @date varchar(8),
        @time varchar(4),
        @year_part varchar(4),
        @month_part varchar(2),
        @day_part varchar(2),
        @hour_part varchar(2),
        @min_part varchar(2)

select @year_part = convert(varchar(4),datepart(yy,getdate()))
       if char_length(convert(varchar(2),datepart(mm,getdate()))) = 1 begin
           select @month_part = '0' + convert(varchar(2),datepart(mm,getdate())) end
           else select @month_part = convert(varchar(2),datepart(mm,getdate()))

       if char_length(convert(varchar(2),datepart(dd,getdate()))) = 1 begin
           select @day_part = '0' + convert(varchar(2),datepart(dd,getdate())) end
           else select @day_part = convert(varchar(2),datepart(dd,getdate()))

select @date = @year_part + @month_part + @day_part

       if char_length(convert(varchar(2),datepart(hh,getdate()))) = 1 begin
           select @hour_part = '0' + convert(varchar(2),datepart(hh,getdate())) end
           else select @hour_part = convert(varchar(2),datepart(hh,getdate()))

       if char_length(convert(varchar(2),datepart(mi,getdate()))) = 1 begin
           select @min_part = '0' + convert(varchar(2),datepart(mi,getdate())) end
           else select @min_part = convert(varchar(2),datepart(mi,getdate()))

select @time = @hour_part + @min_part

Kelly Rowe
Senior Engineer - DBA
Global Card Services, Inc.


---
You are currently subscribed to sql_language as: $subst('Recip.EmailAddr')
To unsubscribe send a blank email to leave-sql_language-$subst('Recip.MemberIDChar')@p2p.wrox.com

  Return to Index