Showing posts with label decimal. Show all posts
Showing posts with label decimal. Show all posts

Tuesday, March 27, 2012

Change Identify/Primary Key Column Data Type?

Hi ...

I've

taken over a project where the tables were created with

identity/primary key columns of type DECIMAL(12,0). The latest addition

to the project is to replicate data down to Pocket PC applications.

Replication requires that identify/primary key columns be of type

INT/BIGINT.

I've attempted to ALTER TABLE xxx ALTER COLUMN yyy

BIGINT; and it fails. Failed due to all the foreign key constraints that have been created. While it didn't give any error messages associated with the the field being the PRIMARY KEY - I'm assuming I may get that error as well.

I then did an ALTER TABLE xxx NOCHECK CONSTRAINT

ALL; for every table in the system to disable checking of foreign keys

and then attempted to alter the column to a bigint and it still failed.

How

can I change the column from Decimal to BIGINT - or do I have to create

new tables, import all the data, get rid of the original tables? Please

tell me I don't have to do the latter.

Thanks ...Unfortunately the only way is to recreate the table or drop & recreate the column. This would require removing the existing constraints (PKs, FKs) and recreating them. Alternatively, you can create a view on the table(s) that does the conversion and replicate those. But this may or may not work depending on your replication scheme and you will have to ask in the Replication newsgroup.

Thursday, March 22, 2012

Change decimal point

How can I change decimal point using SQL command ?
hi Ricardo,
Ricardo Luiz wrote:
> How can I change decimal point using SQL command ?
if you mean you want to alter a column in order to add additional
"precision" to a decimal type column, you can perform an ALTER TABLE ALTER
COLUMN statement like
SET NOCOUNT ON;
USE tempdb;
GO
CREATE TABLE dbo.test (
ID int NOT NULL PRIMARY KEY ,
d decimal (18,2) NOT NULL DEFAULT 0
);
INSERT INTO dbo.test VALUES ( 1 , 1.12 );
GO
PRINT 'fails inserting all decimals'
INSERT INTO dbo.test VALUES ( 2 , 1.123 );
SELECT * FROM dbo.test;
GO
ALTER TABLE dbo.test
ALTER COLUMN d decimal (18,4);
INSERT INTO dbo.test VALUES ( 3 , 1.123 );
SELECT * FROM dbo.test;
GO
DROP TABLE dbo.test;
if you like to modify the decimal separator, than you can not, as this
setting is by design and not dependent to the language associated to the
current login as for date formatting..
you can however cast the value to a varchar(n) and replace the separator
with your own costant, but this way you no longer have a decimal type result
but a varchar..
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.18.0 - DbaMgr ver 0.62.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply

Tuesday, March 20, 2012

change datatype of a column

what is the best (and fast) why to change a column's datatype from varchar to decimal ?
the table has 3 million records and the column is filled with data (no problem with converting the data to numeric).alter table tablename
alter column columnname float null|||tnx

Sunday, February 19, 2012

CCur

this document says that CCUR is supported in SSRS 2005; but I'm trying
to convert a float to money format-- so it gets 2 decimal places and a
dollar sign in front of it.
and i just can't get CCUR to work _AT_ALL_ in SSRS 2005.
http://msdn2.microsoft.com/en-us/library/ms157205.aspx
my actual code is
="The premium for your plan is " & Parameters!AdHoc_PremiumAmount.Value
& "."
I'm trying to do this:
="The premium for your plan is " &
CCUR(Parameters!AdHoc_PremiumAmount.Value) & "."
can anyone help me figure out what I'm doing wrong?
I've got to allow the end users to enter a premiumAmount as a
parameter; the possible datatypes are String, Integer, Float; etc...
I just want the end users to be able to enter 200 and then my CCUR
function should automagically convert it to '$ 200.00'
shouldnt these things just work out of the box?
-Aaronhello?
does anyone else concur?
I just wish that MS would start doing a decent job of keeping track of
things like this.
heres some helpful hints, microsoft
KEEP YOUR DATA IN A DATABASE INSTEAD OF 100,000 DIFFERENT COPIES OF
XML!
-Aaron
aaron.kempf@.gmail.com wrote:
> this document says that CCUR is supported in SSRS 2005; but I'm trying
> to convert a float to money format-- so it gets 2 decimal places and a
> dollar sign in front of it.
> and i just can't get CCUR to work _AT_ALL_ in SSRS 2005.
> http://msdn2.microsoft.com/en-us/library/ms157205.aspx
> my actual code is
> ="The premium for your plan is " & Parameters!AdHoc_PremiumAmount.Value
> & "."
> I'm trying to do this:
> ="The premium for your plan is " &
> CCUR(Parameters!AdHoc_PremiumAmount.Value) & "."
>
> can anyone help me figure out what I'm doing wrong?
> I've got to allow the end users to enter a premiumAmount as a
> parameter; the possible datatypes are String, Integer, Float; etc...
> I just want the end users to be able to enter 200 and then my CCUR
> function should automagically convert it to '$ 200.00'
> shouldnt these things just work out of the box?
> -Aaron|||yeah I need to do the same thing.. anyone have any ideas'
-Susie
On Oct 3, 5:19 pm, aaron.ke...@.gmail.com wrote:
> this document says that CCUR is supported in SSRS 2005; but I'm trying
> to convert a float to money format-- so it gets 2 decimal places and a
> dollar sign in front of it.
> and i just can't get CCUR to work _AT_ALL_ in SSRS 2005.
> http://msdn2.microsoft.com/en-us/library/ms157205.aspx
> my actual code is
> ="The premium for your plan is " & Parameters!AdHoc_PremiumAmount.Value
> & "."
> I'm trying to do this:
> ="The premium for your plan is " &
> CCUR(Parameters!AdHoc_PremiumAmount.Value) & "."
> can anyone help me figure out what I'm doing wrong?
> I've got to allow the end users to enter a premiumAmount as a
> parameter; the possible datatypes are String, Integer, Float; etc...
> I just want the end users to be able to enter 200 and then my CCUR
> function should automagically convert it to '$ 200.00'
> shouldnt these things just work out of the box?
> -Aaron

Sunday, February 12, 2012

Casting Int to Varchar

I want to cast an In to varchar with a specific number of decimal places. So 10 will come across as 10.00.
Is there an easy way of doing this?Would you be llooking for something like this?

Code:
------------------------------
select cast(cast(10 as money) as varchar(10))
select cast(cast(10 as numeric(12,4)) as varchar(10))
------------------------------|||I think this is what Paul was trying to show:

select cast(cast(id as decimal(5,2)) as varchar(10)) from table

Where id and table are defined by you. The decimal parameters would be determined by your maximum integer.

Casting decimal number

Hello,

When I declare a VB variable Dim s as Decimal,

I want to cast d like this :
1452,41
41,00
45,47
756544,04

Only with to digits after the ","

How can I perform this

Hi,

If my variable d=125,45111

How can I view d like 125,45 ?

|||you mean 2 digits after a "decimal" not a "comma" right?|||

You have two choices use place holder in strings and formatting or set precision and scale. Try the links below for details. Hope this helps.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcustomnumericformatstringsoutputexample.asp

http://support.microsoft.com/?kbid=892406

|||

Hi,

If you are asking how to mak this conversion using t-sql, you can use CAST function

You can run and test the result for CAST

DECLARE @.d FLOAT
SET @.d = 125.45111

SELECT @.d, CAST(@.d AS DECIMAL(10,2))

|||

Hi,

Thank I mean two digits after a decimal number ( 44,45 or 4,00 or 7888,01 )

I'm using VB.NET and my variable is declared like Dim x as decimal.

Thanks

Cast varchar to decimal

I am losing my hair...and my mind...
Is there any reason why I wouldn't be able to cast a varchar value of say
7.8 to decimal?
I have a whole bunch of lab results that come with a bunch of garbage in the
result column. I have stripped it away so that it is only a format
[1-x].[0-9]. I want to make it a number so I can identify High and low value
s
for each patient.
Am I missing something...besides my mind?
Thanks in advance,Don't see any problem in achieving what you want.
Can you post a sample to exactly understand what your issue is?
--
HTH,
SriSamp
Email: srisamp@.gmail.com
Blog: http://blogs.sqlxml.org/srinivassampath
URL: http://www32.brinkster.com/srisamp
"Greg" <Greg@.discussions.microsoft.com> wrote in message
news:CC2448DB-9147-4391-BBDA-7891A6E3294F@.microsoft.com...
>I am losing my hair...and my mind...
> Is there any reason why I wouldn't be able to cast a varchar value of say
> 7.8 to decimal?
> I have a whole bunch of lab results that come with a bunch of garbage in
> the
> result column. I have stripped it away so that it is only a format
> [1-x].[0-9]. I want to make it a number so I can identify High and low
> values
> for each patient.
> Am I missing something...besides my mind?
> Thanks in advance,
>
>|||Here is the code... pretty straight forward....
There is something still contained in the string that is messing up the
cast. (See raw data below code)
select top 7
patientid,
decodedvalue,
convert(varchar,replace(replace(replace(
decodedvalue,'
',''),'>',''),'%','')) value,
-- cast(convert(varchar,replace(replace(r
eplace(decodedvalue,'
',''),'>',''),'%','')) as float) value,
len(replace(replace(replace(decodedvalue
,' ',''),'>',''),'%','')) str_length
from
#diabetes_results
where
decodedvalue like '%.%'
order by
patientid
58 6.8 % 6.8 3
58 7.6 % 7.6 3
58 6.7 % 6.7 3
58 7.1 % 7.1 3
58 6.2 % 6.2 3
168 7.5 % 7.5 3
168 7.5 7.5 5
Note the length of '5' in the final record though it is obvious the length
should be 3. I trimmed the column of spaces but they remain.
Any thoughts?
Thanks in advance,
"SriSamp" wrote:

> Don't see any problem in achieving what you want.
> Can you post a sample to exactly understand what your issue is?
> --
> HTH,
> SriSamp
> Email: srisamp@.gmail.com
> Blog: http://blogs.sqlxml.org/srinivassampath
> URL: http://www32.brinkster.com/srisamp
> "Greg" <Greg@.discussions.microsoft.com> wrote in message
> news:CC2448DB-9147-4391-BBDA-7891A6E3294F@.microsoft.com...
>
>|||168 7.5 7.5 5
Perhaps those aren't spaces - in fact I think they are one carriage return
and one line feed character. How are these values inserted? You should
prevent illegal values from being entered at all.
In the mean time - remove char(13) and char(10) from the string.
ML
http://milambda.blogspot.com/

Friday, February 10, 2012

Cast as Varchar

Hi,
I have a table that has a field set to decimal data type and the following
query needs to update it to a varchar type if it meets the criteria. Why am
I still getting the "Error converting varchar to numeric" error? Thanks!
UPDATE [Activities Data]
SET [Activities Data].[SUMMARY DISPOSITION] =
Case
When ([ACTCD] In (1,5,6))
Then Cast('In Process' as varchar (20))
When ([ACTCD] In (3,7,8,16,17))
Then Cast('Completed' as varchar (20))
When ([ACTCD] In (2,9,10,11,12,13,15))
Then Cast('Closed' as varchar (20))
ELSE [ACTCD]
END
WHERE ([Activities Data].PGMNO In (11165,11175,11177,11169,21175))>>>
UPDATE [Activities Data]
SET [Activities Data].[SUMMARY DISPOSITION] =
Case
When ([ACTCD] In (1,5,6))
Then Cast('In Process' as varchar (20))
When ([ACTCD] In (3,7,8,16,17))
Then Cast('Completed' as varchar (20))
When ([ACTCD] In (2,9,10,11,12,13,15))
Then Cast('Closed' as varchar (20))
ELSE [ACTCD]
END
WHERE ([Activities Data].PGMNO In (11165,11175,11177,11169,21175))arkred">
Instead of constantly running an UPDATE statement like this, wouldn't it
make more sense to create a VIEW or a computed column?
Anyway, CASE is an expression that returns a single value. Regardless of
the outcome, this value must be at least implicitly convertible to the same
data type. You have three varchar results, and an INT result, and the only
one you didn't convert is the only one that needed it. How about :
UPDATE [Activities Data]
SET [Activities Data].[SUMMARY DISPOSITION] =
Case
When ([ACTCD] In (1,5,6))
Then 'In Process'
When ([ACTCD] In (3,7,8,16,17))
Then 'Completed'
When ([ACTCD] In (2,9,10,11,12,13,15))
Then 'Closed'
ELSE CONVERT(VARCHAR(20), [ACTCD])
END
WHERE ([Activities Data].PGMNO In (11165,11175,11177,11169,21175))
I still think it's a mistake to store this redundant data because you can
already figure it out at any time; and queries won't have to verify that the
UPDATE has taken place to be sure that the answer is correct.
"Patrice" <Patrice@.discussions.microsoft.com> wrote in message
news:0911EBFD-9E0F-4E67-8799-137C58A7E1DD@.microsoft.com...
> Hi,
> I have a table that has a field set to decimal data type and the following
> query needs to update it to a varchar type if it meets the criteria. Why
> am
> I still getting the "Error converting varchar to numeric" error? Thanks!
>
> UPDATE [Activities Data]
> SET [Activities Data].[SUMMARY DISPOSITION] =
> Case
> When ([ACTCD] In (1,5,6))
> Then Cast('In Process' as varchar (20))
> When ([ACTCD] In (3,7,8,16,17))
> Then Cast('Completed' as varchar (20))
> When ([ACTCD] In (2,9,10,11,12,13,15))
> Then Cast('Closed' as varchar (20))
> ELSE [ACTCD]
> END
> WHERE ([Activities Data].PGMNO In (11165,11175,11177,11169,21175))|||If the field datatype is decimal (as you indicated) then you are getting the
error because you are trying to put a varchar into a decimal -and that won't
work.
Something doesn't look right in this code example. 'In Process',
'Completed', 'Closed', are in fact strings -why are you needing to cast()
them as varchar()?
What is the datatype of the [Activities Data].[SUMMARY DISPOSITION] field?
Having the actual table DDL would be useful.
--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."
*Yet Another certification Exam
"Patrice" <Patrice@.discussions.microsoft.com> wrote in message
news:0911EBFD-9E0F-4E67-8799-137C58A7E1DD@.microsoft.com...
> Hi,
> I have a table that has a field set to decimal data type and the following
> query needs to update it to a varchar type if it meets the criteria. Why
> am
> I still getting the "Error converting varchar to numeric" error? Thanks!
>
> UPDATE [Activities Data]
> SET [Activities Data].[SUMMARY DISPOSITION] =
> Case
> When ([ACTCD] In (1,5,6))
> Then Cast('In Process' as varchar (20))
> When ([ACTCD] In (3,7,8,16,17))
> Then Cast('Completed' as varchar (20))
> When ([ACTCD] In (2,9,10,11,12,13,15))
> Then Cast('Closed' as varchar (20))
> ELSE [ACTCD]
> END
> WHERE ([Activities Data].PGMNO In (11165,11175,11177,11169,21175))|||Patrice wrote:
> Hi,
> I have a table that has a field set to decimal data type and the following
> query needs to update it to a varchar type if it meets the criteria. Why
am
> I still getting the "Error converting varchar to numeric" error? Thanks!
>
> UPDATE [Activities Data]
> SET [Activities Data].[SUMMARY DISPOSITION] =
> Case
> When ([ACTCD] In (1,5,6))
> Then Cast('In Process' as varchar (20))
> When ([ACTCD] In (3,7,8,16,17))
> Then Cast('Completed' as varchar (20))
> When ([ACTCD] In (2,9,10,11,12,13,15))
> Then Cast('Closed' as varchar (20))
> ELSE [ACTCD]
> END
> WHERE ([Activities Data].PGMNO In (11165,11175,11177,11169,21175))
What is the datatype of [Activities Data].[SUMMARY DISPOSITION]? What
happens in your CASE statement is ACTCD is not one of the numeric values
you've listed? My assumption is that one of your records is falling
through the CASE statment, returning the true value of ACTCD, which is
then being stuffed into [SUMMARY DISPOSITION], and that's where your
error is coming from.

Cast as Decimal

myTable in the below code examples resides in a linked Visual FoxPro
database. myTable contains a field called myDecimalField as well as several
others exactly like it and are of Decimal (9,1) not null type.
All the other fields select fine in SQL, but myDecimalField gives the ERROR
below when I SELECT it.
Just for a test, I CASTed myDecimalField in CODE 1 below as a VarChar type
and SQL returns it fine. So, I tried CODE 2 below and tried to CONVERT the
VarChar CAST and I get the same ERROR below.
Can someone help me with syntax in CODE 2 and convert myDecimalField into a
DECIMAL format so I can retain the fields decimals and numberic type?
**********************
CODE 1 (works):
SELECT myIdField, CAST(myDecimalField AS
VARCHAR(55)) AS myDecimalField FROM myTable
CODE 2 ( doesn't work):
SELECT myIdField, CONVERT(DECIMAL(18, 4),
CAST(myDecimalField AS VARCHAR(55))) AS myDecimalField FROM myTable
ERROR:
OLE DB error trace [OLE/DB Provider 'VFPOLEDB'
IRowset::GetData returned 0x80040e21: Data status returned from the
provider: [COLUMN_NAME=myDecimalField
STATUS=DBSTATUS_E_UNAVAILABLE]].
Msg 7341, Level 16, State 2, Line 1
Could not get the current row value of column
'[VFPOLEDB].myDecimalField' from the OLE DB provider 'VFPOLEDB'. The
provider cannot determine the value for this column.Scott Bailey (sbailey@.mileslumber.com) writes:
> myTable in the below code examples resides in a linked Visual FoxPro
> database. myTable contains a field called myDecimalField as well as
> several others exactly like it and are of Decimal (9,1) not null type.
> All the other fields select fine in SQL, but myDecimalField gives the
> ERROR below when I SELECT it.
> Just for a test, I CASTed myDecimalField in CODE 1 below as a VarChar type
> and SQL returns it fine. So, I tried CODE 2 below and tried to CONVERT the
> VarChar CAST and I get the same ERROR below.
> Can someone help me with syntax in CODE 2 and convert myDecimalField
> into a DECIMAL format so I can retain the fields decimals and numberic
> type?
The situation certainly looks spooky, but the root problem is obviously
a problem with FoxPro, or the FoxPro provider. I would guess that there
some rows where myDecimalField has some illegal value.
Assuming that myTable has an column called id, of which the lowest value
is 1, and the highest is 100, you could do
SELECT ... FROM myTable WHERE id BETWEEN 1 AND 50
If that gives the error, narrow down the interval to 1 AND 25 and so on.
Of course it's a good idea to look at the data from FoxPro as well.
If you want an SQL Server solution, you would have to bounce the data
over a temp table, so the conversion from varchar takes place in
SQL Server.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Couple of things:
1. The error occurs on all records so i know it's not bad data, plus there's
another field with same problem.
2. Could you provide some syntax example of creating a temp table with the
varchar conversion and transferring it as you suggested? I've never used a
temp table before.
"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns97A9BCADA85Yazorman@.127.0.0.1...
> Scott Bailey (sbailey@.mileslumber.com) writes:
> The situation certainly looks spooky, but the root problem is obviously
> a problem with FoxPro, or the FoxPro provider. I would guess that there
> some rows where myDecimalField has some illegal value.
> Assuming that myTable has an column called id, of which the lowest value
> is 1, and the highest is 100, you could do
> SELECT ... FROM myTable WHERE id BETWEEN 1 AND 50
> If that gives the error, narrow down the interval to 1 AND 25 and so on.
> Of course it's a good idea to look at the data from FoxPro as well.
> If you want an SQL Server solution, you would have to bounce the data
> over a temp table, so the conversion from varchar takes place in
> SQL Server.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx|||scott (sbailey@.mileslumber.com) writes:
> 1. The error occurs on all records so i know it's not bad data, plus
> there's another field with same problem.
Weird. But I'm not a Foxpro person, so I have no idea of what could
be going on.

> 2. Could you provide some syntax example of creating a temp table with the
> varchar conversion and transferring it as you suggested? I've never used a
> temp table before.
CREATE TABLE #spookydecimal (id int NOT NULL,
decvalue varchar(55) NULL)
-- Add other columns as needed.
INSERT #spookydecimal(id, decvalue)
SELECT myIdField, CAST(myDecimalField AS
VARCHAR(55)) AS myDecimalField
FROM myTable
SELECT id, decvalue, case(decvalue as decimal(18,4))
FROM #spoookydecimal
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||thanks.
"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns97A9DB71F9EE0Yazorman@.127.0.0.1...
> scott (sbailey@.mileslumber.com) writes:
> Weird. But I'm not a Foxpro person, so I have no idea of what could
> be going on.
>
> CREATE TABLE #spookydecimal (id int NOT NULL,
> decvalue varchar(55) NULL)
> -- Add other columns as needed.
> INSERT #spookydecimal(id, decvalue)
> SELECT myIdField, CAST(myDecimalField AS
> VARCHAR(55)) AS myDecimalField
> FROM myTable
> SELECT id, decvalue, case(decvalue as decimal(18,4))
> FROM #spoookydecimal
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx

Cast a null to decimal?

I have to do a join to a table that may have expenses incurred for a
commission report.
How do I cast that null to a 0.00 so I can subtract the expense from the
SalesPrice in a single pass query?
TIA
__StephenHi,
Use ISNULL function:-
ISNULL(fieldname,0.00)
Thanks
Hari
SQL Server MVP
"Stephen Russell" <srussell@.lotmate.com> wrote in message
news:eieXRMyWFHA.2288@.TK2MSFTNGP14.phx.gbl...
>I have to do a join to a table that may have expenses incurred for a
> commission report.
> How do I cast that null to a 0.00 so I can subtract the expense from the
> SalesPrice in a single pass query?
> TIA
> __Stephen
>|||Use Coalesce like so:
Coalesce(ColumName, 0.00)
Thomas
"Stephen Russell" <srussell@.lotmate.com> wrote in message
news:eieXRMyWFHA.2288@.TK2MSFTNGP14.phx.gbl...
>I have to do a join to a table that may have expenses incurred for a
> commission report.
> How do I cast that null to a 0.00 so I can subtract the expense from the
> SalesPrice in a single pass query?
> TIA
> __Stephen
>