Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select where column begins with Letters

I have a table tbl1 with row of data:

ID       TIN     SSS
10001    none    1000-20
10002    69098   PRC
10003    69099   INC

I want to query the the Legal_Doc_No of each ID. The value of each ID is either the TIN or the SSS column.

How can I query the TIN and SSS column starting with letters (none) so that only values starting with numbers will be assigned to Legal_Doc_No

Select
ID,
'Legal_Doc_No' = case when TIN = Letter then SSS
 else TIN end
from tbl1
like image 575
Ryan Abarquez Avatar asked Dec 08 '15 03:12

Ryan Abarquez


People also ask

How do you get the first 5 letters in SQL?

SQL Server LEFT() Function The LEFT() function extracts a number of characters from a string (starting from left).

How do I get the first letter of each word in SQL?

Use the INITCAP() function to convert a string to a new string that capitalizes the first letter of every word. All other letters will be lowercase.

How do you SELECT words that start with a vowel in SQL?

Method 1: To check if a name begins ends with a vowel we use the string functions to pick the first and last characters and check if they were matching with vowels using in where the condition of the query. We use the LEFT() and RIGHT() functions of the string in SQL to check the first and last characters.


1 Answers

Most databases support left(), so you can do something like this:

select id,
       (case when left(time, 1) between 'a' and 'z' or left(time, 1) between 'A' and 'Z'
             then SSS else TIN
        end) as Legal_Doc_no
from tbl1;

Depending on the database, there might be other solutions.

In SQL Server, you can do:

select id,
       (case when time like '[a-z]%'
             then SSS else TIN
        end) as Legal_Doc_no
from tbl1;

If you have a case-sensitive collation, then you'll need to take that into account:

select id,
       (case when lower(time) like '[a-z]%'
             then SSS else TIN
        end) as Legal_Doc_no
from tbl1;
like image 192
Gordon Linoff Avatar answered Sep 21 '22 02:09

Gordon Linoff