how to display employee names starting with a and then b in sql

前端 未结 12 1618
谎友^
谎友^ 2021-02-05 19:58

i want to display the employee names which having names start with a and b ,it should be like list will display employees with \'a\' as a first letter and then the \'b\' as a

相关标签:
12条回答
  • 2021-02-05 20:47

    Oracle: Just felt to do it in different way. Disadvantage: It doesn't perform full index scan. But still gives the result and can use this in substring.

    select employee_name 
    from employees
    where lpad(employee_name,1) ='A'
     OR lpad(employee_name,1) = 'B'
    order by employee_name
    

    We can use LEFT in SQL Server instead of lpad . Still suggest not a good idea to use this method.

    0 讨论(0)
  • 2021-02-05 20:48

    To get employee names starting with A or B listed in order...

    select employee_name 
    from employees
    where employee_name LIKE 'A%' OR employee_name LIKE 'B%'
    order by employee_name
    

    If you are using Microsoft SQL Server you could use

    ....
    where employee_name  LIKE '[A-B]%'
    order by employee_name
    

    This is not standard SQL though it just gets translated to the following which is.

    WHERE  employee_name >= 'A'
           AND employee_name < 'C' 
    

    For all variants you would need to consider whether you want to include accented variants such as Á and test whether the queries above do what you want with these on your RDBMS and collation options.

    0 讨论(0)
  • 2021-02-05 20:48

    Here what I understood from the question is starting with "a " and then "b" ex:

    • abhay
    • abhishek
    • abhinav

    So there should be two conditions and both should be true means you cant use "OR" operator Ordered by is not not compulsory but its good if you use.

    Select e_name from emp 
    where  e_name like 'a%' AND e_name like '_b%'
    Ordered by e_name
    
    0 讨论(0)
  • 2021-02-05 20:49

    From A to Z:

    select employee_name from employees ORDER BY employee_name ;
    

    From Z to A:

    select employee_name from employees ORDER BY employee_name desc ;
    
    0 讨论(0)
  • 2021-02-05 20:53
    select * 
    from stores 
    where name like 'a%' or 
    name like 'b%' 
    order by name
    
    0 讨论(0)
  • 2021-02-05 21:00

    We can also use REGEXP

    select employee_name 
    from employees
    where employee_name REGEXP '[ab].*'
    order by employee_name
    
    0 讨论(0)
提交回复
热议问题