Is there any way to convert string to DOUBLE format when inserting new record, I want to strip it out or ignore if it exists in value when do insert in database.
INSERT INTO table1 (measure) VALUES val1
val1 can contain 20000 or 20,000. Can I do it with mysql?
You must first remove the separators by using REPLACE.
INSERT INTO table1 (measure) VALUES (REPLACE(val1, ',', ''))
As njk mentioned, you can use REPLACE function, but to ignore if it exists in value, you use INSERT IGNORE.
INSERT IGNORE INTO table1 (measure) VALUES (REPLACE(val1, ',', ''))
The IGNORE keyword will not insert the new record if the value is found ONLY IF the column has an unique index defined or a primary key.
http://dev.mysql.com/doc/refman/5.5/en/insert.html
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace
Related
0.0.0.1 saved in sql table column as 10001.
MY data base contains values as such above mentioned i wanted to sort it based on the values but if I sort is as it is it will give me wrong order so i need tp convert it to the above mentioned format(10001). i.e. Remove the dots(.)
Thank you.
(I guess you're actually using Oracle as a database, regarding the tool - Oracle SQL Developer - you've also tagged, which means that MySQL tag should be removed).
To me, it looks as if you'd want to a) remove dots, b) change datatype to number (so that it is correctly sorted):
order by to_number(replace(col, '.', ''))
It presumes that only characters allowed are digits and dots. If there's a value like 'A.0.0.1', it'll - of course - fail, as you can't convert letter A to a number.
Why are you storing the period '.' Character to begin with? If it's not needed you can remove it.
If you want to remove all non-alphanumeric characters you could use a regular expresion.
create table t (nm varchar2(20));
insert into t values ('.0.0.0.1');
insert into t values ('10.0.1.1.0');
commit;
select * from t;
NM
.0.0.0.1
10.0.1.1.0
update t
set nm = regexp_replace(
regexp_replace(nm, '[^A-Z0-9 ]', ''),
' {2,}', ' '
);
select * from t;
NM
0001
100110
You can use the translate command with a SELECT and the data will not be charged in the table.
See below
create table t (nm varchar2(20));
insert into t values ('.0.0.0.1');
insert into t values ('10.0.1.1.0');
commit;
SELECT translate(nm, '*.','*') from t
TRANSLATE(NM,'*.','*')
0001
100110
SELECT DISTINCT(column_name)
FROM Table_name
ORDER BY
TO_NUMBER (REGEXP_SUBSTR (column_name, '\d+',1,2)),
TO_NUMBER (REGEXP_SUBSTR (column_name,'\d+',1,3)) NULLS FIRST,
TO_NUMBER (REGEXP_SUBSTR (column_name,'\d+',1,4)) NULLS FIRST;
Is there a flag to indicate that data is of binary type, or does that need to be converted to another type. For example, is there a way to do:
INSERT INTO mytable (number) VALUES (0000 0010)
Instead of:
INSERT INTO mytable (number) VALUES (2)
I know the above is a trivial example but basically I'm wondering if you can type a binary/hex value directly into a query.
You can use a bit-value literal
INSERT INTO mytable (number) VALUES (b'00000010');
or
INSERT INTO mytable (number) VALUES (0b00000010)
Is it possible to retrieve the maximum value of a key column, increase it and insert it as a new value in the database, like this (pseudo-sql)
insert into mytable (mykeycolumn,col1,col2)
values((max(mykeycolumn)+1),'val1','val2');
Yes, you INSERT from a SELECT, but your fixed fields must "come from the SELECT" as well, like such:
INSERT INTO mytable (mykeycolumn, col1, col2)
SELECT MAX(mykeycolumn)+1, 'val1', 'val2'
FROM mytable;
Complementing: as a_horse_with_no_name pointed out, MAX() + 1 could cause your problems if you have simultaneous transactions in mytable. At some point two identical mykeycolumn values would be generated and you would get an error. The ideal solution is to convert your table to use auto_increment.
You can with this:
INSERT INTO mytable (mykeycolumn,col1,col2) VALUES
((SELECT MAX(mykeycolumn) FROM mytable AS foo)+1,'val1','val2');
try this
INSERT INTO mytable (mykeycolumn, col1, col2)
SELECT SCOPE_IDENTITY()+1, 'val1', 'val2' FROM mytable;
I have a table named student which consists of (rollno, name, sem, branch)
If I want to INSERT only one value (i.e only name has to be entered) what is the query?
To insert values into specific columns, you first have to specify which columns you want to populate. The query would look like this:
INSERT INTO your_table_name (your_column_name)
VALUES (the_value);
To insert values into more than one column, separate the column names with a comma and insert the values in the same order you added the column names:
INSERT INTO your_table_name (your_column_name_01, your_column_name_02)
VALUES (the_value_01, the_value_02);
If you are unsure, have a look at W3Schools.com. They usually have explanations with examples.
insert into student(name) values("The name you wan to insert");
Be careful not to forget to insert the primary key.
First, if the table is all empty, just wanna make the column 'name' with values, it is easy to use INSERT INTO. https://www.w3schools.com/sql/sql_insert.asp.
`INSERT INTO TABLE_NAME (COLUMN_NAME) VALUES ("the values")`
Second, if the table is already with some values inside, and only wanna insert some values for one column, use UPDATE. https://www.w3schools.com/sql/sql_update.asp
UPDATE table_name SET column1 = value1 WHERE condition;
insert into student (name)
select 'some name'
or
insert into student (name)
values ('some name')
Following works if other columns accept null or do have default value:
INSERT INTO Student (name) VALUES('Jack');
Further details can be found from the Reference Manual:: 13.2.5 INSERT Syntax.
Execute this query, if you want the rest of columns as "#", do insert # inside the single quote which is left blank in query.
Also, there is no need of defining column name in the query.
insert into student values('','your name','','');
Thanks...
Is this possible or do I have to list all the columns?
INSERT INTO table_name (column1, **column3**, column2,...)
VALUES (value1, value2, value3,...)
Do I have to list all the columns in order and have values for each one?
You only have to put the columns you plan on inserting. The only order that needs to match up is the column name and value.
IE: 3 Columns: col1, col2,col3
INSERT INTO TABLE (col1,col2) VALUES(col1value,col2value)
INSERT INTO TABLE (col2,col3) VALUES(col2value,col3value)
INSERT INTO TABLE (col3,col2) VALUES(col3value,col2value)
For your INSERT statement it should be like this:
INSERT INTO table_name (column1, column3, column2,...) VALUES (value1, value3, value2,...)
Since you moved column3, value3 should "match up" with it
You can put your query columns in any order, as long as you specify that order like you are doing above. The actual values you insert as part of the VALUE clause must match the INSERT INTO (x,y,z) part of your query.
Any columns that you do not specify will have a default value inserted. The default value is determined by the DEFAULT value set when the column was created.
Your INSERT can fail if a column has a NOT NULL specification on it and no DEFAULT value, and you do not provide one in the query.
Your column names must correspond to the values that you are inserting. For instance, in your above query, if column1 is a varchar, column3 an int, etc, the values must correspond and be in the exact order for your query to execute successfully.