?
Solved

Creating a view with a Decimal column

Posted on 2004-04-30
6
Medium Priority
?
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 7

Accepted Solution

by:
Lori99 earned 2000 total points
ID: 10959428
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
ID: 10959473
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
ID: 10959512
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 26

Expert Comment

by:Hilaire
ID: 10959569
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
ID: 10960240
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
ID: 10960273
You're welcome, corwin_ranger.  Glad to help.  Thanks for the points!
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Suggested Courses

801 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