how to get rows which begin with A(capital letter)? - sql-server-2008

how could I select rows that begin with capatil latter A
I've used this but it won't work
select name from myTable
where name like 'A%'
but the selected rows began with a (lower case ) and A (upper case)

Try this:
select name from myTable
where name like 'A%'
COLLATE SQL_Latin1_General_CP1_CS_AS
where the ...CS... means Case-Sensitive.

Related

Select rows that fully match the end of a string

Column1 has a record with the value example.
A user submits the value somethingwithexample.
The end of his string matches the value in Column1. How to SELECT Column1 in MySQL?
You could use the like operator:
SELECT *
FROM mytable
WHERE :userinput LIKE CONCAT('%', mycolumn)
EDIT:
As noted in the comment, if the column is empty, this will evaluate as userinput LIKE '%', which is by definition always true. This edge-case should be handled explicitly:
SELECT *
FROM mytable
WHERE mycolumn IS NOT NULL AND
mycolumn != '' AND
:userinput LIKE CONCAT('%', mycolumn)
select * from table_name
where column_name like '%example'
select * from your_table
where $userInput like concat('%', col)
select * from tablename where value like '%example';
will select all rows whose value column has values that are ending with example
for example: mathexample, scienceexample;
select * from tablename where value like '%example%';
will select all rows whose value column has values such as mathexample1, scienceexample2 etc

MySQL order incorrect with capitalized letters

I'm trying to order at table with:
select * from my_table order by name
but something goes wrong, since:
IT is listed before Ib
For some reason capitalized letters are seen as more important?
Try this for case insensitive ordering:
SELECT * FROM `my_table` ORDER BY `name` COLLATE 'latin1_general_ci'

how to know the count of words starting with a particular word in database?

I am using a mysql database for storing the data. The table consists of 2 columns s_no and name. I used index on this table on name. Now I want to get only words starting with a particular letter say "a" or "b" etc. How can I do it? Is there any SQL query for retrieving data in this fashion?
select s_no,name from tablename where name like 'a%' or name like 'b%'
select * from table_name
where name like 'a%'
select s_no,name from table where name like 'a%' or name like 'b%'
Select * from tablename where name like 'a%'
You can also pass the value inside parameters
#value varchar(50)
Select * from tablename where name like #value
so whatever characters u insert like a or b just mearge it with % and here you go
like #value=#value+ '%' before query .

MySQL magic column "name" and synthesized columns

So I got a SQL statement. The idea is that I want to do a case-insensitive LIKE.
I do it like this:
SELECT
FilenameId AS id,
LOWER(CONVERT(BINARY(Filename.Name) USING utf8)) AS name
FROM Filename
WHERE name LIKE '%something%'
COLLATE utf8_general_ci
This works fine, however my query also returns the case-transformed name. What I want to do
is synthesize the insensitive name and do a LIKE query on it, but also return the non case-transformed name.
SELECT
FilenameId AS id,
Filename.Name AS name,
LOWER(CONVERT(BINARY(Filename.Name) USING utf8)) AS iname
FROM Filename
WHERE iname LIKE '%something%'
COLLATE utf8_general_ci
...but then MySQL happily refuses:
Unknown column 'iname' in 'where clause'
What am I doing wrong? I am on MySQL 5.5 FWIW.
I don't know why you came up with this, but usually others have trouble getting LIKE case sensitive, not the other way round.
Write your query simply like this:
SELECT
FilenameId AS id,
Filename.Name AS name
FROM Filename
WHERE name LIKE '%something%'
And in general, you can't access aliases in WHERE clause. Either put your query into a subquery like Dhinakaran suggested or use HAVING (if you are lazy).
The difference? WHERE is rowbased, HAVING works on the result after applying WHERE clause (and GROUP BY).
From the manual:
The following two statements illustrate that string comparisons are not case sensitive unless one of the operands is a binary string:
mysql> SELECT 'abc' LIKE 'ABC';
-> 1
mysql> SELECT 'abc' LIKE BINARY 'ABC';
-> 0
Create a sub query then put where on subquery
Select * from (SELECT
FilenameId AS id,
Filename.Name AS name,
LOWER(CONVERT(BINARY(Filename.Name) USING utf8)) AS iname
FROM Filename ) temp
WHERE temp.iname LIKE '%something%'
COLLATE utf8_general_ci
Or
SELECT
FilenameId AS id,
Filename.Name AS name,
AS iname
FROM Filename
WHERE LOWER(CONVERT(BINARY(Filename.Name) USING utf8)) LIKE '%something%'
COLLATE utf8_general_ci

mysql alphabetical order

i am trying to sort mysql data with alphabeticaly like
A | B | C | D
when i click on B this query runs
select name from user order by 'b'
but result showing all records starting with a or c or d i want to show records only starting with b
thanks for help
i want to show records only starting with b
select name from user where name LIKE 'b%';
i am trying to sort MySQL data alphabeticaly
select name from user ORDER BY name;
i am trying to sort MySQL data in reverse alphabetic order
select name from user ORDER BY name desc;
but result showing all records
starting with a or c or d i want to
show records only starting with b
You should use WHERE in that case:
select name from user where name = 'b' order by name
If you want to allow regex, you can use the LIKE operator there too if you want. Example:
select name from user where name like 'b%' order by name
That will select records starting with b. Following query on the other hand will select all rows where b is found anywhere in the column:
select name from user where name like '%b%' order by name
You can use:
SELECT name FROM user WHERE name like 'b%' ORDER BY name
If you want to restrict the rows that are returned by a query, you need to use a WHERE clause, rather than an ORDER BY clause. Try
select name from user where name like 'b%'
You do not need to user where clause while ordering the data alphabetically.
here is my code
SELECT * FROM tbl_name ORDER BY field_name
that's it.
It return the data in alphabetical order ie; From A to Z.
:)
I had the same challenge, but after little research I came up with this and it gave me what I wanted, and I was able to overcome that path.
SELECT * from TABLE ORDER BY name
Wildcard Characters are used with like clause to sort records.
If we want to search a string which is starts with B then code is like the following:
select * from tablename where colname like 'B%' order by columnname ;
If we want to search a string which is ends with B then code is like the following:
select * from tablename where colname like '%B' order by columnname ;
If we want to search a string which is contains B then code is like the following:
select * from tablename where colname like '%B%' order by columnname ;
If we want to search a string in which second character is B then code is like the following:
select * from tablename where colname like '_B%' order by columnname ;
If we want to search a string in which third character is B then code is like the following:
select * from tablename where colname like '__B%' order by columnname ;
Note : one underscore for one character.
I try to sort data with query it working fine for me please try this:
select name from user order by name asc
Also try below query for search record by alphabetically
SELECT name FROM `user` WHERE `name` LIKE 'b%'
MySQL solution:
select Name from Employee order by Name ;
Order by will order the names from a to z.