how to write sql query using substring - mysql

I have some fields in a table Employee, with fields
empID,empName,empAddress,empCity...
And all the values of empAddress field contains like "ADR 250 Candy", "ADR 330 Simla, "ADR 220 Karty" and so on.
I want a query to list otr the address without the word 'ADR'.
Eg: I want to display the address like "250 Candy", "330 Simla", "220 Karty" ....
Please help me on this

Try
SELECT SUBSTR(E.empAddress, 4) AS 'Address' FROM Employee E
If you want to skip the space character also replace SUBSTR(E.empAddress, 4) with SUBSTR(E.empAddress, 5)

try this :
SELECT EmpAddress = REPLACE(E.empAddress, 'ADR ', '')
FROM Employee E

Related

SQL Is there a way to link two tables with the same value but different formats?

So I have two columns from two different databases that I would like to link.
Problem is that my first column outputs the numbers with this format "1 789 987" and my second column outputs the data "0000000001789987"
How can I write my WHERE sql forumla to idententify these as matching?
Ok so I pulled out the qrys to excel to provide you with more information.
Here are the different tables.
Looks like Tbl2 has NUM column set to text. And even though the QRY in the program gave spaces to the numbers in Tbl1 it looks like the qry removed them shrug
SELECT *
FROM "Tbl1","Tbl2"
WHERE "Tbl1"."num" = "Tbl2"."num"
AND "Tbl1"."Date" BETWEEN '2019-01-21' AND '2019-01-25'
I hope the information became abit clearer. I'm new to SQL and Stackoverflow, i'll try and improve my questions information in the future.
Well, to transform format 1 to format 2 you can try something like this :
set #format1 = "1 789 987";
set #format2 = "0000000001789987";
select LPAD(REPLACE(#format1, ' ', ''), 16, "0") as format1, #format2 as format2
Output is :
====================================
format1 | format2
====================================
0000000001789987 | 0000000001789987
This way format1 looks like format2 if you test it. The REPLACE remove the ' ' and the LPAD will fill the string with 0 untill the string is 16 char length like the format2.
So you can use this in you WHERE condition :
...WHERE LPAD(REPLACE(your_first_column, ' ', ''), 16, "0") = your_other_column
Now you can try to transform both column in int too, you didn't provide lot of information about those format so hard to find the best solution !
This cast may fit for you:
NOTE: tbl1 contains ids like: 1 789 987
select *
from tbl1 join tbl2 on (
cast( -- casts to integer
replace(tbl1.text_id, ' ', '') -- removes whitespaces
as int) =
tbl2.numeric_id -- join on second table
)
In any case please provide sample data and a testable example what you did and the results you need

Delete a specific value from a string (string contains comma separated values)

For example, if I have a table structure like this:
Table 1
ID Name Value
001 Rajesh 90,100,210,400
002 Suresh 100,400,300,66
003 Mahesh 200,500
004 Virat 400,578,57
How can I delete 400 from Suresh?
DELETE Value ="400" FROM table1
WHERE Name = 'Suresh'
This doesn't work.
I would recommend splitting the values into a second table which is related via the person's ID. However, you can use the following query for your current situation:
UPDATE table1
SET Value = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', Value, ','), ',400,', ','))
WHERE Name = 'Suresh'
Here's a SQL Fiddle. For reference, see MySQL's string functions

Select query or select entries that don't start with a number - MySQL

I need to select all entries that do not start with a number between 1-9.
Example Entries:
6300 Dog Lane
Kitty Drive
500 Bird Chrest
800 Tire Road
Johnson Ave
Park Ave
So if I ran a query on the above, I would expect:
Kitty Drive
Johnson Ave
Park Ave
The table is called objects and the column is called location.
Something I tried:
SELECT DISTINCT name, location FROM object WHERE location NOT LIKE '1%' OR '2%' OR '3%' OR '4%' OR '5%' OR '6%' OR '7%' OR '8%' OR '9%';
Unfortunately, that is unsuccessful. Is this possible? If no, I will resort to modifying the data with Perl.
Try this:
SELECT DISTINCT name, location FROM object
WHERE substring(location, 1, 1)
NOT IN ('1','2','3','4','5','6','7','8','9');
or you have to add NOT LIKE before every number:
SELECT DISTINCT name, location FROM object
WHERE location NOT LIKE '1%'
OR location NOT LIKE '2%'
...
You can use the following stntax:
SELECT column FROM TABLE where column NOT REGEXP '^[0-9]+$' ;
SELECT DISTINCT name, location FROM object
WHERE location NOT REGEXP '^[0-9]+$' ;
Try this. It's simpler:
SELECT DISTINCT name, location FROM object WHERE location NOT LIKE '[0-9]%';
What you "tried" needed to have AND instead of OR. Also, DISTINCT is unnecessary.
If you have
INDEX(location)
this would probably be faster than any of the other answers:
( SELECT name, location FROM object
WHERE location < '1'
) UNION ALL
( SELECT name, location FROM object
WHERE location >= CHAR(ORD('9' + 1)) )
This technique only works for contiguous ranges of initial letters, such as 1..9.
A somewhat related question: Should I perform regex filtering in MySQL or PHP? -- it asks about fetching rows starting with 1..9 instead of the opposite.
Try this for SQL Server:
select column_name
from table
where substring(column_name,1,1) not in (1,2,3,4,5,6,7,8,9)
ISNUMERIC should work. (will exclude 0 as well).
Sample code -
ISNUMERIC(SUBSTRING(location, 1, 1)) = 0

How to create a computed column name using another column's value (sql server 2008)

I need to use the value of a column in the name of a new column....this is the line I need help with:
Count([DepartmentName]) As [[DepartmentName] + "Emails"]
Code:
SELECT
[CustomerId],
#MetricMonth AS "MetricMonth",
#MetricYear AS "MetricYear",
[LeadType], [DeviceTypeId], [DepartmentName],
Count([DepartmentName]) As [[DepartmentName] + "Emails"]
FROM
[myTable]
WHERE
LeadType = #LeadType
AND _CreateDate BETWEEN #StartDateTime AND #EndDateTime
GROUP BY
[CustomerId], [LeadType], [DeviceTypeId], [DepartmentName]
The reason for the need is that the receiving table has columns labeled as such and this seems like the cleanest way to do it. There are 16 possible values for DepartmentName so I don't want to have a bunch of case statements.
Here's a sample of the result. There will be multiple groups because of DepartmentName and DeviceTypeId.
CustomerId MetricMonth MetricYear LeadType DeviceTypeId DepartmentName NewName
28590 4 2014 Email 1 New 9
36980 4 2014 Email 1 Finance 3
876 4 2014 Email 1 New 9
Thanks!
You in effect want a column name that has multiple values, ie a column with multiple names, which is just impossible in any flavor of SQL, afaik.
Short of that, you have two options:
if you really want columns with names like "Department1 Emails" then you will have to pivot the data (and you'll have to hard-code all the Department Names). If that is what you want see here.
if you just want a column called "Department Emails" with values such as "Department1 Emails: 30" then you can do this:
SELECT [DepartmentName], [DepartmentName] + ' Emails: ' + CAST(COUNT([DepartmentName]) AS VARCHAR(20))
FROM [myTable]
GROUP BY [DepartmentName]

Search string mysql db that can have spaces, be in another string, etc

I have this database wich contains product codes like
EXA 075 11112
0423654
3 574 662 123
JOLA 22354 5
LUCS 2245 785
I use a query with %LIKE% to list the products mathing a string entered by the user for example "22" would list
JOLA 22354 5
LUCS 2245 785
The problem is that the user does not necessarily know the format of the code, so it types in 07511112 and the output is zero, because "EXA 075 11112" is not matched by %LIKE%.
Is there a way to construct the query to trim all spaces from the product field before the search occurs, and then search by the string also trimed of spaces using %LIKE% ? I guess it should then match all entries. Or is there another way ?
I cannot run replace ' ', '' on the column, the codes must remains as there are now.
You could use replace function
select *
from mytable
where REPLACE( `productcode` , ' ' , '' ) like '%searchparam%'