This should be very simple but MS Access is killing me!
All I want to do is find and replace all instances of ' (and some others) with the appropriate character, in this case, an apostrophe.
Here's my query:
UPDATE Table1
SET Title = Replace(Title, "'", "'")
WHERE Title LIKE '*'*'
And even a simple select doesn't work:
SELECT * FROM Table1
WHERE Title LIKE '*'*'
Does anyone have a solution? I really have searched and found nothing for this particular issue.
I could do a PHP script but all this stuff is supposed to be kept in the DB so I kinda need to sort it with a simple query if possible.
Thanks a lot!
EDIT: Some Sample Data of Title in Table1
Row 1. 'Quick Release' Loop, Stainless Steel
Row 2. Silver 'V' Shaped Edging
Row 3. Plastic 'T' Shaped Seal
The bad thing is that # is system character in jetSQL. There are several ways to deal with it:
omit WHERE part and update all strings with replace.
use such WHERE (InStr(1,[Title],"'")>0);
use DAO or ADO recordset
Related
I have quite a strange problem, I'm very new to SQL and I was doing a free course in SQL
I'm actually learning the "SELECT" command.
So, I create my database, create some table, put some data in it with the "INSERT INTO" command
And now I want to select some data in it, but I have a strange bug (not an error) when i do
SELECT * FROM aliment;
everything work like it's supposed to do, but when I do
SELECT 'nom','calories' FROM aliment;
Something strange happens.
Instead have a list of all the specific data i'm looking to get i just get a list with all the data replaced by the name of the columns.
Here 2 screen to show you what's happens:
[2
I know one it's from the terminal(and yes it's not mine but from a video) and mine is from the software, but it's still had to work no?
You have a typo in your SQL. Use backticks (on the same key as ~ on a US keyboard) around your column names, not '. Using a single quote (an apostrophe) makes it a literal value, not a column name.
SELECT `nom`,`calories`FROM aliment;
I have a database full of important information... I need to change the information to fit a new format...
The current information follows this format... (Note this is all inserted in one cell)
DataString1:SomeOtherString1:MoreString1|DataString2:SomeOtherString2:MoreString2|DataString3:SomeOtherString3:MoreString3
I need to be able to do the following...
1) Locate all of the '|' symbols.
2) For each '|' symbol I need to find the second ':' before it.
3) Insert another ':' before the results of step 2.
I can accomplish this through code in another language like PHP for example but I would like to be able to do it via SQL.
The above example would turn into this... (I bolded the changes...)
DataString1::SomeOtherString1:MoreString1|DataString2::SomeOtherString2:MoreString2|DataString3::SomeOtherString3:MoreString3
I ended up just making a PHP script to do this, I'm not even sure what I wanted was entirely possible with MySQL as there was over 30 instances of | in each cell, and over 180 instances of ':' in each cell.... If anyone has an answer feel free to post and if it works Ill vote it as best answ
In sql the substring can be used like this substring(expression,startposition, length), If your occurence is repeating then you will have to create an update script which will check the substring postion at the character length lets say
DataString1
Here position of S is 5, you should count the occurence of | and save this on to a variable
select #sd substring (column_name ,start,length) from table where --conditons
Update table set column_name = '' where substring (column_name ,start,length) = '|'
you have to get the occurance of #sd and do the math to concatinate the string and update your cells
I ended up just making a PHP script to do this, I'm not even sure what I wanted was entirely possible with MySQL as there was over 30 instances of | in each cell, and over 180 instances of ':' in each cell.... If anyone has an answer feel free to post and if it works Ill vote it as best answer.
I am reading a value from table with apostrophe with which I create a dynamic query and than I run a sp to save it in another table, which works fine without apostrophe but throw an error when it contains an apostrophe.
Select #arguments = argument from Mytable
e.g.
set #sql = 'exec nameOfSP' + #arguments
#arguments value comes from database
#argument sample value '612f0', 'This is an example second string'
Yes I know and agree that this is very bad code smell and therefore the question is not about design (which unfortunately couldn't be changed) but about the best possible solution in current scenario.
I am looking possible for a solution with encoding?
If there is a possibility of a quote coming through in your arguments do something like this:
set #sql = 'exec nameOfSP ' + REPLACE(#arguments, '''', '''''');
"the question is not about design (which unfortunately couldn't be changed)" seems like someone is going for the risk of saving in design that will cost a lot after... if you really must use dynamic sql like this you can use replace on ' to '' (that's right, just double it).
However, I must say that this is not a solution to your problem in any way, it's only a workaround.
You should do whatever you can to change the desing.
I am new to SQL and I have several large database with upper case first and last names that I need to convert to proper case in SQL sever 2008.
I am using the following to do this:
update database
Set FirstNames = upper(substring(FirstNames, 1, 1))
+ lower(substring(FirstNames, 2, (len(FirstNames) - 1) ))
I was wondering if there was any way to adapt this so that a field with two first names is also updated (currently I make the change and then go through and manually change the second name).
I have looked over the other answers in this field and they all seem quit long, compared to the query above.
Also is there any way to assist with converting the Mc suranmes ( I will manually change the others)? MCDONALD to McDonald, again I am just using the about query but replacing the FirstNames with LastName.
This is probably best done outside of SQL. However, if there is a requirement to do it on the server or if speed isn't an issue (because it will be an issue so you need to figure out if you care), the way you are going about it is probably the best way of doing so. If you want, you could create a UDF that puts all of the logic in one area.
Here is some code I came across (with attribution and more information below it):
CREATE FUNCTION dbo.fCapFirst(#input NVARCHAR(4000)) RETURNS NVARCHAR(4000)
AS
BEGIN
DECLARE #position INT
WHILE IsNull(#position,Len(#input)) > 1
SELECT #input = Stuff(#input,IsNull(#position,1),1,upper(substring(#input,IsNull(#position,1),1))),
#position = charindex(' ',#input,IsNull(#position,1)) + 1
RETURN (#input)
END
--Call it like so
select dbo.fCapFirst(Lower(Column)) From MyTable
I got this code from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=37760 There is more information and other suggestions in this forum as well.
As for dealing with cases like the McDonald, I would suggest one of two ways to handle this. One would be to put a search in the above UDF for key names ('McDonald', 'McGrew', etc.) or for patterns (the first two letters are Mc then make the next one capital, etc.) The second way would be to put these cases (the full names) in a table and have their replacement value in a second column. Then simply do a replace. Most likely, however, it will be easiest to identify rules like Mc then capitalize instead of trying to list every last-name possibility.
Don't forget you may want to modify the above UDF to include dashes, not just spaces.
Maybe this is too long but it is very easy and can be adapted for -, ', etc:
UPDATE tbl SET LastName = Case when (CharIndex(' ',lastname,1)<>0) then (Upper(Substring(lastname,1,1))+Lower(Substring(lastname,2,CharIndex(' ',lastname,1)-1)))+
(Upper(Substring(lastname,CharIndex(' ',lastname,1)+1,1))+
Lower(Substring(lastname,CharIndex(' ',lastname,1)+2,Len(lastname)-(CharIndex(' ',lastname,1)-1))))
else (Upper(Substring(lastname,1,1))+Lower(Substring(lastname,2,Len(lastname)-1))) end,
FirstName = Case when (CharIndex(' ',firstname,1)<>0) then (Upper(Substring(firstname,1,1))+Lower(Substring(firstname,2,CharIndex(' ',firstname,1)-1)))+
(Upper(Substring(firstname,CharIndex(' ',firstname,1)+1,1))+
Lower(Substring(firstname,CharIndex(' ',firstname,1)+2,Len(firstname)-(CharIndex(' ',firstname,1)-1))))
else (Upper(Substring(firstname,1,1))+Lower(Substring(firstname,2,Len(firstname)-1))) end;
Tony Rogerson has code that deals with:
double barrelled names eg Arthur Bentley-Smythe
Control characters
I haven't used it myself though...
Every week, I have to completely replace the data in several very large MySQL tables. So what I normally do is delete the existing data, import the new data, and then run my usual queries to modify the new data as needed.
Unfortunately, these days I have noticed that the new data contains unwanted characters, such as quotes and extra spaces. With well over 100,000 records in some of these tables (AFAIK), I cannot easily open the data in notepad to strip out unwanted characters, prior to importing.
I realize I could write a separate find and replace query for every single column in every table, like this:
UPDATE mytablename SET mycolumn = REPLACE(mycolumn, '"', '');
But having to name every column is a bother. Anyway, I would like to find a more elegant solution. Today, I found a snippet on the internet that looks like a start:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE
table_name = 'myTable' and ordinal_position = 1
I think the next step might be to loop through the ordinal positions, and then replace and update each column, but I don't know how to do this in MySQL. I also don't know how to stop the loop after the last column is reached, to avoid error messages.
Is there an easy way to do this? Or am I hoping for too much?
I am a beginner, so a clear, simple explanation would be much appreciated.
Thanks in advance.
MORE INFORMATION:
Since my first post, I have discovered that stored procedures are not allowed on my server. Too bad.
Anyway, I have tried this new code, just to get started:
set #mytablestring='mytable';
set #mycolumnnumber=1;
set #mycolumnname=(SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = #mytablestring and ordinal_position = #mycolumnnumber);
SELECT #mycolumnname FROM mytable;
Unfortunately, in the final SELECT query, #mycolumnname is interpreted as a string, not as a column name. So the query does not work. If I could get past this, I believe I could write some code to loop through the columns by incrementing #mycolumnnumber.
If anyone knows how to solve this, I would really appreciate some help.
Many thanks.
I suggest that you take a look at vim, sed, awk and many of the other text editors and text processing utilities that you can find on Linux (and sometimes on Windows too). 100,000 records may be a pain in Notepad, but it's a piece of cake for real text processing utilities.
For example, to strip all # characters from foobar.txt:
sed 's/#//g' foobar.txt > foobar-clean.txt
Or, the same thing with the file opened in (g)vim:
:%s/#//g