473,806 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Remove Seconds from Datetime: How To?

I want to create some volume metrics, and I need to produce a report
that shows how many rows were inserted by minute into a particular
table.

This is a candidate for a simple GROUP BY select, except that that the
INSERT_DT column in the table goes down to the second. I want to
GROUP BY at the minute level. I don't know an easy way to simply
truncate the seconds from the datetime. Extracting the time alone
won't work, because I want to compare minutes from different dates
(e.g. I am not interested in finding out if 12:47 of each day is the
highest volume minute, but rather that 23:14 of a particular day had
the highest number of inserts).

I did something ugly that works, but there has to be a better way.
Here's what I used:

cast(datename(y ear,INSERT_DT)+ '-'+datename(mont h,INSERT_DT)
+'-'+datename(day, INSERT_DT)+' '+datename(hour ,INSERT_DT)
+':'+datename(m inute,INSERT_DT ) as datetime)

It seems very strange to pull the components out of the original
datetime column, re-assemble them (sans minutes) with the stupid
dashes, spaces and colons into a string, and then re CAST them back
into a datetime.

What is the simpler way?

Thanks,

Bill
Oct 26 '08 #1
6 45390
"bill" <bi**********@g mail.comwrote in message
news:f7******** *************** ***********@l42 g2000hsc.google groups.com...
>I want to create some volume metrics, and I need to produce a report
that shows how many rows were inserted by minute into a particular
table.

This is a candidate for a simple GROUP BY select, except that that the
INSERT_DT column in the table goes down to the second. I want to
GROUP BY at the minute level. I don't know an easy way to simply
truncate the seconds from the datetime. Extracting the time alone
won't work, because I want to compare minutes from different dates
(e.g. I am not interested in finding out if 12:47 of each day is the
highest volume minute, but rather that 23:14 of a particular day had
the highest number of inserts).

I did something ugly that works, but there has to be a better way.
Here's what I used:

cast(datename(y ear,INSERT_DT)+ '-'+datename(mont h,INSERT_DT)
+'-'+datename(day, INSERT_DT)+' '+datename(hour ,INSERT_DT)
+':'+datename(m inute,INSERT_DT ) as datetime)

It seems very strange to pull the components out of the original
datetime column, re-assemble them (sans minutes) with the stupid
dashes, spaces and colons into a string, and then re CAST them back
into a datetime.

What is the simpler way?

Thanks,

Bill
CAST(INSERT_DT AS SMALLDATETIME)

--
David Portas
Oct 26 '08 #2
David Portas (RE************ *************** *@acm.org) writes:
CAST(INSERT_DT AS SMALLDATETIME)
Or CONVERT(char(16 ), INSERT_DT, 126)

These two are not equivalent. David's solution will round, mine will
truncate:

declare @d datetime
select @d = '20081212 23:00:45'
select convert(char(16 ), @d, 121)
select convert(smallda tetime, @d)
If you want the data to be surrected as a datetime value, change 121
to 126 in my solution.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Oct 26 '08 #3
On Sun, 26 Oct 2008 12:27:08 -0700 (PDT), bill wrote:

(snip)
>It seems very strange to pull the components out of the original
datetime column, re-assemble them (sans minutes) with the stupid
dashes, spaces and colons into a string, and then re CAST them back
into a datetime.

What is the simpler way?
Hi Bill,

In addition to the methods presented by David and Erland, here's one
more:

DATEADD(minute,
DATEDIFF(minute , '20080101', INSERT_DT),
'20080101');

This method can easlliy be adapted to strip off other parts of the date.
For instance, change "minute" to "hour" (twice) to strip off minutes and
get the last whole hour.

The principle used is to calculate the number of minutes that have
passed since some base date/time (in this case: midnight, Jan. 1st 2008)
and then add that number back to the same base date/time. You can use
any base date you like, just make sure to avoid overflows (for instance,
the number of seconds since 1900 is more than the maximum integer
stores).

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Oct 27 '08 #4
Thanks everyone for the suggestions. It will save me time in the
future. Where can I find a comprehensive list of the Functions, their
syntax and arguments in SQL 2008? I tried googling "SQL Server
Function List" and the like without much luck. Couldn't find such a
list in BOL either. I'm sure it's there, and I am just using the
wrong search terms.

For clarity sake, I am looking for a something like this:
http://www.psoug.org/reference/builtin_functions.html only for SQL.
These things are all over the web for Oracle, but must be indexed
under diferent search terms for SQL Server.

Thanks,

Bill
Oct 29 '08 #5
In SQL Server 2008 Books Online (August 2008) there is a page titled
Functions (Transact-SQL). That page lists a variety of broad
categories of functions, each with a link to another page for the list
of functions in that category.

Roy Harvey
Beacon Falls, CT

On Wed, 29 Oct 2008 13:24:20 -0700 (PDT), bill
<bi**********@g mail.comwrote:
>Thanks everyone for the suggestions. It will save me time in the
future. Where can I find a comprehensive list of the Functions, their
syntax and arguments in SQL 2008? I tried googling "SQL Server
Function List" and the like without much luck. Couldn't find such a
list in BOL either. I'm sure it's there, and I am just using the
wrong search terms.

For clarity sake, I am looking for a something like this:
http://www.psoug.org/reference/builtin_functions.html only for SQL.
These things are all over the web for Oracle, but must be indexed
under diferent search terms for SQL Server.

Thanks,

Bill
Oct 30 '08 #6
Try:

Cast(INSERT_DT As Smalldatetime)
"bill" <bi**********@g mail.comwrote in message
news:f7******** *************** ***********@l42 g2000hsc.google groups.com...
>I want to create some volume metrics, and I need to produce a report
that shows how many rows were inserted by minute into a particular
table.

This is a candidate for a simple GROUP BY select, except that that the
INSERT_DT column in the table goes down to the second. I want to
GROUP BY at the minute level. I don't know an easy way to simply
truncate the seconds from the datetime. Extracting the time alone
won't work, because I want to compare minutes from different dates
(e.g. I am not interested in finding out if 12:47 of each day is the
highest volume minute, but rather that 23:14 of a particular day had
the highest number of inserts).

I did something ugly that works, but there has to be a better way.
Here's what I used:

cast(datename(y ear,INSERT_DT)+ '-'+datename(mont h,INSERT_DT)
+'-'+datename(day, INSERT_DT)+' '+datename(hour ,INSERT_DT)
+':'+datename(m inute,INSERT_DT ) as datetime)

It seems very strange to pull the components out of the original
datetime column, re-assemble them (sans minutes) with the stupid
dashes, spaces and colons into a string, and then re CAST them back
into a datetime.

What is the simpler way?

Thanks,

Bill

Nov 2 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3250
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert value to a time value' error. It's related to glibc returning an error to a pre-1970 date, I think. My question: /how/ do I go through the Python direction I've created to remove the pre-1970 date objects? Ideally, I would be able to iterate...
3
1664
by: bredal Jensen | last post by:
Hello gurus, I'm building a small booking system and i have come accross quiet a tedious pitfall. "I need to make sure that people do not book for tomorrow when todays time is greater or equal to 11."
5
2715
by: Salad | last post by:
I wrote a routine for somebody yesterday. When I compare datStart to datEnd in the DoWhile comamnd, when the date/times match they don't. For example, 1:00:00 PM does not match 1:00:00 PM. I guess this may be due to precision, or imprecision, of the value stored in a date field. IOW, date/time fields are accurate to the second but may or may not be accurate if you add or subtract some time from it. If you have an idea why the...
3
12831
by: nriesch | last post by:
In the documentation, the "Second" property of class DateTime is a value between 0 and 59. In UTC time, approximately every year of so, a leap second is added at 00:00:00 UTC, so as to account for the irregular rotation of the earth, which is slowing down a little bit. So, a minute can contains 61 seconds ( range 0..60 ). Besides, the NTP protocol takes leap second into account, and UTC time also.
1
12291
by: Prabhu | last post by:
Hi, Can any one help me getting Seconds elapsed from a given date time to current time in .Net?. for e.g. Suppose assume a given time date time "01-Jan-2000 00:00:00", I should get the number of seconds elapsed from the given date time to current date time.
3
1488
by: Hrvoje Voda | last post by:
How to put a dateTime value back to Null! I have a table in database in witch I put a dateTime values. I would like to delete that value so that it becomes null again. Hrcko
3
4239
by: Andrew S. Giles | last post by:
Hello, I am importing a flat text file, and putting it into a datagrid for display on a form. Currently the users have their dates and times seperated. I have two fields, therefore in the datatable feeding the datagrid control. Both are of the DateTime Type. How do I get the time field to display only the Time, and not the date, which is apparently the default.
2
1247
by: Jon | last post by:
I am doing the following: Me.lblDTCompleted.Text = DateTime.Parse(drTemp.Item(_DATECOMP)).ToString + strTZ where drTemp.Item(_DATECOMP) is datetime field in SQL server. I want to display the date correctly formatted for the localized area, but without the seconds displayed. Using the above correctly formats it, but leaves the seconds on. How can I format it correctly for the region and remove the seconds at the same time? I tried...
7
13362
by: TheLostLeaf | last post by:
DateTime tTime = DateTime.Now; ------------------------------------------------------------------------------------------- tTime returns "1:59:00 PM" it never returns seconds. Database field is SQL datetime or smalldatetime.
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10620
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.