Excel - Generate INSERT Statements and Handle Single Quote - sql-server

I have the following formula in an Excel spreadsheet:
="INSERT INTO #dreams VALUES('"&B3&"','"&C3&"','"&D3&"','"&E3&"','"&F3&"','"&G3&"','"&H3&"','"&I3&"')"
Works fine, basically, unless I have data like this in, for example, in D3.
US INVESTMENTS' GRADE CORPORATE BOND FUND (QUALIFIED)
In this case, the INSERT statement looks like this:
INSERT INTO #dreams VALUES('2019039','550678','US INVESTMENTS' GRADE CORPORATE BOND FUND (QUALIFIED)','F','5f','Hirofumi Nakamura','ACBD1','N')
That single quote will be interpreted as a delimeter. I need to have US INVESTEMENTS''
That is, substitute two single quotes for the one single quote.
I have tried various things, but can't get it to work.
EDIT: Would something similar to this work?
="INSERT INTO #dreams VALUES('"&B3&"','"&C3&"',=SUBSTITUTE(D2,"'","''"),'"&E3&"','"&F3&"','"&G3&"','"&H3&"','"&I3&"')"

Wrap each of the column references with SUBSTITUTE and replace ' with ''. For example SUBSTITUTE(B3,"'","''").

Using what #Rory said, this works:
="INSERT INTO #dreams VALUES('"&B3&"','"&C3&"','"&SUBSTITUTE(D3,"'","''")&"','"&E3&"','"&F3&"','"&G3&"','"&H3&"','"&I3&"')"

Related

SQL Pattern matching not giving the correct output

I am trying to find a certain set of characters in a column from a datatable. I have tried the pattern that seems more logical to me (right below) but it doesn't seem to be doing the job. What I wish to achieve is a pattern where I have something like '["5"]', basically with: square brackets, quotation marks, any integer number, quotation marks, square brackets. The output I am getting is just empty, and I can't seem to undersand why. Besides this, I would like to update the records that do not follow this pattern to follow it. Does anyone have a solution for this?
To give you some context, here is the test table:
I want to achive only the last three records.
Here is what I have tried:
SELECT ToJsonTestValue
FROM Test
WHERE ToJsonTestValue LIKE '["%"]'
and
UPDATE dbo.Test
SET ToJsonTestValue = '["'+ToJsonTestValue+'"]'
WHERE ToJsonTestValue LIKE '#';
You have a couple of problem here. Firstly you have the square brackets, which needs escaping. Then you also use % which is a multi character wildcard, however, it appears that you want a single character. It also appears that that character can only be an integer, so you might want to be more specific. Either of these should give you the result you want:
--Using single character wildcard:
SELECT *
FROM (VALUES('["1"]'),('["["1"]"]'))V(S)
WHERE V.S LIKE '[[]"_"[\]]' ESCAPE '\';
--Specifically requiring int:
SELECT *
FROM (VALUES('["1"]'),('["["1"]"]'))V(S)
WHERE V.S LIKE '[[]"[0-9]"[\]]' ESCAPE '\';

Use columns (dataset fields) with whitespace in Report Builder

Can I have whitespace in a column name that I use in a dataset inside of Report Builder? E. g. from a query like
SELECT col as [red cards] FROM table
I get an error similar to (due to translation)
<Name>
Please enter a valid name. The name cannot contain any spaces...
Here it is suggested to use underscores instead, but google shows precious little on the topic.
I am using the query somewhere else as well, so an as-descriptive-and-readable-as-possible name would be more desirable.
What you have should work (I've just tested this it worked for me) with something this simple. I have sometimes had issues with the use of alias in queries which have multiple joins.
If it still doesn't want to work, try RedCards or Red_Cards.

nested if statment with so many text conditions ERROR

this is my data table
I'm writing this formula in openoffice not excel, that's why you will see ";" instead of ","
my questions is that I'm trying to put the currency of each country's capital name, and I did it but the thing is that I'm unable to make more than 42 conditions!!!!!
Is there another way or another formula can I use???
Here is the formula I did, and it's working
=IF(D3="AMSTERDAM";"EUR";IF(D3="FRANKFURT";"EUR";IF(D3="OSLO";"NOK";IF(D3="COPENHAGEN";"MULTI";IF(D3="ALICANTE";"EUR";IF(D3="BARCELONA";"EUR";IF(D3="BERLIN TXL";"EUR";IF(D3="VILNIUS";"EUR";IF(D3="BRUSSELS";"EUR";IF(D3="CATANIA";"EUR";IF(D3="DUSSELDORF";"EUR";IF(D3="FARO";"EUR";IF(D3="GRAN CANARIA";"EUR";IF(D3="HELSINKI";"EUR";IF(D3="MALAGA";"EUR";IF(D3="MUNICH";"EUR";IF(D3="PARIS CDG";"EUR";IF(D3="RIGA";"EUR";IF(D3="SANTA CRUZ PALMA";"EUR";IF(D3="SEVILLA";"EUR";IF(D3="TENERIFE";"EUR";IF(D3="BUDAPEST";"HUF";IF(D3="ANTALYA";"TRY";IF(D3="GAZIPASA";"TRY";IF(D3="ISTANBUL";"TRY";IF(D3="BERGEN";"NOK";IF(D3="STAVANGER";"NOK";IF(D3="STAVANGER VIA ESBJERG";"NOK";IF(D3="LONDON CITY";"GBP";IF(D3="LONDON LHR";"GBP";IF(D3="LONDON STN";"GBP";IF(D3="MANCHESTER";"GBP";IF(D3="FUERTEVENTURA";"ISK";IF(D3="LANZAROTE";"ISK";IF(D3="PORTO SANTO";"ISK";IF(D3="GLASGOW";"SCP";IF(D3="GDANSK";"PLN";IF(D3="CLUJ­NAPOCA";"RON";IF(D3="STOCKHOLM";"SEK";IF(D3="PRAGUE";"CZK";""))))))))))))))))))))))))))))))))))))))))
I'd suggest you use a table in another section of your spreadsheet then use VLOOKUP to match the currency to your country.
=VLOOKUP(D3;Currency_Table;2;FALSE}
Which is lookup D3 in the table named Currency_Table and return the exact match (from FALSE) in the second column which will give you your currency.
Or if you want the formula to exist without dependency upon another table you could use something like:
=VLOOKUP(D3;{"AMSTERDAM"\,"EUR";"FRANKFURT"\,"EUR";"OSLO"\,"NOK"; etc...};2;FALSE}
NB: I've added an escape \ before the comma because I'm assuming you are from a language area that uses , as a decimal by your language settings I'm assuming you'll need that in your array for that to work.

Identify all strings in SQL Server code (red color - like in SSMS)

I was not able to solve this by myself so I hope I didn't miss any similar post here and I'm not wasting your time.
What I want is to identify (get a list) of all strings used in SQL Server code.
Example:
select 'WordToCatch1' as 'Column1'
from Table1
where Column2 = 'WordToCatch2'
If you put above code to SSMS all three words in apostrophes will be red but only words 'WordToCatch1' and 'WordToCatch2' are "real" strings used in code.
My goal is to find all those "real" strings in any code.
For example if I will have stored procedure 10k rows long it would be impossible to search them manually so I want something what will find all those "real" strings for me and return a list of them or something.
Thanks in advance!
The trouble is, Column1 is nothing particular different compared to WordToCatch1 and WordToCatch2 - not unless you parse the SQL yourself. You could modify your query to take the quotes away from Column1 and it will show up coloured black.
I guess a simple regex will show up all identifiers after an AS keyword, which would be easier than fully parsing SQL, if all the unwanted strings are like that, and its not just an example.

How can I enter an e-mail address with a comma in it into a cell?

I'm trying to insert an email address with a comma in it (and it has to be entered that way) and I'm currently stumped on how to properly insert the exact text into this cell.
The e-mail address that needs to be entered as/is:
joe,.E.Blow#someemail.com
What I've tried is work different escape patterns into the code. For example, this...
'''joe'''+','+'''.E.Blow#someemail.com'''
gets entered into the database as...
'joe','E.Blow#someemail.com'
I've tried different combinations with single and double quotes and it can't even get past a simple parse without errors.
What is the best way to go about getting the email address entered as noted above? I know it's something simple but I'm just spinning my wheels here.
the solution is simple:
for example: joe,.E.Blow#someemail.com
SEE FIDDLE

Resources