Solved

Creating a view with a Decimal column

Posted on 2004-04-30
6
303 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

807 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