Overcoming the reserved word "IN" in sql server [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Just for reference I am using SQL Azure.
I noticed when I am trying to select data from a table based on a license plate and the state of that plate I get no results back if the state is "IN". I realize the word "IN" is reserved in SQL server; however, I am containing that within quotes in my query. I currently am in testing phase and have only one record in the table which has a lisence plate 287YGB and state IN.
If I write my query as follows I get nothing back.
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles WHERE tblVehicles.Lisence = '287YGB' AND tblVehicles.PlateState = 'IN'
If I write my query this way I get back my result. But this is not good enough.
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles WHERE tblVehicles.Lisence = '287YGB'
And finally, if I write my query this way I get the only row in the table.
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles
From these tests I can see that the last where parameter is causing the problem. I am assuming it is due to the fact that the word "IN" is reserved. Is there a way around this?

Reserved words usually only cause problems if you're using them as field names, and in that case you need to wrap them with brackets ("[]") to eliminate the problem. I will amost guarantee you that your PlateState has some garbage in it, so you need to either trim it first (LTRIM(RTRIM(PlateState)) = 'IN') or use Like '%IN%' instead, and this will return the results you expect.

try this
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles WHERE tblVehicles.Lisence = '287YGB' AND LTRIM(RTRIM(tblVehicles.PlateState)) = 'IN'

Related

Wildcard operator ^ Not working as expected [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 28 days ago.
Improve this question
When I use:
SELECT *
FROM Customers1
WHERE email LIKE '%[^#]%'
There are no customers on the output at all, when there should be two on the output. Any ideas what might be causing this? I need to find customers email addresses which don't contain the '#'symbol.
I've tried running code:
SELECT *
FROM Customers1
WHERE email LIKE '%[#]%';
This shows 13 on the output.
I've also tried the '%[!#]%'; but this makes no difference.
In my understanding, you are trying to get all the email in a table that is not a valid mail address (by checking that email must have # symbol in it).
The way you are trying to achieve this will not work as this will fetch all data in the column except the null and blank (empty string) data.
[^] wildcard will check LIKE and PATINDEX in some special cases like.
A column has only single characters.
You are fetching a string where a specific position has not that value, etc.
For this scenario I will rather suggest, use a simple TSQL like.
SELECT *
FROM Customers1
WHERE email NOT LIKE '%#%';

Comparison with '{0}' in SQL SELECT [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 months ago.
Improve this question
I have to take over old colleague's code and trying to understand this one SQL statement like below:
SELECT * FROM my_table WHERE date_key = '{0}'
Column date_keycontains int values such as 20220712, 20220120, etc.
The first guess is that SELECT statement filters for rows with 0 value in column date_key. However, when running that line of code, I receive this error :
SQL Error [100038] [22018]: Numeric value '{0}' is not recognized
What exactly does that line of code do?
That looks like a placeholder, replaced with an actual value in code when calling the query.
See similar What is {0},{1},{2},{3} in the SQL query

Why does SQL want you to select the column and THEN refer to the table? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
So this isn't a technical question, but rather questioning why a language is designed the way it is.
I've been learning SQL and one thing that's been bothering me greatly is how SQL asks you to name the column you want and THEN name the table you want to get it from. To me, it would make more sense that you refer to the parent body (which is the table) and THEN the column it has. But SQL seems to forces users to do it the other way around. Why?
I'm just curious as to why the language is designed this way.
SELECT column
FROM table
why not
FROM table
SELECT column
SQL tries to mimic English language to some extent, so that it feels natural to formulate the query.
In spoken English you would say something like "I want the names of the employees". You would not say "I want of the employees their names" or something like that.
But you are right, it might have been a good idea to have the query represent the order of execution. And "From the employee table I want the names" would not be so far off the mark :-)
SQL is a descriptive language, not a procedural language. It describes the result set being produced. And, you can think of that result set as a report, with column headers.
As such, the basic querying construct returns those column headers. The rest of the query describes how they are produced.
You may find this post useful. Starting with FROM is the most logical way to think about a query (Why would anyone write SELECT before knowing what to SELECT from?). However, SQL guidelines were designed as if your query were a command. Thus, you are commanding the system to SELECT the data for you, and the FROM further specifies that command.
Of course, the actual execution is distinct from the lexical and logical orders above.

What are sql function possible positions in sql statement? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am looking for all the possible positions in a sql statement that a sql function can be called. It is my first time to use sql functions and in my code I need to detect whether users' input contains sql function, if so, the input is not valid.
The position in sql statement means SELECT clause or other clause like FROM, WHERE......
I am using SQL Server
So is there any suggestions for detect sql functions in a sql statement or all the possible positions for the function in a statement?
SQL functions are allowed in any place in a SELECT where a column is allowed, i.e. anywhere in the SELECT list, in WHERE, JOIN, GROUP BY, HAVING, ORDER BY.
You'll need a SQL parser to know for sure if it's a function...
If I understood your question, you are asking how and where to check about SQL code entered by the user.
Where:
I think you might need to check it in the user interface, check the textbox or files or whatever the input stream of the user is. This shall not be done in the sql server ASAIK
How:
usually you can use parameters in your SQL statements so that any value passed by the user is passed by a parameter and if it contains a SQL code, it will not be executed.

SQL Server 2008 - DECLARE function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
What does the "s" do at the end of line 8 of this query:
http://www.sqlfiddle.com/#!3/f8816/20/0
I can't find it anywhere and the statement won't work without it.
Thanks!
The s is an alias for the result set which allows it to be referenced within the query.
The readability of a SELECT statement can be improved by giving a table an alias, also known as a correlation name or range variable. A table alias can be assigned either with or without the AS keyword:
table_name AS table alias
table_name table_alias
Using table aliases
The s is a table alias. It gives a name to a table or subquery used in the from clause.
SQL Server requires that all subqueries use aliases. Not all databases do.
I strongly encourage you to use them. They often make queries much more readable.
The data set getting created in the from is given the name 's' (similar to putting "AS s") so you can reference it otherwise in the code. Any data sets being created in a from requires a name be given to it, hence why it only works with the 's'.