Insert a percent in SQL to MS Access - sql

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.

Related

SUBSTRING() function behaving differently in SELECT and WHERE clause? [duplicate]

This question already has an answer here:
Why is it that a change in query plan suddenly seems to break a query
(1 answer)
Closed 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I have a table with the following column:
server
SLQ-ABCD-001
SLQ-ABCA-002
SLP-DBMSA-003
SLD-ABC-004
SLS-123456-005
I would like to be able to filter the rows based on a substring of this column, specifically, the string between those hyphens; there will always be three characters before the first hyphen and three characters after the second hyphen.
Here's what I have tried:
AND substring(server, 5, (len(server)-8)) in ('ABC', 'DBMSA')
AND substring(server, charindex('-', server)+1,(charindex('-',server, charindex('-', server)+1)-(charindex('-', server)+1))) in ('ABC', 'DBMSA')
Both of these work perfectly fine (expected substrings obtained) when used in the SELECT clause but give the error below
Invalid length parameter passed to the LEFT or SUBSTRING function.
I am not able to use the more simpler way, AND server like '%ABC%' as I have more than one combination of characters I'm looking for and also, because that comma separated list will be dynamically parsed in that query for this use case.
Is there any way this type of filter can be achieved in SQL Server?
EDIT
After #DaleK helped me realize that the issue might be I might have some bad data (server names with length < 8) and that I might have missed them when I tested the expression in the SELECT clause since I might have some other filters in my WHERE clause, here's how I had managed to get around that
SELECT *
from
(SELECT *
from my_original_table
where
--all my other filters that helped me eliminate the bad data
) my_filtered_table
where substring(server, 5, (len(server)-8)) in ('ABC', 'DBMSA');
As for the "question being duplicate" part, I think the error in that question is encountered in SELECT statement where as in my case, the expression worked fine in the SELECT statement and only errored when used in the WHERE clause.
For solution, the one provided by #Isolated seems to work perfectly fine!
One simpler approach you can try is the (often misused) parsename function:
Example being
with sampledata as (
select * from (
values('SLQ-ABCD-001'),('SLQ-ABCA-002'),('SLP-DBMSA-003'),('SLD-ABC-004'),('SLS-123456-005')
)x([server])
)
select [server]
from sampledata
cross apply(values(Replace([server], '-','.')))v(v)
where ParseName(v,2) in ('ABC', 'DBMSA');
No need for substring. You could nest left and right with len such as this:
with my_data as (
select 'SLQ-ABCD-001' as server union all
select 'SLQ-ABCA-002' union all
select 'SLP-DBMSA-003' union all
select 'SLD-ABC-004' union all
select 'SLS-123456-005'
)
select server
from my_data
where left(right(server, len(server) - 4), len(right(server, len(server) - 4))- 4) in ('ABC', 'DBMSA')
server
SLP-DBMSA-003
SLD-ABC-004
And left(right(server, len(server) - 4), len(right(server, len(server) - 4))- 4) works fine in the select clause too.

Unable to cast string to date when inserting on Databricks SQL

I have a table created like so :
CREATE TABLE IF NOT EXISTS MyDataBase.Table (
`date` DATE,
`name` STRING,
`isTarget` BOOLEAN
) USING DELTA LOCATION '/mnt/path/to/folder/'
I need to manually insert values in the table using SQL. I tried to do it like so :
INSERT INTO CalendrierBancaire.Data VALUES (
('2022-01-01', 'New Year', True)
)
But it fails on this error :
Error in SQL statement: AnalysisException: cannot resolve 'CAST(`col1` AS DATE)' due to data type mismatch: cannot cast struct<col1:string,col2:string,col3:boolean> to date; line 1 pos 0;
I also tried to replace the date string with :
CAST('2022-01-01' AS DATE)
to_date('2022-01-01', 'yyyy-MM-dd')
But neither worked and returned the same error. It looks like the SQL parser wants to convert the whole row to date which is stupid. Do you have any idea how I can do it ?
Thanks.
PS : In real usage I have almost 30 to 40 lines to insert so letting a variable with the CAST expression while be painful to realize for all values.
It looks like the problem is that you have additional brackets around values that you want to insert, so it's interpreted as a single column - you need to use following syntax (see docs):
INSERT INTO CalendrierBancaire.Data VALUES
('2022-01-01', 'New Year', True)
or if you have multiple rows to insert, then list them via comma:
INSERT INTO CalendrierBancaire.Data VALUES
('2022-01-01', 'New Year', True),
('2022-02-01', 'New Year 2', True)
P.S. Although it works for me just fine even with extra brackets

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

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'
);

Conditional update query to pad alphanumeric values in MS Access

I have an Access table with a field that contains alphanumeric values (1234, 123A, 12A34, ABC3, etc). I am trying to create a conditional update query to add leading zeros to bring all values that contain at least 1 letter up to five characters but none to the only numeric values (eg 123, 00A12, 0000X).
My current code looks like:
UPDATE MyTable SET MyTable!Field = Format(Field, String(5, "0")) WHERE MyTable!Field LIKE '*[A-Z]*'
When I run the query, I don't get any error messages but it also fails to add any leading zeros.
I've also tried Format(Field, "00000") using Not Like and '*[0-9]*' or '*[0123456789]*' etc.
Interestingly, when I run a query by itself to select any of the values containing a letter (Like '*[A-Z]*'), it correctly pulls all 1000 values that need to be updated but when I add the conditional, it fails. Similarly, I've been successful in the past with adding leading zeros the entire field using Format(Field), String(5, "0") but it also fails when I add a conditional.
I'm pretty new to Access and SQL, so I feel like I've probably misunderstood the syntax somewhere. Or is there something else I should be doing?
Format() is wrong function to use.
If every value in field is 5 characters or less, consider:
UPDATE MyTable SET Field = String(5-Len(Field), "0")) & Field WHERE Not IsNumeric(Nz(Field,0))

Error converting data type varchar to numeric. SQL Server INSERT statement

I am trying to insert some data into this table, but I get this error:
Msg 8114, Level 16, State 5, Line 3
Error converting data type varchar to numeric.
I tried to remove the quotes, but it is still showing me the same error:
USE CobornSalesDB;
GO
INSERT INTO SalesActivity
VALUES ('AC00001', '05-12-2016', 'AG16170', 'C000001', 'P0001', 'S00002'‌​, '1', '200000.00', '',‌ ​'1.2220', '20', '100000.00', '25-12-2016', '30-12-2016', '31-12-2016', 'A00‌​0001', 'PR00001');
GO
In all of your numeric columns, take the commas out of the values you are inserting.
This is your query taken from the comment.
You really should include it in the question as text, not as image.
INSERT INTO SalesActivity VALUES
('AC00001',
'05-12-2016',
'AG16170',
'C000001',
'P0001',
'S00002'‌​,
'1',
'200000.00',
'',‌ -- valueEUR
​'1.2220',
'20',
'100000.00',
'25-12-2016',
'30-12-2016',
'31-12-2016',
'A00‌​0001',
'PR00001');
In the definition of the table we can see that valueEUR column is numeric. You are passing a string there. Not just a string, but a string that can not be converted into a number. An empty string '' can't be converted into a number.
I'm guessing, you want to insert NULL in that field. So, you should
write NULL in the INSERT statement.
Also, you should remove all quotes around numbers, so that the server would not have to convert strings into numbers.
Also, you should write your dates in YYYY-MM-DD format. Otherwise, one day you may be surprised to see that server guessed it incorrectly and swapped month and day.
Also, you should list all column names in the INSERT statement. Otherwise your code will break when you add a new column to the table.
The query should look similar to this:
INSERT INTO dbo.SalesActivity
(Activity_ID,
[Date],
Quatation_Number,
Customer_ID,
Product_ID,
Status_ID,
Quantity,
valueGBR,
valueEUR,
Rate,
Commission,
weightedValue,
estDecisionDate,
currentEstCompletionDate,
originalEstCompletionDate,
Agent_ID,
Probability_ID)
VALUES
('AC00001',
'2016-12-05',
'AG16170',
'C000001',
'P0001',
'S00002'‌​,
1,
200000.00,
NULL,‌
​1.2220,
20,
100000.00,
'2016-12-25',
'2016-12-30',
'2016-12-31',
'A00‌​0001',
'PR00001');
Try entering date in mm/dd/yyyy format like this
INSERT INTO SalesActivity VALUES ('AC00001','12-05-2016','AG16170','C000001',
'P0001','S00002'‌​,1,200000.00,'',‌ ​1.2220, 20,100000.00,
'12-25-2016','12-30-2016','12-31-2016','A00‌​0001','PR00001');