decipher this postgresql syntax? [duplicate] - sql

This question already has answers here:
How can I comment special \ commands in PostgreSQL's psql commandline tool?
(5 answers)
Closed 8 years ago.
I have a query in an excel file that I inherited from the previous user/creator of the tool. The external connection is to a PostgreSQL database. Here's the line of script that I need to decipher so that I can hopefully adjust the date range for the query:
with
icon_date as (select max(icon.date::date)/* '1/1/2014'::date*/ as icon_date from pmm.icon)
...
pmm is the schema and .icon is the table name
My specific question is what this part means:
/* '1/1/2014'::date*/
I have no clue what surrounding a date::data type with /* */ would do in the first part of the query. Any ideas? I can post more of query if that would help.

That is just a comment and it will be ignored.
There are (at least) two ways to put comments into SQL:
everything after -- until end of line
everything between /* and */ (even spanning lines)
My guess is that this is code left over from testing, where instead of the max you would select some fixed date (because it is faster, or data was missing).

Related

Replacing weird control characters from sql server table [duplicate]

This question already has answers here:
SQL Server - Remove all non-printable ASCII characters
(4 answers)
Closed 4 years ago.
I have a sql server table where control characters appear when column is copied and pasted into notepad. I need to remove/replace these control characters. For example here is a text i copied from my sql server table into notepad
How do i remove "OSC". I have searched the net and here but cant find anything on this. Table was imported from SSIS as ANSI (i also tried data conversion in ssis to convert the column to ascii but still to no avail).
"OSC" is CHAR(157). Try using REPLACE(Values, CHAR(157), ''). If it works then you can update in the table. Hope it helps.

Why some rows can be inserted into and some cannot? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
When I use the Cashier table or some other tables that have small amounts of records the process proceeds and table is inserted into the external database. But when I change the cashier into the transaction database (400k+ records), Visual Studio reports an error near "Transaction" Help would be appreciated thanks.
Cashier Database (working)
Dim query As String = "select * into MyDatabase2.dbo.Cashier from bos_primary_db.dbo.Cashier"
Transaction Database (not working)
Dim query As String = "select * into MyDatabase2.dbo.Transaction from bos_primary_db.dbo.Transaction"
This is the error message:
Incorrect syntax near the keyword 'Transaction'
this is probably because Transaction is a reserved word in SQL.
Depending on your RDBMS (that you didn't specify), there are ways to "escape" it:
for Sql Server, you should wrap reserved words in square brackets:
select * into MyDatabase2.dbo.[Transaction] from bos_primary_db.dbo.[Transaction]
For MySql you should use an apostrophe:
select * into MyDatabase2.dbo.`Transaction` from bos_primary_db.dbo.`Transaction`
For Oracle you should use double quotes:
select * into MyDatabase2.dbo."Transaction" from bos_primary_db.dbo."Transaction"
Note: You should always try to avoid using reserved words. This link describes my favorite way of do it.

How does separated clause and args protect against SQL injection? [duplicate]

This question already has answers here:
How can prepared statements protect from SQL injection attacks?
(10 answers)
Closed 8 years ago.
I heard that separated SQL clause and args can protect against SQL injection. For example,
clause = SELECT * WHERE ID = ? AND NAME = ?
with ID = 23, and NAME = "Tom".
Can someone explain to me how it works?
Basically, you're making the distinction between data and the actual code (query part) very clear. You're telling the SQL server: this is clearly data and this is clearly code.
This way, you're basically skipping the part where the server has to pull apart the code and data from your query so there's no chance the server can misinterpret bits of data as part of your query.
Edit: as per the link in the comments, this answer pretty much answers your question much better than I've explained here.

SQL Server Replace Command with WIldcard [duplicate]

This question already has answers here:
Perform regex (replace) in an SQL query
(5 answers)
Closed 5 years ago.
I am in need of some help. Thanks to fellow user davids, I was able to get certain things working within SQL Server and am in need of some more help. Here is what I am trying to do:
UPDATE Table1 SET keyfield=(REPLACE(REPLACE(Column1,'http://*/folder/',''),'.avi',''))
UPDATE Table2 SET keyfield=(REPLACE(REPLACE(Column2,'http://server2/folder/',''),'.mpg',''))
Can anyone help me out or point me in the right direction to get the wildcard to work? This particular column, the * is an IP Address and it will change all the time. Granted I will know the IP's ahead of time and can probably do it one by one, but I would prefer to have it automatically replace. Thanks in advance!
You should look into regular expressions.
My google karma found this article in MSDN Magazine.
Edit:
See also:
Perform regex (replace) in an SQL query

What does a colon (':') mean in SQL syntax? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the colon sign “:” do in a SQL query?
Simple SQL question:
What does : stand for?
For example:
SELECT * FROM myTable
WHERE Employee_column = :P_EmplId;
The : isn't exactly easy to google when you don't know what this is called. Even searching here didn't help. I'm using Oracle 11g if that makes any difference.
It is a bind variable:
A placeholder in a SQL statement that must be replaced with a valid
value or value address for the statement to execute successfully. By
using bind variables, you can write a SQL statement that accepts
inputs or parameters at run time. The following example shows a query
that uses v_empid as a bind variable:
Most likely you took the query from a template. It is meant to be processed with php's MDB2 sql framework. The ":" (colon) signals a placeholder in the statement, meant to be replaced when the query is executed.