SQL: Not equal operator Problem - asp.net

I am using a not equal operator <> in my sql statement but it doesn't retrieve any record which is not equal to the selected date.
CODE:
Command = New SqlCommand("SELECT * FROM [Products] WHERE [ParkingStartDate] <> #StartDate", myConn)
Command.Parameters.AddWithValue("#StartDate", StartDate1)

This won't return anything if either of the following is true:
StartDate1 is a NULL
ParkingStartDate for all values is a NULL or equal to StartDate1 (obvious one)
Check that you are passing a non-NULL value in StartDate1 and there are records satisfying your condition.

If the values are null you would have to do
Command = New SqlCommand("SELECT * FROM [Products] WHERE [ParkingStartDate] <> #StartDate OR ParkingStartDate is null", myConn)
Command.Parameters.AddWithValue("#StartDate", StartDate1)

First stop using that <> operator.
Use instead != (NOT EQUAL)
run this statement in sql. it will return zero results. to illustrate my point.
select '1' where NULL <> 0
instead use
where columname != #startdate or columnname is null

One important thing to take into consideration when dealing with querying based on date is that the date in SQL Server is treated as exact as the date you send in. So, if you pass in a full date/time, like 2011-10-24 14:35:29, it will return all dates that are not that exact date. If you are looking for a particular portion of that date to be selected against, you need to only give that portion of the date. Using the DATEPART command will help here also.

If the value is undefined, it is not included in <> or != clause.
Along with these you can use sql function 'COALESCE()' to include rows having undefined cells.
"SELECT * FROM [Products] WHERE COALESCE([ParkingStartDate],'') <> #StartDate OR ParkingStartDate is null"
Hope it will help you.

My recommendation would be to try with NULLIF operator. Modify your query to be like :
SELECT * FROM [Products] WHERE NULLIF([ParkingStartDate], #StartDate) IS NOT NULL OR ParkingStartDate is NULL
Hope this helps.

Related

BigQuery SQL - Change 0 to NULL, change NUMERIC to STRING type and PARSE DATETIME (single query)

I have a requirement where the current type is NUMERIC but I need it to be DATETIME, I am currently working with the data owner to get them to convert the data type so I will not have to perform these steps.
As stated above, the NUMERIC type needs to be changed to DATETIME but I have an issue where zero values exist. I have currently written two SQL queries to achieve the result but I was hoping to be able to do this in a single query.
See the below two queries:
Query 1
SELECT
MATERIAL,DESC,NUMBER,
CASE
WHEN START_ACTUAL = 0 THEN NULL
ELSE START_ACTUAL
END AS START_ACTUAL,
CASE
WHEN END_ACTUAL = 0 THEN NULL
ELSE END_ACTUAL
END AS END_ACTUAL,
FROM `SAMPLEFILE1`
Query 2
SELECT
MATERIAL,DESC,NUMBER,
PARSE_DATETIME('%Y%m%d%H%M%S', CAST(START_ACTUAL AS STRING)) AS START_ACTUAL,
PARSE_DATETIME('%Y%m%d%H%M%S', CAST(END_ACTUAL AS STRING)) AS END_ACTUAL,
FROM `SAMPLEFILE1_VIEW`
I'm sure someone will put me right straightaway and it is very simple :)
Thanks in advance
You could wrap your case statements in the CAST and PARSE_DATETIME, but you could just use nullif:
SELECT
MATERIAL,DESC,NUMBER,
PARSE_DATETIME('%Y%m%d%H%M%S', CAST(NULLIF(START_ACTUAL,0) AS STRING)) AS START_ACTUAL,
PARSE_DATETIME('%Y%m%d%H%M%S', CAST(NULLIF(END_ACTUAL,0) AS STRING)) AS END_ACTUAL,
FROM `SAMPLEFILE1_VIEW`

SSIS Merge Statment Datetime not updated

I am using Merge Statement in my SSIS package. The problem is that it doesn't update the datetime column when i run the package. It inserts the datetime correctly but doesn't update them from NULL to some datetime if a new datetime is available in source database.
Both source and destination has same column type (datetime(2),null).
I am using the code below in SQL Task after truncating staging table.
MERGE abc.dbo.invoices AS targe
USING (SELECT
ID
,cash_received_date
,schedule_datetime
,delivery_date
FROM Staging.dbo.tmpabcinvoices) AS sourc
ON targe.id = sourc.id
WHEN MATCHED and
targe.schedule_datetime <> sourc.schedule_datetime
or
targe.delivery_date <> sourc.delivery_date
or
targe.cash_received_date <> sourc.cash_received_date
THEN UPDATE SET
,targe.schedule_datetime=sourc.schedule_datetime
,targe.delivery_date=sourc.delivery_date
,targe.cash_received_date=sourc.cash_received_date
WHEN NOT MATCHED THEN
INSERT(
old_invoiceid
,cash_received_date
,schedule_datetime
,delivery_date
)
VALUES
(
sourc.old_invoiceid
,sourc.cash_received_date
,sourc.schedule_datetime
,sourc.delivery_date
);
GO
You have a comma which shouldn't be there at the start of this line:
,targe.schedule_datetime=sourc.schedule_datetime
Also, you'll need to add this to take care of the NULLs:
targe.schedule_datetime <> sourc.schedule_datetime
or (targe.schedule_datetime IS NULL AND sourc.schedule_datetime IS NOT NULL)
or targe.delivery_date <> sourc.delivery_date
or (targe.delivery_date IS NULL AND sourc.delivery_date IS NOT NULL)
or targe.cash_received_date <> sourc.cash_received_date
or (targe.cash_received_date IS NULL AND sourc.cash_received_date IS NOT NULL)
While ANSI_NULLS is set to ON, NULLs are basically unknowns, so they can't be evaluated to either 'equal to' or 'not equal to'.

Corona SQLIte : Checking if a value exists or not

I would like to check if a value can be returned or not by my SQL request:
Search_IDItem = "SELECT * FROM giftshop WHERE id ="..item_id..""
for row_2 in db:nrows(Search_IDItem) do (..)
CheckInventory = "SELECT * FROM inventory WHERE code ="..row_2.code..""
if CheckInventory ~= nil then
print(row_2.code)
updateItemsCode(row_2.code, "inventory", "qtyoninventory", row_2.qtyoninventory+1)
else
insertInventory(2,row_2.code, row_2.name, row_2.src, row_2.desc, "no",row_2.qtyoninventory,row_2.price,row_2.usetxt)
end
The error is:
near "=": syntax error
Basicly, I would like to know if the value exist, I'll only update the field "quantity", if not, I will create the new item.
Unfortunately, it doesn't work! Is there any advice or solution?
Syntax error is a result of misunderstanding of your query by the SQL.
One of the most often reasons is incorrect string insertion. So I suppose that row_xx.code is a string
To solve the problem you should "quote" row_xx.code by ' symbol like this:
CheckInventory = "SELECT * FROM inventory WHERE code ='"..row_2.code.."'"
Update
To solve the second part of the problem and figure out if value exists I suggest following
In old good plain Java when I get SQLite Cursor I may check if it has rows at all like this:
if(cursor.moveToFirst())
// it has at least one value
else
// it hasn't values at all
Sure there is similar thing for Corona
You also may use following approach to update record (creating if not exists):
// create record IF NOT EXISTS:
String createNonExistentRec = "Insert or ignore into "...// insert key/unique values
db.execSQL(createNonExistentRec, ...);
// update values:
String updateQuery = "..."
...
I found a solution :
for x in db:urows "select count(*) from inventory" do
if x>0 then -- The inventory is empty
for w in db:urows("SELECT count (*) FROM inventory WHERE code ='"..row_2.code.."'") do
--The item is already in the inventory
if w > 0 then
CheckInventory = "SELECT * FROM inventory WHERE code ='"..row_2.code.."'"
for row_4 in db:nrows(CheckInventory) do
updateItemsCode(row_4.code, "inventory", "qtyoninventory", row_4.qtyoninventory+1)
end
else
--The item is not in the inventory, so we add it!
for maxid in db:urows("SELECT MAX(id) FROM inventory") do
insertInventory(maxid+1,row_2.code, row_2.name, row_2.src, row_2.desc, "no",1,row_2.price,row_2.usetxt)
end
end
end
else
insertInventory(1,row_2.code, row_2.name, row_2.src, row_2.desc, "no",1,row_2.price,row_2.usetxt)
end
end

<SQLITE> compare old value and new value

I have a trigger.
I want to do insert changed values after every update.
But I get syntax error on line IF .. THEN, and I can't find example.
IF (IFNULL(NEW.symbol,'') <> IFNULL(OLD.symbol,'')) THEN
INSERT INTO LOG(old_value, new_value, DATA, user)
VALUES ('Symbol: '|| IFNULL(OLD.symbol,''), 'Symbol: ' (IFNULL(NEW.symbol,''), CURRENT_TIMESTAMP, id_user)
Can You help me?
SQLite has no IF statement.
You must put the condition into the WHEN clause of the trigger.

SQLite SELECT statement where column equals zero

I'm preety new to SQLite.
I have a preety basic question.. Why can't I select rows where specific column equals zero?
The is_unwanted column is type TINYINT (which I see in SQLite basically means INTEGER)
So, I have only one record in the database (for testing).
When I try
SELECT is_unwanted FROM 'urls'
I get a result of "0" (zero), which is fine because that column contains the actual number 0.
I tried =>
SELECT * FROM 'urls' WHERE is_unwanted = 0
And got NO result, but
SELECT * FROM 'urls' WHERE is_unwanted <> 0
gives me result.
What am I doing wrong??
Try running
select '{' || is_unwanted || '}' from urls
to see if the value in the database is really a string containing spaces.
SQLite is a dynamically typed database; when you specify TINYINT is is a hint (SQLite uses the term "affinity") for the column. You can use
select is_unwanted, typeof(is_unwanted) from urls
to see the values with their types.
You could try:
SELECT * FROM urls WHERE coalesce(is_unwanted,'') = ''

Resources