Is there anyway to avoid formatting the comments?
To reproduce, paste below example SQL code to BQ:
--important comment on table info
select * from mytable1;
/* some more
multiline
important comment
on table info:*/
select * from mytable2;
It will look like this:
Now, click on "MORE", then "Format query"
Some words in comments get capitalised, like "table", "on" and become part of SQL code:
Related
I have two columns, id (integer) and code (characters varying 50).
This is how my table looks like:
id code
9135 2NLX
when I do a select statement like this
select * from table where code = '2NLX'
it shows nothing.
When I do it like this however, the row shows up:
select * from table where code like '%2NLX%'
Why is this happening? It doesn't look like there is a leading space or spaces in the code field. When I paste the data in excel there are no random spaces. Can someone please help me?
Looks like we've loaded some snowflake tables via ELT with "TO, FROM" as column names and they are both classic functions in any sql tool
Whenever I run a query for specifically those columns, there's always an error - how do I fix it apart from changing column names? Don't want to change column names as ELT process always happens from mongoDB via log based replication (stitch data)
select * - works perfectly , all other columns work too. Just "to" , "from" is the issue - should that never be used a columns?
select to, from from table limit 10 ; // tested [to, "to", 'to'] - none work
Error: SQL compilation error: error line 1 at position 7 invalid identifier '"to"'
Any ideas how to fix this apart from source column change or snowflake column changes?
Snowflake uses the standard double quotes to escape identifiers. However, when identifiers are escaped, the case of the letters matters, So, these are not the same:
select "to"
select "To"
select "TO"
You need to choose the one that is correct for your column names.
In addition spaces matter, so these are not the same:
select "to "
select " to"
select "to"
That is, what looks like to might be something else. You need to know what that is to escape the name properly.
If you can't figure them out, there is a trick to create a view to give the table reasonable names. Something like this:
create view v_t (to_date, from_date, . . .) as
select *
from t;
You need to be sure to include all the column names in the table in the column name list, in the same order as they are in the table. Then you can use the view with reasonable names.
I have the following query:
SELECT * FROM MailingList
There are about 20+ columns in the MailingList table, one which is called Address. This column has some fields which contain commas, which I need to take out. So I updated my query to:
SELECT REPLACE(Address, ',', '') AS Address, * FROM MailingList
But now I have two Address columns. Is there a way to only display one Address column while still using the wildcard (*) for all the other columns?
There is not a way to do this, though listing the columns you want explicitly is a good idea anyway.
You can trick as following query:
Get the data into a temp table
Drop the cloumns that are not needed
Get results and drop temp table
SELECT *, REPLACE(Address, ',', '') AS Address2
INTO #TempTable
FROM MailingList
ALTER TABLE #TempTable
DROP COLUMN [Address]
SELECT * FROM #TempTable
DROP TABLE #TempTable
I agree with Shadow - avoid using the * wild card if you can...
I know listing out ALL of the columns in select statement for big tables is a pain so here is a quick short cut you may not be aware of: In SQL Server Management Studio, browse through the object explorer and find the table you want to select from (MailingList). Right-click it to view the context menu and choose "Script Table as" then "SELECT TO" then "New Query Editor Window". This will create a new select statement with each column spelled out. In the future, use this method to create select statements, queries, procedures, etc. rather then the * wildcard. Performance is better and it just looks nicer :-)
Then you can solve your alias issue with the replace function.
I want to add multi-line table/column comment.
Normally this is used;
COMMENT ON TABLE USERS IS 'User table has the user data'
What I need is a way to insert the new-line inside the single quotation marks like;
COMMENT ON TABLE USERS IS 'User table has the user data <smthg_here_for_new_line> 1- Name column has name <smthg_here_for_new_line> 2- Number Column has the id'
So that table comments will be seen like;
User table has the user data
1- Name column has name
2- Number Column has the id
Anybody knows how add multi-line table/column comments?
You can simply put line feeds inside the single-quotes of your comment declaration, for example:
COMMENT ON COLUMN MYTABLE.MYCOLUMN
IS
'Line 1
Line 2.
Line 3';
Note, however, that in SQL Developer (and perhaps other tools) this will not always display as expected. With the following query ...
SELECT *
FROM USER_COL_COMMENTS
WHERE
TABLE_NAME = 'MYTABLE'
AND COMMENTS IS NOT NULL;
... you'll get exactly what you're looking for in Script Output (i.e., highlight the query, right-click, select "Run Script"):
TABLE_NAME COLUMN_NAME COMMENTS
---------- ----------- --------------
MYTABLE MYCOLUMN Line 1
Line 2
Line 3
MYTABLE OTHERCOLUMN Other comments
But in a Query Result (i.e., highlight the query, right-click, select "Run Statement"), or when opening the table and looking at the Columns tab, the full comment will be run together on a single line.
Note: The tables in which these comments can be queried are:
Comments on tables: USER_TAB_COMMENTS
Comments on columns: USER_COL_COMMENTS
In SQLPlus you can use concat with chr(10) (or chr(13) || chr(10) in Microsoft Windows environment):
'User table has the user data' || chr(10) || '1- Name column has name...'
Also, it should be possible to just interpret newlines by setting SQLBLANKLINES to ON:
SET SQLBLANKLINES ON
COMMENT ON TABLE USERS IS 'User table has the user data
1- Name column has name
2- Number Column has the id'
I'm having a little trouble with the command line of a oledb query on an access database.
I've tried a number of different syntaxes, both right in access and in my code. The connection to the database is fine, the error comes when I try to fill the da, or when I run it as a query in access.
SELECT * FROM MyTable WHERE MyColumn = 'This Text String';
When I run this in acces, I get a Data Type mismatch. Double quotes yields the same. I thought it was because of the spaces do I tried wrapping the Text String in square brackets thusly:
SELECT * FROM MyTable WHERE MyColumn = [This Text String];
In Access this prompts "Enter Parameter Value" listing the parameter as "This Text String".
I've also tried using the Like operator:
SELECT * FROM MyTable WHERE MyColumn LIKE 'This Text String';
Whether with single or double quotes, this produces no errors, but also produces no results. I've Tried CONTAINS which gives me a syntax error.
At this point I don't know what else to try. what I'm trying to accomplish is:
Return all the rows in 'MyTable' where the text in 'MyColumn' equals 'This Text String'
Can someone help me? I feel I'm overlooking something, possibly obvious.
Edit: Ahh... I'm not sure if this is the problem, but perhaps it will help.
I just realized that the column (MyColumn) that I'm trying to filter based upon, is not just plain text in access, it's actually a lookup of a column in another table.
Pehaps that's the issue, because the data type is not actually text, it's number (since it's a list). Ok... how do I overcome that?
You need to join the lookup table
SELECT * FROM MyTable INNER JOIN Lookup l on MyColumn = l.key and l.value = 'This Text String';