SQL If not Null - sql

Excuse my attempt at this, very rusty with SQL.
When I run the following code:
"IF NOT ISNULL Then INSERT INTO [XX].[dbo].[XXX] end if(
I get the following error message "AN EXPRESSION OF NON-BOOLEAN TYPE SPECIFIED"
I have tried to find out how to solve this error with no luck.
Basically I want to insert into [xx] if the cell is 'NOT NULL'.

you're missing the cell value to compare:
try this:
IF NOT ISNULL <cellValue> Then INSERT INTO [XX].[dbo].[XXX] end if ...

Since you refer to "dbo" I guess you're working with Sybase or SQL Server. It looks like you want to insert a single row of values based on a variable at hand. The accepted answer may work in your specific case but I think the more generic solution is below:
insert into <table> (<column list>)
select <values to insert>
where <#variable or column_value> is not null
The accepted answer has syntax that is not valid for Sybase/SQL Server. If you prefer that approach it should look something like the below:
if <cellvalue> is not null begin insert into <table> (...) values (...) end

Related

Trigger to convert empty string to 'null' before it posts in SQL Server decimal column

I've got a front table that essentially matches our SSMS database table t_myTable. Some columns I'm having problems with are those with numeric data types in the db. They are set to allow null, but from the front end when the user deletes the numeric value and tries to send a blank value, it's not posting to the database. I suspect because this value is sent back as an empty string "" which does not translate to the null allowable data type.
Is there a trigger I can create to convert these empty strings into null on insert and update to the database? Or, perhaps a trigger would already happen too late in the process and I need to handle this on the front end or API portion instead?
We'll call my table t_myTable and the column myNumericColumn.
I could also be wrong and perhaps this 'empty string' issue is not the source of my problem. But I suspect that it is.
As #DaleBurrell noted, the proper place to handle data validation is in the application layer. You can wrap each of the potentially problematic values in a NULLIF function, which will convert the value to a NULL if an empty string is passed to it.
The syntax would be along these lines:
SELECT
...
,NULLIF(ColumnName, '') AS ColumnName
select nullif(Column1, '') from tablename
SQL Server doesn't allow to convert an empty string to the numeric data type. Hence the trigger is useless in this case, even INSTEAD OF one: SQL Server will check the conversion before inserting.
SELECT CAST('' AS numeric(18,2)) -- Error converting data type varchar to numeric
CREATE TABLE tab1 (col1 numeric(18,2) NULL);
INSERT INTO tab1 (col1) VALUES(''); -- Error converting data type varchar to numeric
As you didn't mention this error, the client should pass something other than ''. The problem can be found with SQL Profiler: you need to run it and see what exact SQL statement is executing to insert data into the table.

Visual Basic 6 sql error

I am trying to insert a user into my database using the following insert statement
insert into [user] (username,password,idnumber,address,phonenumber,isAdmin,vehicleid) values ('Langton','123456','63-222-78393','3 where reaod who','999300324',False,0)
But when I run the code it tells me there is a syntax error in my insert statement, what could be the problem.
HiYou missed the quotes for 'False'
The problem lies with your vehicleid, this has to have a value and '0' is an incorrect number for a record/field. Change this to '1' or more.

Syntax for insert data into table in SAP BODS

I tried this to insert data into table in SAP BODS, but it seems like it won't work :
BEGIN
sql('TEST_DB', 'INSERT INTO TEST_CODE VALUES ({$ID_NUMBER}, {$DATE}, {$NAME}))
END
Is there any missing syntax? I already search for the sql statement and followed them, but still can't work. Appreciate any help. Thanks.
SQL function needs two parameters .
1st is the DataStore Name and second being the query. I can't find any flaw in your sql function.May be values are not according to the datatypes of the columns.
Try using SQL transform instead of SQL function
,with SQL transform you can verify the syntax too.
Try the following syntax:
BEGIN
sql('TEST_DB', 'INSERT INTO TEST_CODE VALUES ( ([$ID_NUMBER]), ([$DATE]), ([$NAME]) ))
END
The correct syntax is:
BEGIN
sql('TEST_DB', 'INSERT INTO TEST_CODE VALUES ('|| $ID_NUMBER ||' , '||$DATE|| ' , '||$NAME||')');
END

Insert Operation Failed for SQL Server

I tried to insert some rows into SQL server database and throws an error:
Conversion failed when converting the varchar value 'null' to data type bit
Could anyone explain what is this for?
This is not in a program.
What you might be trying
INSERT INTO TESTTABLE(BITCOLUMN) VALUES('NULL')
What should it actually be..
INSERT INTO TESTTABLE(BITCOLUMN) VALUES(NULL)
If you want to insert a null value into a column, you need to write NULL, and not 'NULL' (wrapping it in quotes indicates that it is a VARCHAR containing the letters N, U, L L - not a null value).
That is what the error message is telling you: you're trying to insert the VARCHAR value 'NULL' into a column that will only accept bit data.
Change the 'NULL' in your insert query to NULL.
You need something like this:
INSERT INTO TableName(ColumnName) VALUES(NULL); --this is the right way
Instead of this:
INSERT INTO TableName(ColumnName) VALUES('NULL'); --this is the wrong way

Using Case statement within update clause - Sybase

The following is sybase code. Can someone see if the following is correct. I guess I'm missing out on the syntax somewhere
declare #test varchar(32)
select #test="/data/dump/team/"
update link
set link.value=
case when #test=substring(link.value,1,17)
then #test
when #test != substring(link.value,1,17)
value
end
where link.value != ""
and link_id=0 and row_id = 462135
As it is give me the following error: "Incorrect syntax near keyword end on line 10."
Can please someone help me with the syntax.
Try adding "then" to the second case:
declare #test varchar(32)
select #test="/data/dump/team/"
update link
set link.value=
case when #test=substring(link.value,1,17)
then #test
when #test != substring(link.value,1,17)
then value
end
Why not simply do an "else" for the second "when" ?