Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Creating a view with a Decimal column

Posted on 2004-04-30
6
Medium Priority
?
311 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

609 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