How do I build a SQL insert query that allows me to insert a blank into a numeric field? - sql

I have some values from an Excel workbook that I'm adding into an array. Eventually, I loop through the array and build a sql insert query (using Excel VBA) with the values stored into the array.
I debugged the sql query string I built and it appears as this:
INSERT INTO RAPTOR_BG.Project_Attributes (
P-Key,
Contract_Currency,
FX_Rate,
Rate_Discount,
Study_Award,
BLD,
Submitted_Date
)
VALUES (
'1111111|7-22-2020',
'USD',
'0.91',
'',
'5-29-2019',
'',
'7-22-2020'
);
Both Rate_Discount and BLD are numeric and won't accept ' '. Is there a way to get sql to accept this blank as a null? I did some research and I saw that some people have had success using a case statement, but all of the examples I've seen use variables or something like this (#SomeName) found in the answer here.
I've tried adapting my code to fit the examples I've seen by doing the below, but it kicked an error stating that at least one of the result expressions in a CASE specification must be an expression other than the NULL constant.
*insert statement from before*
VALUES (
'1111111|7-22-2020',
'USD',
'0.91',
CASE WHEN '' is null then null end,
'5-29-2019',
'',
'7-22-2020'
);
I'm going to be running my code on several hundred workbooks and some of these values will be blank and others won't be so I can't necessarily hardcode a null instead of the ' '. Is there a way to work around getting the numeric fields to accept a blank as null?

Ok, I figured this one out. Please find the answer below.
INSERT INTO RAPTOR_BG.Project_Attributes (
P-Key,
Contract_Currency,
FX_Rate,
Rate_Discount,
Study_Award,
BLD,
Submitted_Date
)
VALUES (
'1111111|7-22-2020',
'USD',
'0.91',
CASE WHEN '' is null then 0 end,
'5-29-2019',
CASE WHEN '' is null then 0 end,
'7-22-2020'
);

Related

How to divide the text into two columns by matching text using SQL select query

I have table with a column Note / Reason - like this:
Note / Reason
test
test2
test REASON:ANOTHER PROVIDER
this is a test REASON:NO FITTER
I want to divide the text like below into note and reason like below reason with start with reason and other text will be note
Note Reason
---------------------------------------------------
test
test2'
test REASON:ANOTHER PROVIDER
this is a test REASON:NO FITTER
Using Sean's sample data, as I mentioned in the comments, use CHARINDEX, LEFT and STUFF:
SELECT LEFT(NoteReason,CHARINDEX('REASON:',NoteReason + 'REASON:')-1) AS Note,
STUFF(NoteReason,1,CHARINDEX('REASON:',NoteReason)-1,'') AS Reason
FROM #NoteReason;
Considering you have extra white space, you may also want to wrap the expressions in a TRIM.
It would be better if you provided sample data in a consumable format. That way it is easy for others to use and it is also more precise so others aren't guessing or making assumptions about your tables and sample data. Given the sparse information in your question something like this should be somewhat close.
declare #NoteReason table (NoteReason varchar(100))
insert #NoteReason values
('test')
, ('test2')
, ('test REASON:ANOTHER PROVIDER')
, ('this is a test REASON:NO FITTER')
select Note = case when charindex('REASON', n.NoteReason) = 0 then n.NoteReason
else left(n.NoteReason, charindex('REASON', n.NoteReason) - 1)
end
, Reason = case when charindex('REASON', n.NoteReason) > 0 then substring(n.NoteReason, charindex('REASON', n.NoteReason), len(n.NoteReason)) else '' end
from #NoteReason n

Replace null values in cell

I am unable to replace null values in cells. I have created a facet to only display cells that have null values. I then went to edit cells > Transform function and tried to use the replace function but it does not seem to be working.
Different things I have tried:
replace(value, null, 'other_text')
replace(value, 'null', 'other_text')
I expected the null values to be replaced with 'other text'
Screenshot:
You are not replacing the value null, but the string 'null'. The correct syntax for replacing is value.replace('old','new') or replace(value,'old','new'). But replacing doesn't work on null. You should either create a facet for null-values (your current screenshot shows some non-null-values) and fill the expression field with 'new' or you could do something like if(value==null,'new',value).

Insert a percent in SQL to MS Access

Similar to CONCAT equivalent in MS Access, I want to insert a string including a percentage symbol.
INSERT INTO Financier_Fp_line (FP_Id, Item_, Creditor, Description, Payment_Type, Actual_Amount, Note_)
VALUES (
'86',
'2',
"",
'BFA 10%',
"Set-aside",
'20',
''
)
is not working.
My original text was BFA 10%.
I've tried concatenating as above using "+" and "&". I get the same error with each method.
Data type mismatch in criteria expression.
Any clues anyone?
Edit 2019.05.10: In re-creating the sql (as requested, now shown above), either oledb or Access itself decided to not interpret the % as a wild-card, and the insert is working fine. Go figure.
Thanks people.
This will work:
INSERT INTO myTable (myFields)
VALUES (myValue1,...,'Bfa 10%')
and:
INSERT INTO myTable (myFields)
VALUES (myValue1,...,'Bfa 10' & Chr(37))
This should work:
select . . . , "Bfa 10" & "%"
I'm not sure why you would want to do this instead of the simpler "Bfa 10%", but that should work as well.

Error Inserting values in Dreamer Table

I'm sorry if this is a simple question but I'm rather new to SQL and I'm having an issue trying to insert some values in a table.
The query I'm executing is the following:
INSERT INTO Dreamer VALUES (
'', 'Dreamer name', '0', '1554542121', 'pablogardiazabal#gmail.com',
'Dreamer FB', 'Dreamer TW', 'Dreamer', 'M', '', 0, 0, 'Dreamer DAD',
'Dreamer MOM', '0', '0', '151515131321', '545343512123',
'DreamerDAD#daddreamer.com', 'DreamerMOM#momdreamer.com',
'Dreamer DAD DIR', 'Dreamer MOM DIR', '1515312123123',
'5456453423', 0)
And the table is designed like this:
TABLE This is the Table
Thanks a lot guys and gals!
EDITTED (sorry about the wrong format)
The error I'm getting is the following
Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.
There is mismatch in your order of values, It is better to provide your insert script along with column names and corresponding values as below:
INSERT INTO Dreamer (col1, col2...) VALUES (val1,val2...)
There is no shortcut to doing the dirty work of comparing every string value in your select list with the definition of the associated column. And you REALLY need to learn how to write literials of a given datatype. '' is not really a valid integer - if you want zero than use the correct literal. And the integer zero is not the same as NULL. Do not start using a bad habit of equating special values with NULL - it will eventually bite you.
The first problem I see is '151515131321' - which is to be assigned to the column DocNumberP : varchar(10). That is more than 10 characters.

Oracle : IN and OR

I've a scenrio which process many data in Oracle database. In some cases, the variable Sec_email will contain many values and in some cases Sec_email will contain null or ' '.
so can please any one tell me how to write a query for this?
I tried with
(C.SECONDARY_EMAIL IN ('?,?') OR '' = '' )
where C is the Client table.
When I use this i get the count as 0.
You can perform a not null check before the IN comparison like
Sec_email is not null and C.SECONDARY_EMAIL IN (...
One obvious problem is that Oracle (by default) treats empty strings as NULL. So: '' = '' is the same as NULL = NULL, which is never true.
Arrgh.
In any case, you are probably constructing the query, so use is null instead:
(C.SECONDARY_EMAIL IN ('?,?') OR '' IS NULL
I think the real problem, though, is the first comparison. The IN list has one element with a constant, not two (but perhaps that is your intention). If you want to put a variable number of values for comparison, one method uses regular expressions. For instance:
C.SECONDARY_EMAIL REGEXP_LIKE '^val1|val2|val3$' or '' IS NULL
If you would like to get a list of values when some of them is null you should use:
("some other conditions" OR C.SECONDARY_EMAIL IS NULL)
The question is if it is not null and not ' ' value what you are expecting, if it should be some king of
pattern you should use regular expression:
regexp_like(C.SECONDARY_EMAIL, '^(.+?[,]+?)+$')
Also, if you have a few conditions in where clause use should use brackets to group you conditions null check and another one.
All conditions i this case will be divided by OR.
(C.SECONDARY_EMAIL IS NULL OR regexp_like(C.SECONDARY_EMAIL, '^(.+?[,]+?)+$'))
or
(C.SECONDARY_EMAIL IS NULL OR regexp_like(C.SECONDARY_EMAIL, '^(.+?[,]+?)+$')
OR C.SECONDARY_EMAIL = ' ')