tsql - How to get DateTime formated -


in sql server how can current date time 12:00 am. getdate() current date , time.i need have datetime formated this:

2011-02-04 12:00 or 2011-02-04 00:01

thanks

you first need know how time want. called "flooring" date time. see example:

--floor datetime  select '0 none',  getdate()                                                                   -- none    2008-09-17 12:56:53.430 union select '1 second',dateadd(second,datediff(second,'2000-01-01',getdate()),'2000-01-01')  -- second: 2008-09-17 12:56:53.000 union select '2 minute',dateadd(minute,datediff(minute,0,getdate()),0)                        -- minute: 2008-09-17 12:56:00.000 union select '3 hour',  dateadd(hour,datediff(hour,0,getdate()),0)                            -- hour:   2008-09-17 12:00:00.000 union select '4 day',   dateadd(day,datediff(day,0,getdate()),0)                              -- day:    2008-09-17 00:00:00.000 union select '5 month', dateadd(month,datediff(month,0,getdate()),0)                          -- month:  2008-09-01 00:00:00.000 union select '6 year',  dateadd(year,datediff(year,0,getdate()),0)                            -- year:   2008-01-01 00:00:00.000 order 1 print' ' print 'note when flooring second, arithmetic overflow if use 0. pick known value guaranteed lower datetime attempting floor' print 'this uses date less given date, there no arithmetic overflow' select '1 second',dateadd(second,datediff(second,dateadd(day,datediff(day,0,getdate()),0)-1,getdate()),dateadd(day,datediff(day,0,getdate()),0)-1)  -- second: 2008-09-17 12:56:53.000 

once floor it, use 1 of flavors of convert() format like:

this format want, without changing time:

select convert(char(10), getdate(), 121)+' '+ltrim(right(convert(varchar(30), getdate(), 100),7)) 

output:

------------------ 2011-02-04 7:19am 

to format , set time want:

select convert(char(10),dateadd(day,datediff(day,0,getdate()),0), 121)+' '+ltrim(right(convert(varchar(30), dateadd(day,datediff(day,0,getdate()),0), 100),7)) 

output:

------------------ 2011-02-04 12:00am 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -