How can I use MySQL variable with POINT - mysql

I am trying to use a MySQL variable together with POINT like this (simplified):
SET #lat=145.033667; SET #long=-37.932000; INSERT INTO Location(position) values (GeomFromText(‘POINT(#lat #long)'));
This works fine:
SELECT 'POINT(145.033667 -37.932000)';
As does this:
SELECT GeomFromText('POINT(145.033667 -37.932000)');
Is there anyone who know how I can make this work?

I think you have a syntax error:
SET #lat=145.033667;
SET #long=-37.932000;
INSERT INTO Location(position)
values (GeomFromText('POINT(#lat #long)'));
^^^ Here you need '

Related

Insert into a specific row multiple values with Where clause?

I want to insert two dates into a specifc row on my table using MySql but have a syntax error on Workbench (Error 1064):
INSERT INTO atable (Activated,Expiry) WHERE Access_Code = 'accesscode'
VALUES('$current_date','$Expiration_Date')
What would be the correct version for this?
It would be something like this:
UPDATE atable
SET Activated='$current_date',
Expiry='$Expiration_date'
WHERE Access_Code='accesscode';
I'm assuming you will be setting $current_date and $Expiration_date via (what looks like PHP) code.
Hope this helps.

MySQL REPLACE variable string

I want to use MySQL query to change a link .
the link is like this :
http://website.com/click.php?ad_client=blablabla&add_id=548124&more=stuffhere
if I know the add_id number this is easy :
UPDATE table SET name = REPLACE(name, '&add_id=548124', '')
The problem is I have to change 5000 lines and I don't know the add_id number ... so what would be a correct mysql replace() code to remove &add_id=somenumber ??
USE This....
UPDATE table
SET name = CONCAT(SUBSTRING(name , 1,
INSTR(name ,'&add_id') - 1),SUBSTRING(name ,
INSTR(name , '&more'),
LENGTH(name ) - INSTR(name , '&add_id')))
Either you can do it via UDF - SO Answer or you can simply write PHP code which will replace value & update column again in table.
I would create a stored procedure that uses a cursor to iterate over each row that needs updating.
In the procedure I would find the link to replace and then replace them, one by one.
I've made a sqlfiddle to show how you can get the part to replace inside an select.
I think this approach is clean and easy to read but it's possible to write this a update (that will most likely be hard to read).
first to see that it works :
SELECT 'http://website.com/click.php?ad_client=blablabla&add_id=548124&more=stuffhere' INTO #link;
SELECT
#link as full_link,
SUBSTR(#link,LOCATE('&',#link),LOCATE('&',#link,LOCATE('&',#link)+1)-LOCATE('&',#link)) as remove_part,
REPLACE(#link,SUBSTR(#link,LOCATE('&',#link),LOCATE('&',#link,LOCATE('&',#link)+1)-LOCATE('&',#link)),'') as final_link
And now for your UPDATE:
UPDATE table SET name = REPLACE(name,SUBSTR(name,LOCATE('&',name),LOCATE('&',name,LOCATE('&',name)+1)-LOCATE('&',name)),'')
try this with REPLACE
UPDATE Table1
SET name = REPLACE(if(name like '%add_id=%','' , name ),
'&add_id=' , '' )
DEMO HERE

SQL: How To Append Semi-Colon To Column Value.

I have a column in my database named details_location which references the second part of a url ex. "manager\reports\user\description\UsersRights.htm" I'd like to append a semi-colon to each row value, so that the above appears as "manager\reports\user\description\UsersRights.htm;"
I've tried using something like
update reportdescriptions
set details_location = details_location+';'
To no avail, I'm sure it's something simple I'm missing. Thanks!
Try CONCAT function
update reportdescriptions set details_location = concat(details_location,';')
Try this:
update reportdescriptions
set details_location = CONCAT(details_location, ';');
For more info on CONCAT, check THIS.
You want concat(field_name,"string to add")
What error you get?
If you have a column of type text you may need to cast it to varchar() first
or CONCAT() like
UPDATE reportdescriptions
SET details_location = CONCAT(details_location , ';');

How to define a column that can automate capitalizing?

So that when you insert 'abc',it'll be converted to 'ABC' automatically.
I'm using MySQL,is it possible?
You could write an INSERT trigger that does this for you.
Something like:
CREATE TRIGGER Capitalize BEFORE INSERT ON MyTable
SET NEW.MyColumn = UPPER(NEW.MyColumn)
What about using Upper function while executing query ?
lets say You are using a SP to insert the values ... cast the value to upper case using UPPER function and then insert ?
this really should be done before it is passed to the DB

How do I remove slashes during a select statement

Please I am new. I am performing an insert select and I want to remove the slashes in a particular field say field b at the select portion of the query.
eg. insert into mytable(a,b,c) select a, stripslashes(b),c from mysecondtable;
Please help.
you can use REPLACE like this:
insert into mytable(a,b,c)
select a, REPLACE(b, '\\', '\') as b, c
from mysecondtable;
The REPLACE expression might have to be refined, but I hope this gets you started.
You can use MySQL String functions, e.g. replace.
Also consider sanitizing or preparing any input to the SELECT in the calling application before you execute the query.
use the SUBSTR function of mysql or do something like this:
"insert into table set value = " . stripslashes('whatever')
Are you doint this with PHP? Then a simple $fieldB = strip_slashes($_POST['b']); should do. You should then use $fieldB in your query.