Solved

Creating a view with a Decimal column

Posted on 2004-04-30
6
300 Views
Last Modified: 2006-11-17
I'm working with a database that logs user activity.  The database was created by the application that does the logging.  One of the fields is a VARCHAR field that contains only numeric data.  It represents the number of secends a user was logged on dring a session.

I'm trying to create a view that I'll report against.  In this view, I'm including the user name, the time the event was logged, the original elapsed time (in seconds) and a field calculated from elapsed time to give me the elapsed time in minutes (elapsed_time/60).  The problem I have is that the calculated field is created as an integer, and therefore the result is rounded.  I expect that I'll be using the data to provide a roll-up summary, so the rounding errors can become significant over time.  I'd like to know how I might make this calculated field a decimal field.  Am I approaching this correctly?

Below is the SQL code that I used to creat the view as it is now.  Any help would be appreciated.


USE CiscoACS


go
DROP VIEW time_detail
go
CREATE VIEW time_detail
AS
SELECT LoggedAt,User_Name,elapsed_time,(elapsed_time/60) AS elapsed_minutes
FROM dbo.tacacsAccounting
WHERE (elapsed_time is NOT NULL)
0
Comment
Question by:corwin_ranger
6 Comments
 
LVL 7

Accepted Solution

by:
Lori99 earned 500 total points
Comment Utility
How about this.

CREATE VIEW time_detail
AS
SELECT LoggedAt,User_Name,elapsed_time,convert(decimal(10,2),elapsed_time)/60 AS elapsed_minutes
FROM dbo.tacacsAccounting
WHERE (elapsed_time is NOT NULL)

You would need to change the decimal field definition to whatever is appropriate for your field.
0
 
LVL 10

Expert Comment

by:Shailesh15
Comment Utility
Since it is varchar You should be using...

 ROUND(CAST(elapsed_time AS float) / 60, 2) AS  elapsed_minutes
0
 
LVL 34

Expert Comment

by:arbert
Comment Utility
Of course, the use of CAST/CONVERT if the varchar data ends up having non-numeric data in the column.  You should take that into account and maybe wrap an ISNUMERIC function inside the cast/convert.

Brett
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 26

Expert Comment

by:Hilaire
Comment Utility
Wouldn't a single field with hh:mm:ss format fit your needs better ?

SELECT LoggedAt,User_Name,elapsed_time,
select convert(varchar,dateadd(second, cast(elapsed_time as int), 0),108) as elapsed_hhmiss
FROM dbo.tacacsAccounting
WHERE (elapsed_time is NOT NULL)

can be truncated to mi:ss if you prefer

SELECT LoggedAt,User_Name,elapsed_time,
select right(convert(varchar,dateadd(second, cast(elapsed_time as int), 0),108),5) as elapsed_miss
FROM dbo.tacacsAccounting
WHERE (elapsed_time is NOT NULL)

HTH

Hilaire
0
 

Author Comment

by:corwin_ranger
Comment Utility
Thanks to everyone for all the great info.  I'm new tothe whole development thing and even newer to the SQL thing.  I'm really just now getting a handle on what it CAN do, much less how to do it all.
0
 
LVL 7

Expert Comment

by:Lori99
Comment Utility
You're welcome, corwin_ranger.  Glad to help.  Thanks for the points!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now