query for serial number - mysql

Just want to get an idea & advise.. how to get the exact result for my situation when query..see below table..
I try to use this query and its look fine..but the problem is when I give the input ABC1001Z (different only last character Z).. the query still return Honda as result.. it's supposed not return any result/no result found.. any solution for my case?
SELECT Name
FROM CarNo
WHERE ('ABC1001Z' BETWEEN Start AND End)
AND (len('ABC1001Z') = len (Start));
Your kind support is much appreciated..

Update you code to
SELECT Name FROM CarNo
WHERE (SUBSTRING('ABC1001Z',0,7) BETWEEN SUBSTRING(Start,0,7) AND SUBSTRING(End,0,7) )
AND (len('ABC1001Z') = len (Start));

Maybe that's what you are looking for:
SELECT Name FROM CarNo
WHERE (Start = 'ABC1001Z' OR End = 'ABC1001Z')
AND (len('ABC1001Z') = len(Start));

You seem to want the "number" in the middle to be treated as a number. This suggests something like this:
where left('ABC1001Z', 3) between left(start, 3) and left(end, 3) and
substr('ABC1001Z', 4, 4) + 0 between substr(start, 4, 4) + 0 and substr(end, 4, 4) + 0
I'm not sure how the last character relates to a between query of this form, so I'm not addressing that.

Related

Find a string in a MySQL field with concurrences

Is there any function in MySQL where I especifies the concurrences numbers for the search?
Example:
lcString = "My name-is-Harry-Potter"
In Visual FoxPro you can use this:
?AT('a',lcString,1) && where 1 means "get me the first concurrence"
OutPut = 5
Or
?AT('-',lcString,3) && where 3 means "get me the third concurrence"
OutPut = 17
I was looking for a similar function in mysql but I can't find it.
Thank you all...!!!
You can use SUBSTRING_INDEX and LENGTH MySQL functions to achieve that. This is what MySQL's documentation says about SUBSTRING_INDEX:
Returns the substring from string str before count occurrences of the
delimiter
So, you can wrap that inside LENGTH to get the occurrence, e.g.:
SELECT LENGTH(SUBSTRING_INDEX('My name-is-Harry-Potter', 'a', 1)) + 1
SELECT LENGTH(SUBSTRING_INDEX('My name-is-Harry-Potter', '-', 3)) + 1

Where condition is not working

I have a SQL query. But that contains a where condition, and the OR in that where is not working.
Query
SELECT `st_student`.`st_id`, `ab_date`, `as_date` `st_status`
FROM (`st_student`)
WHERE `st_status` = 1
OR `st_status` = 2
AND `ab_date` BETWEEN '08/01/2015' AND '08/31/2015'
OR `as_date` BETWEEN '08/01/2015' AND '08/31/2015'
AND `aca_no` = 2
GROUP BY `st_student`.`st_id`
This condition is not working:
OR `as_date` BETWEEN '08/01/2015' AND '08/31/2015'
Is there any mistake in that?
One possible error concerns the use of the AND and OR operators, since AND has priority over OR, and your query can be interpreted in the wrong way. So you should use the parentheses, and write something like this:
SELECT `st_student`.`st_id`, `ab_date`, `as_date` `st_status`
FROM `st_student`
WHERE (`st_status` = 1 OR `st_status` = 2)
AND (`ab_date` BETWEEN '08/01/2015' AND '08/31/2015'
OR `as_date` BETWEEN '08/01/2015' AND '08/31/2015')
AND `aca_no` = 2
GROUP BY `st_student`.`st_id`
In case that your columns are declared as a varchar datatype you will need to use str_to_date function. Varchar cannot be compared as a date unless you convert it to one. Try this and let me know. Best of luck.
SELECT `st_student`.`st_id`, `ab_date`, `as_date` `st_status`
FROM `st_student`
WHERE (`st_status` = 1 OR `st_status` = 2)
AND STR_TO_DATE(`ab_date`,'%d/%m/%Y')
BETWEEN
STR_TO_DATE('01/08/2015','%d/%m/%Y')
AND
STR_TO_DATE('31/08/2015','%d/%m/%Y')
OR
STR_TO_DATE(`as_date`,'%d/%m/%Y')
BETWEEN
STR_TO_DATE('01/08/2015','%d/%m/%Y')
AND
STR_TO_DATE('31/08/2015','%d/%m/%Y')
AND `aca_no` = 2
GROUP BY `st_student`.`st_id`

Append number to front of string with leading 0

How do I correctly replace the first character with 'M'? Suppose you have a PATIENT_ID_NONNUM = 'M001', and we want 1001 as a result.
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_BIOMETRICS]
SET PATIENT_ID = CONVERT(NUMERIC(22,0),'1' + CONVERT(NVARCHAR(50),PATIENT_ID))
WHERE SUBSTRING(PATIENT_ID_NONNUM, 1, 1) = 'M'
EDIT:
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_MEDICATION]
SET PATIENT_ID = CONVERT(NUMERIC(22,0),CONVERT(NVARCHAR(50),'1') + CONVERT(NVARCHAR(50),SUBSTRING(PATIENT_ID_NONNUM, 2, LEN(PATIENT_ID_NONNUM))))
WHERE SUBSTRING(PATIENT_ID_NONNUM, 1, 1) = 'M'
I find STUFF() (an often overlooked function) and LEFT() are a little more readable, but others may disagree:
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_BIOMETRICS]
SET PATIENT_ID = CAST(STUFF(PATIENT_ID_NONNUM, 1, 1, '1') AS NUMERIC(22,0))
WHERE LEFT(PATIENT_ID_NONNUM, 1) = 'M'
I would suggest something like this:
UPDATE [HIMC_I2B2_LZ-PROD].[dbo].[I2B2_SRC_BIOMETRICS]
SET PATIENT_ID = CAST(('1' + SUBSTRING(PATIENT_ID_NONNUM, 2, LEN(PATIENT_ID_NONNUM) - 1)) AS NUMERIC(22,0))
WHERE SUBSTRING(PATIENT_ID_NONNUM, 1, 1) = 'M'
That's going to find all records where the first character is M, and replace the first character with a 1. I haven't tested this, but I believe it should work properly.
I would also suggest not running this type of operation on a production database as a test, which I would assume is what the -PROD stands for in your catalog name.
EDIT: Since it seems important that this query comes out with the PATIENT_ID as a NUMERIC(22,0), I've added the necessary CAST.

MySQL Odd-Joining Issue

I had the following code:
select DB.T1.ID,
DB.T1.B,
DB.T1.C,
DB.T2.ID,
DB.T2.B,
DB.T2.R,
DB.T3.ID,
DB.T3.Q
DB.T1.DUP,
DB.T2.DUP,
DB.T3.DUP
from DB.T1, DB.T2, DB.T3
where DB.T1.id = DB.T2.ID
and DB.T1.id = DB.T3.ID
and DB.T2.id = DB.T3.id
and DB.T1.DUP = 'not_duplicate'
and DB.T2.DUP = 'not_duplicate'
and DB.T3.DUP = 'not_duplicate'
;
The output returned 0 rows, however. So, I changed the values of the "DUP" column in each table from duplicate/not_duplicate instead to 0/1. I tried this code and it worked:
select DB.T1.ID,
DB.T1.B,
DB.T1.C,
DB.T2.ID,
DB.T2.B,
DB.T2.R,
DB.T3.ID,
DB.T3.Q
DB.T1.DUP,
DB.T2.DUP,
DB.T3.DUP
from DB.T1, DB.T2, DB.T3
where DB.T1.id = DB.T2.ID
and DB.T1.id = DB.T3.ID
and DB.T2.id = DB.T3.id
and DB.T1.DUP = 1
and DB.T2.DUP = 1
and DB.T3.DUP = 1
;
The second code works perfectly, the first one returned 0 rows. Does anyone know why was this happening? The values "not_duplicate" and "duplicate" were the exact same strings as the csv's that I imported into the database from. I can't explain why this would be the case and I'm really pretty curious.
Thanks very much!
because the DUP column they dont have this not_duplicate in fields. they have 1
then it doesnt match your query .
the query returns values which are stored in the column DUP .

IF condition in mysql

I have a contact table I wish to query when a certain condition exists. I tried the query below but am getting a syntax error.
SELECT *
FROM contact_details
WHERE contactDeleted` =0
AND IF ( contactVisibility = "private"
, SELECT * FROM contact_details
WHERE contactUserId = 1
, IF( contactVisibility = "group"
, SELECT * FROM contact_details
WHERE contactGroup = 3
)
)
If I'm understanding your question correctly (which is difficult with the lack of info you've provided. Sample datasets and expected outcomes are typically helpful), then I don't believe you need IFs at all for what you want. The following will return contacts that are not deleted and who either have (visibility = "private" and userId = 1) OR (visibility = "group" and group = 3)
SELECT *
FROM contact_details
WHERE contactDeleted = 0
AND (
(contactVisibility = "public")
OR
(contactVisibility = "private" AND contactUserId = 1)
OR
(contactVisibility = "group" AND contactGroup = 3)
)
I am assuming you want to use the IF() function and not the statement which is for stored functions..
Refer to this link for more information on that.
Notice that you have put 2 select statements in there, where the custom return values are supposed to be. So you are returning a SELECT *... now notice that in your upper level sql statement you have an AND.. so you basically writing AND SELECT *.. which will give you the syntax error.
Try using .. AND x IN (SELECT *) .. to find if x is in the returned values.
Let me also list this link to make use of an existing and well written answer which may also applicable to your question.