Solved

# Ho to check the Precision of a decimal field

Posted on 2013-06-24
471 Views
Hi there

I have a column, that is set to

decimal(18,0)

I need to check the (18,0) bit of it, and if it is (18,0) run some SQL else don't run it

so

if field

share is (18,0) do something
else
don't do anything

thanks
0
Question by:mousemat24
[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
1 Comment

LVL 11

Accepted Solution

Louis01 earned 500 total points
ID: 39270961
To select column type and precision and scale:
``````select t.name as table_name, c.name as column_name, y.name as [type_name], c.precision, c.scale
from sys.tables t
inner join sys.columns c
on t.[object_id] = c.[object_id]
inner join sys.types y
on c.system_type_id = y.system_type_id
where t.name = 'your_table_name_here'
and c.name = 'your_column_name_here'
--   and y.name = 'decimal'
--   and c.precision = 18
--   and c.scale = 0
``````

To use in IF
``````IF ((select isnull(count(*), 0)
from sys.tables t
inner join sys.columns c
on t.[object_id] = c.[object_id]
inner join sys.types y
on c.system_type_id = y.system_type_id
where t.name = 'your_table_name_here'
and c.name = 'your_column_name_here'
and y.name = 'decimal'
and c.precision = 18
and c.scale = 0) = 0)
BEGIN
Print 'Do something'
END
ELSE
BEGIN
Print 'Do something else'
END
``````
0

## Featured Post

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
###### Suggested Courses
Course of the Month6 days, 18 hours left to enroll