sql delete from rows value - sql

The database includes a table with a rows:
html/css/java/php
How with sql query delete "java"?
The result would be:
html/css/php

See http://www.w3schools.com/sql/sql_delete.asp for details on DELETE.
DELETE FROM TableName WHERE yourColumn = 'java'

try this (SQL Server syntax, question does not specify which database):
DECLARE #YourTable table (YourColumn varchar(500))
INSERT INTO #YourTable VALUES ('html/css/java/php')
INSERT INTO #YourTable VALUES ('html/css/java')
INSERT INTO #YourTable VALUES ('java/php')
INSERT INTO #YourTable VALUES ('java')
INSERT INTO #YourTable VALUES ('html/css/php')
UPDATE #YourTable
SET YourColumn=REPLACE(
REPLACE(
REPLACE(YourColumn,'/java','')
,'java/',''
)
,'java',''
)
select * from #YourTable
OUTPUT
YourColumn
-------------------
html/css/php
html/css
php
html/css/php
(5 row(s) affected)

I'm not entirely sure what you're asking, because your question isn't very clear.
If "html", "css", "java", and "php" are different values for the same column, this is what you want:
DELETE FROM table_name WHERE column_name = 'java';
You'll need to replace "table_name" with the name of your table and "column_name" with the name of your column.

If there are 4 rows then Aioobe's answer would hold good. If you want to update the column but leave out Java then you should use:
UPDATE table_name SET column_name = Replace(column_name,'java/','')
IF you want to retrieve the information leaving out that data then use:
SELECT Replace(column_name,'java/','') column_name FROM table_name
HTH

I'm not sure what you want to achieve... If you need to delete column 'java', it should look like
ALTER TABLE table1 DROP COLUMN java

Here's another guess:
SQL> select * from t23
2 /
WHATEVER
----------------------------------------------------------------
html/css/java/php
SQL> update t23
2 set whatever = replace(whatever, '/java', null)
3 where whatever like '%/java%'
4 /
1 row updated.
SQL> select * from t23
2 /
WHATEVER
----------------------------------------------------------------
html/css/php
SQL>
There are other ways to do the same thing. For instance some flavours of database support using RegEx in a similar fashion

Related

Oracle SQL add new column based on value of other column

I'm looking for some help to create a new column based on values from another column - if this is even possible... This is not an ideal solution but I'm out running out of options.
I need to replace the beginning folder paths, change the direction of the \ and change the extension
Existing Field:
\\BRRNAKCL12\Audiofiles22\1Year\Diogarfngal_ZZZZZZZZZ\2020\Aug\03\5249013\5249013-07-25-18-96572.cca
New Field:
/location/TELEDATA/2020/Aug/03/5249013/5249013-07-25-18-96572.wav
Oracle version Version 19.2.1.247
Thank you in advance
You can add a new column to your table named NewField:
Alter table TableName add NewField varchar(500);
Then update NewField by replacing some characters as you wish from ExistingField.
update TableName set NewField= replace(replace(existingfield,'\','/'),'.cca','.wav')
Here I have just replace '' with '/' and '.cca' with '.wav'.
To replace path also:
update TableName set NewField= '/location/TELEDATA/'||substr(replace(replace(existingfield,'\','/'),'.cca','.wav'),instr(replace(replace(existingfield,'\','/'),'.cca','.wav'),'/2020',1,1) + 1)
DB-Fiddle:
Schema and insert statements:
create table mytable (existingfield varchar(500));
insert into mytable values('
\\BRRNAKCL12\Audiofiles22\1Year\Diogarfngal_ZZZZZZZZZ\2020\Aug\03\5249013\5249013-07-25-18-96572.cca');
Add new column:
Alter table mytable add NewField varchar(500);
Update query:
update mytable set NewField= '/location/TELEDATA/'||substr(replace(replace(existingfield,'\','/'),'.cca','.wav'),instr(replace(replace(existingfield,'\','/'),'.cca','.wav'),'/2020',1,1) + 1)
Select query:
select * from mytable;
Output:
EXISTINGFIELD
NEWFIELD
\BRRNAKCL12\Audiofiles22\1Year\Diogarfngal_ZZZZZZZZZ\2020\Aug\03\5249013\5249013-07-25-18-96572.cca
/location/TELEDATA/2020/Aug/03/5249013/5249013-07-25-18-96572.wav
db<>fiddle here

SQL CHECK CONSTRAINT VARCHAR

create table table1
(
column1 varchar2(8)
check constraint column1_ch check ........
);
How do I do a check for a data that the first 4 char is a specific set of alphabets while the last 4 is numbers? and as well as a range of values.
examples, data can be ABCD2121, ABCD1111.
range - ABCD0001 to ABCD9999
So 'ABCD' is fixed while the numbers are changing.
I've foudn online about using '[]" to define the numbers but i'm not able to integrate it into my constraint.
Thanks
The easiest way is to do this using a regular expression:
alter table table1
add constraint chck_code check (regexp_like(column1, '(ABCD)[0-9]{4}') );
If you've got a fixed set of prefixes, use regexp_like and enumerate the prefix list:
alter table test_1
add constraint chk_col1 check(regexp_like(column1, '(ABCD|EFGH)[0-9]{4}'));
This will allow ABCD and EFGH as prefixes, followed by exactly 4 digits.
Your check condition should be as follows:
(column1 LIKE 'ABCD[0-9][0-9][0-9][1-9]')
Edit: Modified to use a set prefix vs. a range for the alpha characters.
Here's a solution using Microsoft SQL Server that illistrates this:
DECLARE #MyTable TABLE
(column1 varchar(8) check (column1 LIKE 'ABCD[0-9][0-9][0-9][1-9]'))
INSERT INTO #MyTable (column1)
SELECT 'ABCD0000'
UNION SELECT 'ABCD2121'
UNION SELECT 'ABCD1111';
SELECT *
FROM #MyTable;
INSERT INTO #MyTable (column1)
SELECT 'ABCD000A'; --<== Fails!
INSERT INTO #MyTable (column1)
SELECT 'ABCD221'; --<== Fails!

How to replace specific values in a oracle database column?

I am looking to replace values in a particular column. For example the following column values
column name
----------
Test1
Test2
Test3
Test12
should be (replacing est1 with rest1)
column name
----------
Trest1
Test2
Test3
Trest12
Use REPLACE:
SELECT REPLACE(t.column, 'est1', 'rest1')
FROM MY_TABLE t
If you want to update the values in the table, use:
UPDATE MY_TABLE t
SET column = REPLACE(t.column, 'est1', 'rest1')
If you need to update the value in a particular table:
UPDATE TABLE-NAME SET COLUMN-NAME = REPLACE(TABLE-NAME.COLUMN-NAME, 'STRING-TO-REPLACE', 'REPLACEMENT-STRING');
where
TABLE-NAME - The name of the table being updated
COLUMN-NAME - The name of the column being updated
STRING-TO-REPLACE - The value to replace
REPLACEMENT-STRING - The replacement
In Oracle, there is the concept of schema name, so try using this
update schemname.tablename t
set t.columnname = replace(t.columnname, t.oldvalue, t.newvalue);
I'm using Version 4.0.2.15 with Build 15.21
For me I needed this:
UPDATE table_name SET column_name = REPLACE(column_name,"search str","replace str");
Putting t.column_name in the first argument of replace did not work.

SQL Command to execute multiple times?

I have situations that I need to write multiple rows of the same value to setup some tables. Say I have to add 120 rows with two columns populated. I am looking for a shortcut, instead of having the Insert line repeated n times. How to do this?
In SQL Server Management Studio, you can use the "GO" keyword with a parameter:
INSERT INTO YourTable(col1, col2, ...., colN)
VALUES(1, 'test', ....., 25)
GO 120
But that works only in Mgmt Studio (it's not a proper T-SQL command - it's a Mgmt Studio command word).
Marc
How about
Insert Table( colsnames )
Select Top 120 #value1, #Value2, etc.
From AnyTableWithMoreThan120Rows
Just make sure the types of the values in the #Value list matches the colNames List
what about
insert into tbl1
(col1,col2)
(select top 120 #value1,#value2 from tbl2)
if in sql server 2008 . new in sql server 2008 to insert into a table multiple rows in a single query .
insert into tbl1
(col1,col2)
values
(#value1,#value2),(#value1,#value2),.....(#value1,#value2)
Put the values in an unused table for safe keeping. From there you can insert from this table to the tables you need to setup.
Create an Excel Spreadsheet with your data.
Import the speadsheet into Sql Server.
You can even try with something like this(just an example)
declare #tbl table(col1 varchar(20),col2 varchar(20))
; with generateRows_cte as
(
select
1 as MyRows
union all
select
MyRows+1
from generateRows_cte
where MyRows < 120
)
insert into #tbl(col1,col2)
select
'col1' + CAST(MyRows as varchar),'col2' + CAST(MyRows as varchar)
from generateRows_cte OPTION (MAXRECURSION 0)
select * from #tbl
Note:- Why not you are trying with Bulk insert into SqlServer from a dataset ? I didnot notice first that u have a front end too(VB)!

Referring to column values directly without using variables in T-SQL

Is there a way in T-SQL (SQL Server 2005) to assign a whole record to a record variable and then refer to the particular values using column names?
I mean, instead of:
select #var1 = col1,
#var2 = col2
from mytable
where ID = 1;
and referring to them as #var1 and #var2, something like
#record =
select col1, col2
from mytable
where ID = 1;
and referring to them like #record.col1 and #record.col2 .
I am beginner in t-sql, so hopefully the question is not too trivial.
You can create a table variable and select the whole resultset into it:
DECLARE #tt TABLE (col1 INT, col2 INT)
INSERT
INTO #tt
SELECT col1, col2
FROM mytable
WHERE id = 1
, but you cannot access its data except than in the SELECT query as well.
With pure TSQL (that it without custom datatypes) the thing you ask is impossible.
sounds like you are a programmer ... look at linq maybe as it does what you want.
You can use a temporary table and SELECT...INTO to avoid specifying the column names at the beginning :
SELECT Field1, Field2
INTO #TempTable
FROM MyTable
WHERE MyTable.MyID = 1
but of course you'll still need the FROM #TempTable part when referring to the column names.
SELECT Field1, Field2
FROM #TempTable
and of course to remember to drop the table at the end :
DROP #TempTable
The app code is where you'd normally refer to a single row at a time as a variable.
You could use XML, but you'd have to play with this...
DECLARE #MyRecord xml
DECLARE #Mytable TABLE (col1 int NOT NULL, col2 varchar(30) NOT NULL)
INSERT #Mytable (col1, col2) VALUES (1, 'bob')
select #MyRecord =
(SELECT *
from #Mytable
where col1 = 1
FOR XML AUTO)
SELECT #myRecord.value('./#col', 'int') --also #myRecord.value('#col', 'int')
--gives error
Msg 2390, Level 16, State 1, Line 12
XQuery [value()]: Top-level attribute nodes are not supported
Buried in the Transact SQL documentation I came across this restriction on variables:
Variables can be used only in expressions, not in place of object names or keywords.
Since you'd need to use an object name to qualify a column I don't believe that this is allowed.