SQL Server IN statement with wildcard [duplicate] - sql-server-2008

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a combination of “LIKE” and “IN” in SQL?
The first where clause below runs fine for me but it does not pick up contacts that might have 45211-1234 or 45213-4321
SELECT * FROM contacts
WHERE zipcode IN ('45211','45213')
I thought I could change the where clause to the below to fix, but it fails.
SELECT * FROM contacts
WHERE zipcode IN ('45211%','45213%')
How might I change this so it brings back anything that has proper zip + dash + any zip4? For example, 45211-1234 or 45213-4321.
Note I have whole bunch of zipcodes to enter, not just these two.

How about this:
SELECT * FROM contacts
WHERE Left(zipcode,5) IN ('45211','45213')

Related

Is there a better way to write REGEXP in sql to select unique transaction number R-0001 and R-AD-0001 [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 1 year ago.
Improve this question
Am having a lot of transaction number store in my db, example I-0001, IN-0001, N-0001, S-0001, SN-0001, R-0001 , R-AD-0001, and R-AA-00001.
Every time a new row insert, I will select the maximum number of transaction numbers and + 1 to keep track of every transaction number.
Here is my initial SQL SELECT MAX(`transaction_invoice`) FROM sw_invoice WHERE `transaction_invoice` LIKE '%N-%', but I found out using LIKE to select for example maximum number of N-, LIKE SQL will also select N-0001 and SN-0001 together
Example of LIKE SQL result
To solve this I found out REGEXP might solve my issue here is example SQL
SELECT * FROM `sw_invoice` WHERE `transaction_invoice` REGEXP '^N-', this SQL works well when select maximum number of N-, but when its come to select R-0001, it will also select R-AD-0001, and R-AA-00001 which start with R-
Example of REGEXP SQL result
Is there a way any SQL can only select the maximum transaction number I require? for example if I select transaction number start with R-, its only return the maximum number of R- not R-AD-
Just remove the leading wildcard from the like:
SELECT MAX(`transaction_invoice`)
FROM sw_invoice
WHERE `transaction_invoice` LIKE 'N-%';
You don't need regular expressions for this.
If you need to handle hyphens in the first part, then you need regular expressions:
where transaction_invoice regexp '^N-[0-9]+$'

Issues when getting ID from DB [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 2 years ago.
I hope you all have a good day.
Actually I'm deploying a query to get a complete array of data. But the fact is that I need an Id, first of all, I guess, I must get the last Id first, then I can apply a mathematic operation to get its value + 1. The fact is that I've been trying different sentences and queries with no result.
This is my code:
function obtener_Id(){
global $mysqli;
$resultado_oId = $mysqli ->query("SELECT TOP 1 'id' FROM 'pacient' ORDER BY RowID DESC ")
$id_sacada = mysqli_fetch_assoc($resultado_oId);
$id_enLaMano = $id_sacada['id'];
return $id_enLaMano;
//$id_dinamica = $id_enLaMano + 1;
//return $id_dinamica;
}
As you can see guys, I've commented on the last two lines, cause I´m looking to get at least a value (The result from a query) But Idk if that is correct. Looking on the Internet I've seen relative posts which are solved just to apply the query that we can view under global declarations...
I've tried that on phpMyAdmin with no results and a bunch of errors...
You guys know the correct way to get the max value in the Id column? Or even if I'm doing badly correct me.
A lot of hugs and Luck!
Mizar ^^
I would suggest putting the serial number in a table. Read the serial no before insert, (lock the table, if required) insert the data, then increase the serial no by 1 and update the serial no table with increased value.

Getting empty set from SQL statement [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 4 years ago.
I get am empty set when I enter this SQL statement, could someone please tell me why?
select ayr,mods.mid,mtitle,credits
from mods,comp
where mods,mid = comp.mid and ayr = 2001/02;
It displays the right thing when omitting 'ayr = 2001/02'.
You need quotes around the date
select ayr,mods.mid,mtitle,credits
from mods,comp
where mods,mid = comp.mid and ayr = '2001/02';
Are you sure that after the where clause it should be mods,mid instead of mods.mid, table name is not entered in the correct way after SELECT statement
As per my suggestion you should enter mods.mid
Try this Syntax:-
select ayr,mods,mtitle,credits from mods,comp where mods.mid = comp.mid and ayr = 2001/02;
Post the output

SET mysql variable with list of mails [duplicate]

This question already has answers here:
How to pass a variable to a IN clause?
(4 answers)
Closed 5 years ago.
I need to define a list of variables to use in multiple MySQL queries.
The variable will be a list of mails. I have tried to define it in different ways but it always gives me error in the construction.
SET #listamails='mail1#gmail.com,mail2#gmail.com';
Select * from user WHERE mail IN (#listamails);
Any ideas?
Thank you
You cannot pass in a list to IN using a single variable. The simplest solution in MySQL is find_in_set():
Select u.*
from user u
where find_in_set(mail, #listamails) > 0;
However, this cannot take advantage of an index. For that, you might want to use dynamic SQL.
Try this
SET #listamails='mail1#gmail.com','mail2#gmail.com';
Select * from user WHERE mail IN (#listamails);

"30/360 = 0" Is This A Bug Or What? [duplicate]

This question already has answers here:
SQL Server, division returns zero
(6 answers)
Why does SQL Server round off results of dividing two integers?
(6 answers)
Database calculations are wrong
(1 answer)
Closed 9 years ago.
Ok I have A SQL Procedure I calculate some Payroll Stuff. The Problem here one of my equation '30/360 = 0' return 'zero' and try it on Sql Server Like this :
Select 30/360 --This Return Zero And Should Return '0.08333333'
This is not my real function, just example, Is this normal behavior?
Dividing two integers returns an integer. Use the following instead:
select 30/360.0