SQL Where clause and like query with letter and numbers - sql

I have a table with project numbers. The project numbers have different formats. I want to filter just the project numbers starting with C and 4 numbers (i.e. C1234) and with underscore 3 numbers (i.e. C1234_123). Other formats that i donĀ“t want have a lot more numbers, points in between etc.
Can you help me with the WHERE clause?
WHERE
Projectnumber LIKE 'C____%'
Here I also get the project numbers that have more than 4 numbers after the C. How to define a filter for this format C1234_123 ?
KR Julia

Here is the answer with a working example,
SELECT
[ProjectNumber]
FROM
[Stuff]
WHERE
[ProjectNumber] LIKE 'C[0-9][0-9][0-9][0-9]\_[0-9][0-9][0-9]' ESCAPE '\';
Note the use of the ESCAPE clause to prevent the _ being treated like a wildcard.

Related

Use regexp inSQL/PL for no repetitions in digits

I have a table below and need to use a regular expression to display the correct course digits ( 3 digits being the max # of digits) and also max sure no number appears twice.
the following code I have created is this:
select regexp_substr(course_number,'([0-9][0-9][0-9])\1') as CourseNumber
from courses
/
this is correct showing me an output of this:
How can I sure the regexp in sql/pl to query the digits with no numbers repeating?
the table I created looks like this:

SQL query to exclude numbers, brackets or special characters at the end of a name

I'm trying to exclude all names with numbers in it, but still include certain special characters like:
()-'
So far, i've got this query:
SELECT FirstName FROM Client WHERE FirstName NOT LIKE '%[^0-9]%';
This excludes most names with numbers in it, however, a few like the following are still coming through (not being excluded):
How do i exclude these from above, but still keep rows that look like the ones below:
Thank you in advance!
I'm trying to exclude all names with numbers in it.
Assuming you are using SQL Server, your logic has too many negations. You want:
WHERE FirstName NOT LIKE '%[0-9]%';
This translates to FirstName not having any number in it.
If you want names that are only alphabetical and spaces, then use:
WHERE FirstName NOT LIKE '%[^a-zA-Z ]%';
Your version simply states that the string has no character which is not like a digit -- that is, it consists only of digits.

Hive SQL Extract string of varying length between two non-alphanumeric characters

I would like to extract strings of varying length located between two repeating underscores in Hive QL. Below I show a sampling of the pattern of the rows. Specifically, I would like to extract the string between the 3rd and 4th underscores. Thanks!
2016_sadfsa_IL_THIS_xsdaf_asd_eventbyevent_tsaC_NA_300x250
2017_thisshopper_MA_THIS_NAT_Leb_ReasonsWhy_HDIMC_NA_300x600
2017_FordShopper_IL_THESE_NAT_sov_winterEvent_HDIMC_NA_300x600
Just kept trying and I modified this from previous responses to non-Hive SQL. I am still interested in knowing better ways of doing this. Note that creative_str is the name of the column:
select creative_str, ltrim(rtrim(substring(regexp_replace(cast(creative_str as varchar(1000)), '_', repeat(cast(' ' as varchar(1000)),10000)), 30001, 10000)))
from impression_cr
You should be able to do this with Hive's SPLIT() function. If you're trying to grab the value between the third and fourth underscores, this will do it:
SELECT SPLIT("2016_sadfsa_IL_THIS_xsdaf_asd_eventbyevent_tsaC_NA_300x250", "[_]")[3],
SPLIT("2017_thisshopper_MA_THIS_NAT_Leb_ReasonsWhy_HDIMC_NA_300x600", "[_]")[3],
SPLIT("2017_FordShopper_IL_THESE_NAT_sov_winterEvent_HDIMC_NA_300x600", "[_]")[3]

Remove unnecessary Characters by using SQL query

Do you know how to remove below kind of Characters at once on a query ?
Note : .I'm retrieving this data from the Access app and put only the valid data into the SQL.
select DISTINCT ltrim(rtrim(a.Company)) from [Legacy].[dbo].[Attorney] as a
This column is company name column.I need to keep string characters only.But I need to remove numbers only rows,numbers and characters rows,NULL,Empty and all other +,-.
Based on your extremely vague "rules" I am going to make a guess.
Maybe something like this will be somewhere close.
select DISTINCT ltrim(rtrim(a.Company))
from [Legacy].[dbo].[Attorney] as a
where LEN(ltrim(rtrim(a.Company))) > 1
and IsNumeric(a.Company) = 0
This will exclude entries that are not at least 2 characters and can't be converted to a number.
This should select the rows you want to delete:
where company not like '%[a-zA-Z]%' and -- has at least one vowel
company like '%[^ a-zA-Z0-9.&]%' -- has a not-allowed character
The list of allowed characters in the second expression may not be complete.
If this works, then you can easily adapt it for a delete statement.

Display certain sequence only in VARCHAR

I have a column error_desc with values like:
Failure occurred in (Class::Method) xxxxCalcModule::endCustomer. Fan id 111232 is not Effective or not present in BL9_XXXXX for date 20160XXX.
What SQL query can I use to display only the number 111232 from that column? The number is placed at 66th position in VARCHAR column and ends 71st.
SELECT substr(ERROR_DESC,66,6) as ABC FROM bl1_cycle_errors where error_desc like '%FAN%'
This solution uses regular expressions.
The challenge I faced was on pulling out alphanumerics. We have to retain only numbers and filter out string,alphanumerics or punctuations in this case, to detect the standalone number.
Pure strings and words not containing numbers can be easily filtered out using
[^[:digit:]]
Possible combinations of alphanumerics are :
1.Begins with a character, contains numbers, may end with characters or punctuations :
[a-zA-Z]+[0-9]+[[:punct:]]*[a-zA-Z]*[[:punct:]]*
2.Begins with numbers and then contains alphabets,may contain punctuations :
[0-9]+[[:punct:]]*[a-zA-Z]+[[:punct:]]*
Begins with numbers then contains punctuations,may contain alphabets :
-- [0-9]+[a-zA-Z][[:punct:]]+[a-zA-Z] --Not able to highlight as code, refer solution's last regex combination
Combining these regular expressions using | operator we get:
select trim(REGEXP_REPLACE(error_desc,'[^[:digit:]]|[a-zA-Z]+[0-9]+[[:punct:]]*[a-zA-Z]*[[:punct:]]*|[0-9]+[[:punct:]]*[a-zA-Z]+[[:punct:]]*|[0-9]+[a-zA-Z]*[[:punct:]]+[a-zA-Z]*',' '))
from error_table;
Will work in most cases.