I am currently making a program in c# that uses SQL queries. I am new to SQL and would like some help on a matter.
Is it possible for a specific position in a string to be queried?
Eg. SELECT ID FROM Names WHERE Firstname[0] = "J" AND Lastname = "Doe"
If anything is unclear please let me know, any help is appreciated, thank you.
Yes, you can, but rather like this
SELECT ID
FROM Names
WHERE Firstname LIKE 'J%' AND Lastname = 'Doe';
Notes:
In SQL, strings should be delimited with single quotes.
The LIKE operation has a pattern. The pattern says the first character is J; % is a wildcard that matches 0 or more characters.
Databases generally do not have a built-in array types that are compatible across databases. Further, no databases (as far as I know) , treats strings as arrays of characters. These concepts are somewhat foreign to SQL (although some databases do support arrays).
You could use LIKE in WHERE clause.
example:-
SELECT ID FROM Names WHERE Firstname LIKE "J%" AND Lastname = "Doe"
you can use keyword 'like' for example : WHERE names LIKE 'a%' it will return all name starting with character 'a'. like wise '%a'it will return all names ending with 'a'.
Related
The records I am querying for are kept in 2 different formats. Each person has at least 1 record of their email in the format John.Doe#abc.com. Some people have a second record in which their email is DoeJ#abc.com.
How can I query for the records in which the email is formatted like John.Doe#abc.com?
I attempted to do it with the following SQL Statement but I it returns an empty result:
Select * from email where regexp_like(emailaddress, '. (#)')
The end product will be used in a join with a few other Queries, so selecting distinct values is not an option here. The environment is an Oracle DB, and because this will be done through multiple joins, the more efficient it is the better. Does anyone have any ideas what I am doing wrong, or other ways to accomplish this?
Thank you,
Joshua
You can use REGEXP_LIKE:
Select * from email where regexp_like(emailaddress, '\S*\.\S*\#\S*\.\S*')
Use "\S*" to match all non-whitespace characters
Or just a regular LIKE:
Select * from email where emailaddress LIKE '%.%#%.%'
Not sure what characters are included in the % placeholder in Oracle, so you should test it out.
The REGEXP one will give you tighter control over the pattern matching.
Let me know if it works.
How about using like?
where emailaddress like '%.%#%'
The first format seems distinguished by having a period before the ampersand.
I have a database that contains columns that are text fields (strings, a few sentences long) and columns that are shorter strings (eg: college majors). Is there any way I can use the 'LIKE' function in SQL to search whether one of the values of the College Major column appears in another column?
I don't want to write out each of the college majors as a string since there are over 100.
Yes you can. Something like
where bigdatacolumn like '%' + computer_major + '%'
Since you said, other column contains few lines (Text column), you probably want to consider using Full Text Search instead of using LIKE operator
Use The ANY operator to compare in an array as shown in the below query.
select * from staff where department ILIKE ANY ( ARRAY['AUT%', '%COM%' ,'SP%' ] :: TEXT[] );
I'm trying to run an SQL query involving converting one datatype to another.
SELECT
convert(char(12),name) as NAME
FROM
people
WHERE
convert(char(12),place) = 'Chicago'
This works. However, I'm not sure how to alter the query to use wildcard characters. I imagine it would be something like the following (which does not work in its current state) -
SELECT
convert(char(12),name) as NAME
FROM
people
WHERE
convert(char(12),place) = '%Chicago%'
Please help.
If you want to use wildcard characters, you need to use LIKE:
select convert(char(12),name) as NAME
from people
where convert(char(12),place) LIKE '%Chicago%'
How to retrieve a column containing special characters including alphabets in SQL Query. i have a column like this 'abc%def'. i want to retrieve '%' based columns from that table.
Please help me in this regard.
Is abc%def the column name? or column value? Not sure what you are asking but if you mean your column name contains special character then you can escape them which would be different based on specific RDBMS you are using
SQL Server use []
select [abc%def] from tab
MySQL use backquote
select `abc%def` from tab
EDIT:
Try like below to fetch column value containing % character (Checked, it works in Ingres as well)
select * from tab where col like '%%%'
Others suggest that like '%%%' works in Ingres. So this is something special in Ingres. It does not work in other dbms.
In standard SQL you would have to declare an escape character. I think this should work in Ingres, too.
select * from mytable where str like '%!%%' escape '!';
i want to select name that only contain one word with SQL wildcards..
i have tried
select name from employee where name not like '% %'
it works,but i wonder if there are other ways to do it using SQL wildcards
note : i am a college student,i am studying wildcards right now . i was just wonder if there are other ways to show data that only contain one word with wildcards except the above..
Your method makes proper use of wildcards, alternatively you could do it with CHARINDEX or similar function depending on RDBMS
select name
from employee
where CHARINDEX(' ',name) = 0
Likewise the patindex function or similar use wildcards, but that's pretty much the same as CHARINDEX, just allows for patterns, so if looking for multiple spaces it would be helpful. I don't think there's much in the way of variation from your method for using wildcards.
If you have large database I would suggest to create new indexed column word_count which would be autofilled by insert/update trigger. Thus you will be able to search for such records more efficiently.
That's the way I'd do it using wildcards. The other way would be:
select name
from employee
where charindex(' ', name) = 0