I'd like to generate a query like this:
select id, '=FKERES(B2;'D:\nyunyuka\[kotyog.xlsx]Munka1'!$A:$B;2;0)' as
excel_formula from table;
I learned that I have to escape the ' character using E but it seems not working. I assume that I have to escape all the \ as well. But It's getting complicated to me.
I am not sure if I understood the question correct. Hope that I did.
You need to add extra single quotes like this:
select id, '=FKERES(B2;''D:\nyunyuka[kotyog.xlsx]Munka1''!$A:$B;2;0)' as excel_formula
from table;
So, if there is a single quote ' in a string in a select clause then you have to add another one '' to make it work.
Besides doubling the quotes you can use dollar quoting instead.
So use
select id, $Q$=FKERES(B2;'D:\nyunyuka[kotyog.xlsx]Munka1'!$A:$B;2;0)$Q$ as excel_formula
from table;
Instead of
select id, '=FKERES(B2;''D:\nyunyuka[kotyog.xlsx]Munka1''!$A:$B;2;0)' as excel_formula
from table;
Where $Q$ in the above takes the place of the opening and closing quote ('). It is generally not worth it for just a couple instances but for long strings with lots of quotes it can sure comes in handy.
Related
I have a column that's string and I would like to use a WHERE statement for all strings that begin with the "sea_"
Any ideas on how to achieve that will be appreciated.
Thanks in advance
It's not clear what exactly your where clause should do. If necessary, you should please add more information. Anyway, in general, you can do such a select:
SELECT column FROM table WHERE column LIKE 'sea_%'
The % means that zero, one or more characters can follow.
If you need further conditions like an exact number of trailing characters etc., you please must specify your question.
If you like to ignore Upper/Lower case , it can help you
SELECT * FROM table WHERE LOWER(column) LIKE 'sea_%'
Be aware that underscore _ is itself a placeholder for any single character which must be escaped in order to use it literally.
The criteria you should use to ensure an underscore is also present is
where column like 'sea[_]%';
You can also define your own explicit escape character, such as
where column like 'sea#_%' escape '#';
I have a variable in a stored procedure that contains a string of characters like
[Tag]MESSAGE[/Tag]
I need a way to get the MESSAGE part from within the tags.
Any help would be much appreciated
Note: I have tested it on Oracle RDBMS
A more reliable approach is to use REGEXP_REPLACE.
REGEXP_REPLACE(value, pattern)
Example
SELECT REGEXP_REPLACE(
'<Tag>Message</Tag>',
'\s*</?\w+((\s+\w+(\s*=\s*(".*?"|''.*?''|[^''">\s]+))?)+\s*|\s*)/?>\s*') FROM DUAL;
Just replace "<" with "[" if your tags are different
What you need is this:
SELECT SUBSTRING(ColumnName,CHARINDEX('html_tag',ColumnName)+LEN('html_tag'),CHARINDEX('html_close_tag',ColumnName)-LEN('html_close_tag')) FROM TableName
You'll require to change the html_tag and html_close_tag with your own HTML tag that you want to get rid of.
If the column contains only single tag, simple call of substring function should be enough. Otherwise there will always be some point where regular expression does not suffice since you fall into trap (see this legendary StackOverflow answer).
I'm wondering if it's possible to have a fieldname with parenthesis () and be able to call it with a query. For example I have a field name called...
EnoughMoney(0)
Select EnoughMoney(0) from tbl1
When I select it in SSMS i get the following error....
'EnoughMoney' is not a recognized built-in function name.
The way you can escape names (of columns or of anything else) in SQL Server is by enclosing them in square brackets. Your query will work if you write it like
select [EnoughMoney(0)] from tbl1
As correctly stated by others, it's usually a good practice to avoid spaces and special characters in database objects' names, unless you are forced to, obviously.
Use Select [EnoughMoney(0)] from tbl1
I am running below query in Teradata :
sel requesttext from dbc.tables
where tablename='old_employee_table'
Result:
alter table DB_NAME.employee_table,no fallback ;
I want to get below result using SQL:
DB_NAME.employee_table
Requesttext can be:
create set table DB_NAME.employee_table;
DB Name and table can occur anywhere in the result. Since .(dot) is joining them that's why i want to split with .(dot).
Basically I need sql which can result me surrounding values of .(dot)
I want DBName and Tablename in result.
I'm not a Teradata person, but this should work for both strings given so far, as long as teradata's regexp_substr() supports positive look-behind and positive look-ahead assertions (I might have the Teradata syntax wrong, so a little tweaking may be needed):
SELECT REGEXP_SUBSTR(requesttext, '(?<= )(\w+\.\w+)(?=[,$]?)', 1, 1)
FROM dbc.tables
WHERE tablename='old_employee_table'
See the regex101 example. Hopefully it translates to Teradata easily.
The regex looks for and returns the words either side of and including the period, when preceded by a space, and followed by an optional comma or the end of the line.
You could do this with either regexp_substr() or strtok().
As Jamie Zawinski said:
Some people, when confronted with a problem, think "I know, I'll use
regular expressions." Now they have two problems.
So I would go with the strtok() method. Also I'm lazy and regular expressions are hard.
Function strtok() takes three arguments:
The string being split
The delimiter to split the string
The number of the token to grab.
To get at the <database>.<table> from that string that is returned in your query, we can split by a space, grab the third token, then split that by a comma and grab the first token.
That would look like:
SELECT strtok(strtok(requestText,' ',3),',',1)
FROM dbc.tables
WHERE tablename='old_employee_table'
After I run a query and view the output, for example
select * from People
My output is as follows
First Last Email
Ray Smith raysmith#whatever.itis
How would I export this data so that it looks as follows?
"Ray","Smith","raysmith#whatever.itis"
Or is there a way to do this within SQL to modify records to contain quotes?
Because when you export, it's going to include the commas anyway, right?
If the columns you're interested in are 128 characters or less, you could use the QUOTENAME function. Be careful with this as anything over 128 characters will return NULL.
SELECT QUOTENAME(First, '"'), QUOTENAME(Last, '"'), QUOTENAME(Email, '"')
FROM People
select '"'+first+'","'+last+'","'+email+'"'
from people
This is the kind of thing best done in code however, you shouldn't query for presentation.
select concat(“\"”,first,“\"”,“\"”,Last,“\"”,“\"”,Email,“\"”) as allInOne
Modifying the records to contain quotes would be a disaster; you don't use the data only for export. Further, in theory you'd have to deal with names like:
Thomas "The Alley Cat" O'Malley
which presents some problems.
In Standard SQL, you'd use doubled-up single quotes to enclose single quotes (with no special treatment for double quotes):
'"Thomas "The Alley Cat" O''Malley"'
Some DBMS allow you to use double quotes around strings (in Standard SQL, the double quotes indicate a 'delimited identifier'; SQL Server uses square brackets for that), in which case you might write the string as:
"""Thomas ""The Alley Cat"" O'Malley"""
Normally, though, your exporter tools provide CSV output formatting and your SQL statement does not need to worry about it. Embedded quotes make anything else problematic. Indeed, you should usually not make the DBMS deal with the formatting of the data.
This worked best for me
SELECT 'UPDATE [dbo].[DirTree1] SET FLD2UPDATE=',QUOTENAME(FLD2UPDATE,'''')
+' WHERE KEYFLD='+QUOTENAME(KEYFLD,'''')
FROM [dbo].[Table1]
WHERE SUBSTRING(FLD2UPDATE,1,2) = 'MX'
order by 2
If you are using MS SQL Server, try something like:
SELECT '"'||Table.Column||'"'
FROM Table
-- Note that the first 3 characters between "SELECT" and "||" are:
' " '
-- The characters are the same after "||" at the end... that way you get a " on each side of your value.