dbTalk Databases Forums  

sql script

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss sql script in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Brian
 
Posts: n/a

Default sql script - 04-24-2007 , 03:49 PM






How can I change a character in a string of text. I have a bunch number
that have a lower case l in them and I need to make them an uppercase L.
Example; 99l5555 needs to be 99L5555.



Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: sql script - 04-24-2007 , 04:58 PM






Brian (b.houghtby (AT) eaglecrusher (DOT) com) writes:
Quote:
How can I change a character in a string of text. I have a bunch number
that have a lower case l in them and I need to make them an uppercase L.
Example; 99l5555 needs to be 99L5555.
UPDATE tbl
SET col = replace(col, 'l', 'L')

Or if there other lowercase as well that should be uppercase:

UPDATE tbl SET col = upper(col)

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #3  
Old   
Brian
 
Posts: n/a

Default Re: sql script - 04-25-2007 , 08:41 AM



I must be doing something wrong, this is my error.

Msg 547, Level 16, State 0, Line 3
The UPDATE statement conflicted with the REFERENCE constraint
"FK_GL_TrnsLne_IMA". The conflict occurred in database "EAGLE", table
"dbo.GL_TrnsLne", column 'GLL_ItmID'.
The statement has been terminated.

"Brian" <b.houghtby (AT) eaglecrusher (DOT) com> wrote

Quote:
How can I change a character in a string of text. I have a bunch number
that have a lower case l in them and I need to make them an uppercase L.
Example; 99l5555 needs to be 99L5555.




Reply With Quote
  #4  
Old   
Brian
 
Posts: n/a

Default Re: sql script - 04-25-2007 , 09:51 AM



In your second example, is the word upper referring to a L in this case.
Seems like that would just replace everything in that column with an L?
Sorry, Im kind of a newby and trying to be cautious.

"Erland Sommarskog" <esquel (AT) sommarskog (DOT) se> wrote

Quote:
Brian (b.houghtby (AT) eaglecrusher (DOT) com) writes:
How can I change a character in a string of text. I have a bunch number
that have a lower case l in them and I need to make them an uppercase L.
Example; 99l5555 needs to be 99L5555.

UPDATE tbl
SET col = replace(col, 'l', 'L')

Or if there other lowercase as well that should be uppercase:

UPDATE tbl SET col = upper(col)

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx



Reply With Quote
  #5  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: sql script - 04-25-2007 , 10:33 AM



Brian (b.houghtby (AT) eaglecrusher (DOT) com) writes:
Quote:
In your second example, is the word upper referring to a L in this case.
Seems like that would just replace everything in that column with an L?
In the case of:

Quote:
UPDATE tbl SET col = upper(col)
"abc123" will be converted to "ABC123". That is, the upper function replaces
lowercase characters to the corresponding uppercase characters according to
the rules of the collation for the column.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #6  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: sql script - 04-25-2007 , 04:44 PM



Brian (b.houghtby (AT) eaglecrusher (DOT) com) writes:
Quote:
I must be doing something wrong, this is my error.

Msg 547, Level 16, State 0, Line 3
The UPDATE statement conflicted with the REFERENCE constraint
"FK_GL_TrnsLne_IMA". The conflict occurred in database "EAGLE", table
"dbo.GL_TrnsLne", column 'GLL_ItmID'.
The statement has been terminated.
One problem is that we don't know what you are doing. First you ask how
to do something, then you present an error message. That means that we
have to guess.

If you change 99l5555 to 99L5555 and this gives the error above, this
indicates two things:
1) This is a key value, and there are other table referencing that key
value.
2) The database uses case-sensitive or binary collation.

There are a couple of ways to go. One is to change the constraints
to have ON UPDATE CASCADE.

But it would help to know more about what you are trying to achieve.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #7  
Old   
bubbles
 
Posts: n/a

Default Re: sql script - 04-25-2007 , 09:05 PM



On Apr 25, 5:58 am, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote:
Quote:
Brian (b.hough... (AT) eaglecrusher (DOT) com) writes:
How can I change a character in a string of text. I have a bunch number
that have a lower case l in them and I need to make them an uppercase L.
Example; 99l5555 needs to be 99L5555.

UPDATE tbl
SET col = replace(col, 'l', 'L')

Or if there other lowercase as well that should be uppercase:

UPDATE tbl SET col = upper(col)

--
Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

I've also been able to replace several characters in a string by
nesting the function.
For example, to replace any ":", "-", " " to "_", I'll nest the
function like this:

REPLACE(REPLACE(REPLACE(col,':','_'),'-','_'),' ', '_')

Bubbles



Reply With Quote
  #8  
Old   
avode
 
Posts: n/a

Default Re: sql script - 04-25-2007 , 09:30 PM



You would use a constraint which forces upper case in the column.
Don't forget to take into account of the column collation.
The next T-SQL script makes the point more clear.

CREATE TABLE #foo_cs(
col CHAR(7) COLLATE Latin1_General_CS_AI);
GO
CREATE TABLE #foo_ci(
col CHAR(7) COLLATE Latin1_General_CI_AI);
GO
ALTER TABLE #foo_cs
ADD CONSTRAINT uppercase_always_cs
CHECK(col = UPPER(col));
GO
ALTER TABLE #foo_ci
ADD CONSTRAINT uppercase_always_ci
CHECK(col COLLATE Latin1_General_CS_AI = UPPER(col));
GO
INSERT INTO #foo_cs(col) VALUES('99l9999');
GO
INSERT INTO #foo_ci(col) VALUES('99l9999');
GO
INSERT INTO #foo_cs(col) VALUES('99L9999');
GO
INSERT INTO #foo_ci(col) VALUES('99L9999');
GO
SELECT col FROM #foo_cs
WHERE col COLLATE Latin1_General_CI_AI = '99l9999';
GO
SELECT col FROM #foo_ci WHERE col = '99l9999';
GO

DROP TAble #foo_cs, #foo_ci;
GO

--
Andrey Odegov
avodeGOV (AT) yandex (DOT) ru
(remove GOV to respond)


Reply With Quote
  #9  
Old   
Brian
 
Posts: n/a

Default Re: sql script - 04-26-2007 , 03:15 PM



Sorry, Let me start over. I am trying to do a mass change of a bunch of
numbers that a user entered incorrectly. He entered them with a lower case
letter instead of uppercase. The numbers all begin with a 99l and I want to
do a mass replace to change them all to a 99L. The numbers have a suffix
that is not constant like the prefix. Example; 99l555, 99l556, 99l557 and
so on.

The name of the table is dbo.item and the column is ima_itemid. Here is my
select statement.

Select * from dbo.item where
ima_itemid like '99l%'

This result gives me a mixture of 99l's and 99L's. So apparently the user
eventually started entering them the correct way, in case that is an issue
having a mixture. It appears that my select statement doesn't care about
case since I get upper and lower, although I used lower in my select. Hope
this clarifies some.


"Brian" <b.houghtby (AT) eaglecrusher (DOT) com> wrote

Quote:
How can I change a character in a string of text. I have a bunch number
that have a lower case l in them and I need to make them an uppercase L.
Example; 99l5555 needs to be 99L5555.




Reply With Quote
  #10  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: sql script - 04-26-2007 , 04:44 PM



Brian (b.houghtby (AT) eaglecrusher (DOT) com) writes:
Quote:
Sorry, Let me start over. I am trying to do a mass change of a bunch of
numbers that a user entered incorrectly. He entered them with a lower
case letter instead of uppercase. The numbers all begin with a 99l and
I want to do a mass replace to change them all to a 99L. The numbers
have a suffix that is not constant like the prefix. Example; 99l555,
99l556, 99l557 and so on.

The name of the table is dbo.item and the column is ima_itemid. Here is
my select statement.

Select * from dbo.item where
ima_itemid like '99l%'

This result gives me a mixture of 99l's and 99L's. So apparently the
user eventually started entering them the correct way, in case that is
an issue having a mixture. It appears that my select statement doesn't
care about case since I get upper and lower, although I used lower in my
select. Hope this clarifies some.
Then this should do it:

UPDATE item
SET ima_itemid = replace(ima_itemid, 'l', 'L')
WHERE ima_itemid COLLATE Latin1_General_BIN LIKE '99l%'

By foring a binary collation, only rows with the incorrect pattern
are selected. This should not trigger an FK constraint violation,
given what you have said about both 99l and 99L being returned.

The suggestion from Andrey to add a constraint to prevent this from
happening again is an excellent idea you should pursue.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.