Showing posts with label losing. Show all posts
Showing posts with label losing. Show all posts

Sunday, March 11, 2012

change column width

Hi there,
How would I go about changing column width from varchar 40 to varchar 80
in SQL server 2000? Is it possible without losing data?
Thanks, CalinTester wrote:
> Hi there,
> How would I go about changing column width from varchar 40 to varchar
80
> in SQL server 2000? Is it possible without losing data?
> Thanks, Calin
>
>
See ALTER TABLE in Books Online...
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||ALTER TABLE tblToChange ALTER COLUMN [colToChange] VARCHAR(80) NOT NULL
or
ALTER TABLE tblToChange ALTER COLUMN [colToChange] VARCHAR(80) NULL|||pl wrote:
> ALTER TABLE tblToChange ALTER COLUMN [colToChange] VARCHAR(80) NOT NUL
L
> or
> ALTER TABLE tblToChange ALTER COLUMN [colToChange] VARCHAR(80) NULL
Look at alter table in books online.
Since you are increasing length dont worry about data loss.
Regards
Amish Shah
http://shahamishm.tripod.com

change column width

Hi there,
How would I go about changing column width from varchar 40 to varchar 80
in SQL server 2000? Is it possible without losing data?
Thanks, Calin
Tester wrote:
> Hi there,
> How would I go about changing column width from varchar 40 to varchar 80
> in SQL server 2000? Is it possible without losing data?
> Thanks, Calin
>
>
See ALTER TABLE in Books Online...
Tracy McKibben
MCDBA
http://www.realsqlguy.com
|||ALTER TABLE tblToChange ALTER COLUMN [colToChange] VARCHAR(80) NOT NULL
or
ALTER TABLE tblToChange ALTER COLUMN [colToChange] VARCHAR(80) NULL
|||pl wrote:
> ALTER TABLE tblToChange ALTER COLUMN [colToChange] VARCHAR(80) NOT NULL
> or
> ALTER TABLE tblToChange ALTER COLUMN [colToChange] VARCHAR(80) NULL
Look at alter table in books online.
Since you are increasing length dont worry about data loss.
Regards
Amish Shah
http://shahamishm.tripod.com

change column width

Hi there,
How would I go about changing column width from varchar 40 to varchar 80
in SQL server 2000? Is it possible without losing data?
Thanks, CalinTester wrote:
> Hi there,
> How would I go about changing column width from varchar 40 to varchar 80
> in SQL server 2000? Is it possible without losing data?
> Thanks, Calin
>
>
See ALTER TABLE in Books Online...
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||ALTER TABLE tblToChange ALTER COLUMN [colToChange] VARCHAR(80) NOT NULL
or
ALTER TABLE tblToChange ALTER COLUMN [colToChange] VARCHAR(80) NULL|||pl wrote:
> ALTER TABLE tblToChange ALTER COLUMN [colToChange] VARCHAR(80) NOT NULL
> or
> ALTER TABLE tblToChange ALTER COLUMN [colToChange] VARCHAR(80) NULL
Look at alter table in books online.
Since you are increasing length dont worry about data loss.
Regards
Amish Shah
http://shahamishm.tripod.com

Sunday, February 12, 2012

Casting Help

Can someone please help me with this? I'm losing all my hair trying to figure it out. I've tried all that I can think of. I am getting the Error converting data type varchar to numeric.

Thanks
Sam "O"


cmdInsert = New SqlCommand( "INSERT INTO tmp_blank (sessionid, ssn,job,sun,mon,tue,wed,thu,fri,sat,totcol)
SELECT '" & Session.SessionId & "', ssn,job,sun,mon,tue,wed,thu,fri,sat,
(cast(sun as numeric(4,2)) + cast(mon as numeric(4,2)) +
cast(tue as numeric(4,2)) + cast(wed as numeric(4,2)) +
cast(thu as numeric(4,2)) + cast(fri as numeric(4,2)) +
cast(sat as numeric(4,2))) as totcol from
" & strTableName, conTimeCard)

intUpdateCount = cmdInsert.ExecuteNonQuery()

What are the values for sun, etc.? Could they be null? Could they be non-numeric, like 'fred'? If they are numeric, why are you using a varchar field?|||Thank you for your quick reply. Here are the answers to your questions.

yes values could be null
They will never be non-numeric
I am using an exisiting table and I am not able to modify the datatypes (yet)

Thanks
Sam "O"|||I am not certain the NULL is the issue, but I would try using IsNull(), like so:

CAST(IsNull(@.v,'0') as Numeric(4,2))

Even if it does not throw the exception, if the variable is null, null+anything will return null.

I would also check and see that all values are null rather than a single space. For instance, this will give you the casting error:


DECLARE @.v as varchar(20)
SET @.v=''

SELECT CAST(IsNull(@.v,'0') as Numeric(4,2))

If the values that do not appear obviously to be numbers are not NULL, but are rather empty strings or ' ', then perhaps you will want to run a query to change the "missing" values to null, or create a view with case statements to fix up the values. This is why using one type when you want another is a bad idea.|||I did try the IsNull Prior to posting and still was thrown the exception so it makes sense to me now what you are saying about the empty string possibilities.

Thank you for your input!

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/