sql search in multi columns [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to search only in 2016 year.
select from products
WHERE year="2016"
and name like '%"& name &"%'
or size like '%"& Size &"%'

Put parens around your or-ed conditions, and change the double quotes to single quotes for the year (as suggested by jarlh):
select from products
WHERE year='2016'
and (
name like '%"& name &"%'
or size like '%"& Size &"%'
)
Oh and in case this is VB.NET and you got name and Size from a parameter or something, make sure you are escaping them to prevent SQL injection (if this not already done somewhere else).

No need for double quotes, or any quotes in the LIKE.
SELECT *
FROM products
WHERE year = '2016'
AND (name LIKE '%name%' OR size LIKE '%size%'

Related

using mid function in bigquery [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a column that has data like this:
TEXT000612
TEXT721
TEXT8
Expected output:
000612
721
8
The column only has a 4 letter text text and its only at the beginning at the cell. But the numbers can vary in length. Also, I want to make sure the numbers are string.
There is no mid function in BQ.
Also, if you dont like my question or think it needs to be improve please give me a chance to improve it before flagging it.
Few options for BigQuery Standard SQL
#standardSQL
SELECT col,
SUBSTR(col, 5),
REGEXP_REPLACE(col, r'^TEXT', '')
FROM `project.dataset.table`
If you just want the numbers, use substr():
substr(col, 5)
You can cast() this to a number if you want.

SQL select Statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am on SQL Server 2012, I am trying to write a SELECT statement that uses LEFT() and RIGHT() functions to display a ProductName if the 3rd letter from the end of the product name is a particular letter.
Example the 3rd letter is an 'a'
The simplest optio here is to use like:
select ProductName from Products where ProductName like '%a__'
% is a wildcard matching any number of characters, and _ matches a single characters. like '%a__' means the third-to-last letter is an a.
Working example: http://www.sqlfiddle.com/#!6/6b479/8
If you want to use left and right, you should use:
select ProductName from Products
where len(ProductName)>=3 AND left(right(ProductName,3),1)='a'
Working example: http://www.sqlfiddle.com/#!6/6b479/2
left(right(ProductName,3),1) means: take the last three letters, and take take the first letter out of them. We also check for len(ProductName)>=3 to make sure there are at least three letters, otherwise we may return short product names that begin with an a.

How to check if one table is replica of other table in SQL? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How to find if one table is replica of other table in SQL
In SQL Server, you can compare the results of:
SELECT checksum_agg(checksum(*))
FROM Table1;
SELECT checksum_agg(checksum(*))
FROM Table2;
You could create a join with these as sub-queries if you want, too.
You may wish to use binary_checksum() instead of checksum(). The doc says that checksum() will treat strings that are equal as equal according to the collation (i.e., 'hello' and 'HELLO' if the collation is case-insensitive), while binary_checksum() compares the raw binary values of characters.
This works for MySQL:
CHECKSUM TABLE table_1, table_2;

How do I show ☺♦♦ characters in SQL Server? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
You can I display special Unicode characters in my result set on my SQL Server?
e.g. How can i display those characters ☺♦♦?
Well due to the fact that your question is very unspecific here are some solutions.
You need to specify a string as unicode string. This can be achieved by adding an N in front of the string. See example:
SELECT N'☺♦♦' as a, '☺♦♦' as b
Result:
a b
---- ----
☺♦♦ ???
If you want to store those symbols, you need a type as nvarchar or nchar.
Hopefully this helps you.

Fill with zero to complete a defined number in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to complete cards numbers in sql. I have the prefix =11111 and the number of the card which is variable, therefore it could be '25' or '2130' but at the end I must have 14 numbers. So I need to fill spaces with zeros.
I've read about 'LPAD' but I don't understand very well this method.
You could use lpad, but if you're starting with a number you could use a 9-digit format model instead, and concatenate that onto your prefix:
select '11111' || to_char(25, 'FM000000000') from dual;
11111000000025
The FM format modifier stops Oracle adding a space for a potential +/- sign indicator.
SQL Fiddle demo
Use the ZEROFILL attribute.
But your database should only be responsible for saving data and not changing it before saving.
The best way would be to send the zerofilled data to the database server.