Most anwered "sql" questions
Structured Query Language (SQL) is a language to query databases. Questions should include examples of table code and structure. This tag refers to the default language, not to be used for questions about the implementation of specific DBMS (such as Mysql, Postgresql, Oracle, MS SQL Server). In this case use the specific DBMS tag. Answers to questions marked with ANSI SQL should use SQL as much as possible.
Learn more…6,771 questions
Sort by count of
-
199
votes9
answers12451
viewsHow to prevent SQL code injection into my PHP code?
I developed a PHP page for internal use of the company I work with and only very few people use it. Through this page it is possible to make some queries, insertions, changes and removals of data…
-
33
votes8
answers48170
viewsHow to calculate a person’s age in SQL Server?
Suppose the table Pessoa and the countryside DataNascimento. In an SQL query, what is the best way to calculate a person’s age in integer format in the T-SQL language of SQL Server?…
-
60
votes8
answers16426
viewsWhy is using "SELECT * FROM table" bad?
It is often said to take all columns of a table through the command SELECT * FROM tabela is a bad practice. Any recommendation without explanation is not helpful. So... If it is bad practice, there…
-
13
votes7
answers180098
viewsFormat decimal places directly in the SQL command in Firebird
I have a table ESTOQUE containing a field called QTDE, this field has 3 decimal places. What would be the command to return directly from SQL formatted with 3 houses? Because the integer values are…
-
6
votes7
answers2949
viewsHow to use multiple SQL commands (in the case delete on ORACLE) in one line only on C#?
I made a command because of Foreign Keys, only to plug hole, but it is not working for an invalid character error, I believe it is the ";". I know I’m doing this the wrong way, so I accept…
-
66
votes7
answers11287
viewsIs it wrong to write byte of images into the database?
When should I make this recording directly at the bank? What situations? I know I can record in the bank the image path.
-
28
votes7
answers33990
viewsHow do SELECT all fields except a few?
It is known (and has been asked) which should be avoided SELECT * in some cases in bank queries. But let’s imagine that I have a table with 50 columns, and I want to select 40 of them. Just the fact…
-
1
votes7
answers412
viewsMount an sql query filtering data
I am trying to assemble a report, where I need to list deleted documents, I have the following sql statement: SELECT id,nf,status,data FROM documentos For example I have the following results: When…
sqlasked 7 years, 10 months ago Smoke Rohden 793 -
14
votes6
answers11304
viewsHow to get distance given the coordinates using SQL?
I’m doing a function in php, in which, given a GPS coordinate, it will search in the database the recorded locations, which are not necessarily fixed, within a certain distance. My question is, how…
-
1
votes6
answers708
viewsHow to count the number of clients that returned from one year to the next through SQL?
I have a table Serviços(Nro_Serv, Data, Cliente, Valor) and would like to get customers who did service with me in a year X (ex: 2011) and who also did in a year Y (ex: 2012). I tried to use…
-
65
votes6
answers66144
viewsType of the CPF or CNPJ field in the VARCHAR or INT database?
What type of CPF or CNPJ field in the database VARCHAR or INT? I’ve seen some posts suggesting using INT to optimize performance in the event of JOINand filters. If you have 0 the left is only…
-
9
votes6
answers49720
viewsConvert DATA dd/mm/yyyy hh:mm:ss to yyyy/mm/dd
I have a date in the following format 14/12/2015 00:00:00 and need to convert to 2015-12-14 as I can do it on SQL?
-
5
votes6
answers32941
viewsConvert varchar to date in SQL
You can convert dates into sweep for date. I’m having difficulties in performing searches between dates, due to this discrepancy in the database where I perform the query. When I enter my line of…
-
17
votes5
answers2401
viewsHow do I merge two results of a query?
I am developing an application in C# and I would like to know how to put together two results of two darlings SQL in one. I have the following code: public List<MalaDireta> ObterMalaDireta() {…
-
33
votes5
answers74978
viewsHow to get the table name and attributes of a Mysql database?
How to get the names of all tables in the database Mysql? How to get the attributes (name, type, etc...) of a given table in the Mysql database?
-
10
votes5
answers2566
viewsIs it recommended to use Linq instead of SQL?
To avoid string abuse and avoid code injection problems can be a good practice using the Linq. I can also imagine, for example, that it’s slower. Who ever used the Linq to communicate with the…
-
15
votes5
answers29661
viewsHow do I know if a column exists in an SQL Server table?
I am trying to add a new column to an SQL Server table, and I want to know if it already exists or not. I have tried something like this: IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE…
-
2
votes5
answers508
viewsQuery error with WHERE AND COUNT on Oracle. "SQL command not properly ended"
I have the following query: INSERT INTO TB_CLASSIFICACAO_UG (COD_CLASSIFICACAO_UG, TXT_DESCRICAO, IND_ATIVO) VALUES (SEQ_COD_TB_CLASSIFICACAO_UG.nextval, 'TESTANDO', 1) WHERE (SELECT…
-
4
votes5
answers620
viewsHow to select records that have a relationship with all values in a list?
I have the following sql: SELECT DISTINCT cp_pessoa.id, cp_pessoa.nome FROM cp_pessoa LEFT JOIN cp_habilidade_freelancer ON (cp_habilidade_freelancer.id_freelancer = cp_pessoa.id) LEFT JOIN…
-
12
votes5
answers6123
viewsPhysical Exclusion vs Logical Exclusion
What is the advantage of making logical exclusion (Ex: Set a flag indicating that the record is active or not), instead of performing physical exclusion? Is it common practice? It’s safe? Derived…
-
32
votes5
answers41630
viewsMysql Limit Equivalent in SQL Server
Someone knows how to relate the differences between Mysql and SQL Server, including using LIMIT n,n? Or if you have a link that lists the differences would be nice. Example SELECT * FROM tabela…
-
5
votes5
answers36671
viewsHow to get the total character size of an SQL column?
I want to take the total size of characters that a column supports, example INT supports 10 characters. I tried to use DATALENGTH that does not return the number of characters, but the amount of…
-
4
votes5
answers6102
viewsHow to create a view in mysql by taking data from 3 tables and repeating the different columns in the result?
I have the tables: +----------Tabela-1--------+ id | nome | cnpj | qtd1 1 | carlos | 0563233 | 4 +--------------------------+ +----------Tabela-2--------+ id | nome | cnpj | qtd2 1 | carlos |…
-
11
votes5
answers64148
viewsDifference between two dates in days, hours, minutes and seconds
Problem: How to subtract between two dates and return the value in Days, Hours, Minutes and Seconds in an SQL query with SQL Server 2014? Example: Dados: DataRecebimento DataEnvio 2013-11-29…
-
5
votes5
answers15132
viewsChange NULL value in SQL SERVER
How to change the NULL value in sql server to for example 'NONE', how to fix it ? My result is coming like this: Florida United States NULL 16 Nevada United States NULL 17 Nevada United States Cell…
-
8
votes5
answers42558
viewsHow to delete data from a table with dependencies in other tables
I need to delete data from a table that has dependencies on other tables. For example, so that I can delete a data from the person table first I have to delete a dependency that exists in the…
-
13
votes5
answers53548
viewsMysql decimal value
I am trying to insert decimal value in mysql database, wanted to put these values for example: 1.500,00 Thousand, hundred and ten What is the type of column I must declare?…
-
1
votes5
answers923
viewsHelp with PHP and Mssql special characters
I’m making a form, and when I record the information with special characters, record it well... only when I rescue the information it comes encoded... already used , utf8_encode/Decode and nothing I…
-
6
votes5
answers10525
viewsShow every day between two dates
I need to display all dates that exist between two set dates. I do not need the information of the difference between these dates, nor the data that are in the interval between them, but show the…
-
0
votes5
answers3030
viewsCodeigniter -> Message: Trying to get Property of non-object
I’m trying to fix this bug: Severity: Notice Message: Trying to get Property of non-object Filename: contracts/view.php Line Number: 37 CODE: contratos_model.php public function getById($id){…
-
1
votes5
answers580
viewsDoubt in SQL query "between" or "in"
I am generating a report between date, as the figure below: However, when I enter the starting date: Initial year: 2014 Initial month: 6 and Final year: 2015 Final month: 5 does not bring the data,…
-
10
votes5
answers26385
viewsWhat is the best practice to know if an Row exists in a SELECT in Mysql?
For example, when we check if a user is already registered in the table, we do not need any data return by query, just check if the number of Rows is greater than 0 (num_rows > 0). Us scripts and…
-
8
votes5
answers6773
viewsSeparate ddd from phone with SQL statement
I have a table with fields DDD and Phone. Some were registered correctly, others the ddd is next to the phone and need to separate. TABLE +--------------------+ | DDD | Telefone |…
-
1
votes5
answers100
viewsCopy a word from a column to the end
I have several records in sql for example almedia,jose costa santos and I want to change the value to: jose costa santos almeida In other words, I want to take the last name at the beginning (before…
-
4
votes5
answers19374
viewsHow to check if a field of type text or ntext is null or empty?
How can I check whether a field of the type ntext is null or empty in where? I got the following select SELECT Subject, Notes from DocumentNotes where Notes is null or Notes = ' ' but when comparing…
-
2
votes5
answers1793
views -
7
votes5
answers2439
viewsSubtract sums of fields in two separate tables
I need to add the value of a column in SQL, so it looks like this: SELECT SUM(qtdsaco) FROM armazem This code works. Now I need to add the value of the other table, here it is: SELECT SUM(qtdsaco)…
-
7
votes5
answers4473
viewsIn the ORDER BY of a SELECT change a comma for a period
I have a table in MYSQL (can be seen here http://sqlfiddle.com/#! 9/187ce/1 ), where the price is with commas and not points: 2.00 2,33 11.00 When I’m going to give an order by: ORDER BY preco ASC…
-
4
votes5
answers29127
viewsuse if in sql server
I have 3 tables in my database, CUSTOMERS, DISTRIBUTORS and EXPORT and I have a production order table where I take the customer type (Distributor, customer or export) and the ID of each of them.…
-
2
votes5
answers1818
viewsGroup records per day
I have the following table ID | Início | Fim | Horas 333 | 01/01/2017 | 03/01/2017 | 5 333 | 02/01/2017 | 05/01/2017 | 1 333 | 05/01/2017 | 07/01/2017 | 3 333 | 01/01/2017 | 07/01/2017 | 6 I need a…
-
3
votes5
answers1923
viewsIs it possible to update the first 100 lines in sql(server)?
UPDATE tbl_venda SET produto=1 WHERE produto=3 The problem is that there are more than 10,000 sales lines and the pc hangs. I wish it was possible to do 100 to 100, 500 to 500, so it doesn’t get…
-
2
votes5
answers14274
viewspsql command at prompt. (postgresql database)
Good morning, everyone, I’m trying to make a script with extension. bat to make automatic update in the bank,. I am doing as follows to access my bank through windows pronpt. psql -U bank user -w…
-
31
votes5
answers12535
viewsWhy is Varchar(255) widely used?
Why in various databases, say to set the size as varchar(255)? For example, in a field email why put varchar(255)? It would not be better (in question of storage and processing speed) to put…
-
6
votes5
answers16830
viewsHow do I list all tables with their respective databases?
I would like to list all the tables in my database that ends with a specific name and also all my databases that match these tables. I tried to do it but nothing comes back. Query SELECT da.name AS…
-
30
votes5
answers7016
viewsHow to delete all duplicates except one?
I have a table that has duplicate records. They shouldn’t be duplicated. Some records duplicated two, five and even thirty times by accident. I need to remove these duplicate records, but I need to…
-
2
votes5
answers2617
viewsSelect last dates of each SQL month
I would like to perform an SQL to get from a list of dates the last day of each month. Example: From a list 01/01/2016 until 31/12/2016 return the last days of each month: 31/01/2016, 28/02/2016...…
-
10
votes5
answers972
viewsHow to get the name of the day of the week on the first day of the month?
How do I get the name (Monday, Tuesday, Wednesday...) of the first day of the month of December from the current date?
-
4
votes5
answers3132
viewsWhat is the difference between comparison operators on Oracle?
In Oracle there are several operators to make "different" comparisons, such as: <> ¬= != ^= Example: Select * from tabel where nomeTabela <> 's'; Select * from tabel where nomeTabela !=…
-
4
votes5
answers1602
viewsChange return /Date(1386295200000)/ to date format
I have a return from json that comes so from SqlServer: /Date(1386295200000)/ How to convert to date format dd/mm/yyyy?
-
50
votes5
answers60507
viewsDISTINCT and GROUP BY, what is the difference between the two statements?
DISTINCT The SELECT DISTINCT statement is used to return only values distinct (different). Within a table, a column usually contains many values duplicated; and sometimes you just want to list the…