How to query for err in MySQL? - sql

I added a column to my table that does not allow null. I guess that is why some of my rows now have real values in the column but some other rows now have (err) in the column. How can I query for this?
select * from Table where new_column = err
That doesn't work.

select * from Table where new_column='err'

select * from Table where new_column is null I think this will work, but my MySQL skills are rusty

Related

Updating a table row multiple values oracle 11g

I m struggling to update one column for a table with a sub query. I have a table where currently one of the values is null.
Currently I have:
UPDATE DW1_PURCHASES SET DW1_PURCHASES.TOTAL_AMT =
(
SELECT DW1_PURCHASES.QUANTITY * DW1_PRODUCTS.PRICE
FROM DW1_PURCHASES, DW1_PRODUCTS
WHERE DW1_PURCHASES.PRODUCT_ID = DW1_PRODUCTS.PRODUCT_ID
)
Although subquery returns data which I need to insert I get a error of single row subquery returns multiple rows.How do I basically shift subquery result to the table?
Thanks.
You don't have to JOINthe update table inside the sub-query. Just correlate the sub-query with update table
UPDATE DW1_PURCHASES
SET DW1_PURCHASES.TOTAL_AMT =
(
SELECT DW1_PURCHASES.QUANTITY * DW1_PRODUCTS.PRICE
FROM DW1_PRODUCTS
WHERE DW1_PURCHASES.PRODUCT_ID = DW1_PRODUCTS.PRODUCT_ID
)
Note : If your DW1_PRODUCTS table has duplicated PRODUCT_ID then even now there is a possibility to get the same error

Sql query to find records with column value is blank

I'm trying to find records with Column value is blank. as you can see in the table I have records with following values when I fire a Distinct query on the Column MOVE_STU.
now I can find all the record with column value related to (1,2,3,4 and 6) but I'm not able to find the records with Column Value related to (5). as there are Thousands of record in the table i'm not able to figure out how should I write query in order to get these records. Kindly help. Thanks in Advance. :)
Use trim and comparison with empty string to get records that contain only whitespaces:
SELECT *
FROM your_table
WHERE LTRIM(RTRIM(MOVE_STU)) = ''
Try something like this:
SELECT * FROM YourTable WHERE LTRIM(RTRIM(YourField)) = ''
This will give you all matches that are empty, or have only whitespace.
How about this ?
SELECT * FROM YourTable WHERE MOVE_STU = ''
SELECT * FROM YourTable WHERE ISNULL(MOVE_STU,'') = ''
You can try this:
If your datatype is int then you have to change in varchar
ALTER TABLE [tbl_name] ALTER COLUMN [MOVE_STU] [VARCHAR(50)];
then run the query:
SELECT * FROM [tbl_name] WHERE MOVE_STU IS NULL;

Get the non empty data from a sql_varient column

I have a sql table with sql_varient column. I want to query the table to get the records which has value. I used following query
SELECT *
from Table
WHERE sql_varient_column IS NOT NULL
But its return the data which has no data also. How can I achieve this
To get the row for column sql_varient_column is not null and not empty:
SELECT *
from Table
WHERE NULLIF(sql_varient_column, '') IS NOT NULL
Try this one:
SELECT * FROM Table Where sql_varient_column != ''
I think you want this.
you can try it
SELECT *
from Table
WHERE ISNULL(sql_varient_column,'') != ''

How can I write a SQL statement to update a column in a table from another column in the same table?

I have an oracle DB where a program is writing into two columns when it updates the table. The 2nd column is based on the value from the 1st column. Well over time people have hand edited the database and forgot to insert values into the 2nd column. I'd like to write a simple sql statement that updates all columns and syncs the 2nd column to the 1st column. I know there's some simple statement to do this. Doing a little googling I thought something like the following:
UPDATE suppliers
SET supplier_name = (
SELECT customers.name
FROM customers
WHERE customers.customer_id = suppliers.supplier_id
)
WHERE EXISTS (
SELECT customers.name
FROM customers
WHERE customers.customer_id = suppliers.supplier_id
);
However, that is between 2 different tables where I would be doing it on the same table.
The following works in SQL Server (haven't checked Oracle yet).
UPDATE SUPPLIERS SET Supplier_Name = CustomerName
I'd give this a try and see if it works...
If both columns are in the same table, you can use the simplest query:
UPDATE your_table
SET column1 = column2
WHERE column1 != column2;
This supposes that both columns are NOT NULL. If the columns are nullable however, use decode instead:
UPDATE your_table
SET column1 = column2
WHERE decode(column1, column2, 1, 0) = 0;
update tableName set col2 = col1

how to select a row where one of several columns equals a certain value?

Say I have a table that includes column A, column B and column C. How do I write I query that selects all rows where either column A OR column B OR column C equals a certain value? Thanks.
Update: I think forgot to mention my confusion. Say there is another column (column 1) and I need to select based on the following logic:
...where Column1 = '..' AND (ColumnA='..' OR ColumnB='..' OR ColumnC='..')
Is it valid to group statements as I did above with parenthesis to get the desired logic?
Unless I'm missing something here...
SELECT * FROM MYTABLE WHERE COLUMNA=MyValue OR COLUMNB=MyValue OR COLUMNC=MyValue
I prefer this way as its neater
select *
from mytable
where
myvalue in (ColumnA, ColumnB, ColumnC)
SELECT *
FROM myTable
WHERE (Column1 = MyOtherValue) AND
((ColumnA = MyValue) OR (ColumnB = MyValue) OR (ColumnC = MyValue))
Yes, it's valid to use parentheses. However, if you're searching multiple columns for the same value, you may want to consider normalizing the database.