SQL Where statement for strings - sql

I have a column that's string and I would like to use a WHERE statement for all strings that begin with the "sea_"
Any ideas on how to achieve that will be appreciated.
Thanks in advance

It's not clear what exactly your where clause should do. If necessary, you should please add more information. Anyway, in general, you can do such a select:
SELECT column FROM table WHERE column LIKE 'sea_%'
The % means that zero, one or more characters can follow.
If you need further conditions like an exact number of trailing characters etc., you please must specify your question.

If you like to ignore Upper/Lower case , it can help you
SELECT * FROM table WHERE LOWER(column) LIKE 'sea_%'

Be aware that underscore _ is itself a placeholder for any single character which must be escaped in order to use it literally.
The criteria you should use to ensure an underscore is also present is
where column like 'sea[_]%';
You can also define your own explicit escape character, such as
where column like 'sea#_%' escape '#';

Related

Splitting variable content in SQL

I have a variable in a stored procedure that contains a string of characters like
[Tag]MESSAGE[/Tag]
I need a way to get the MESSAGE part from within the tags.
Any help would be much appreciated
Note: I have tested it on Oracle RDBMS
A more reliable approach is to use REGEXP_REPLACE.
REGEXP_REPLACE(value, pattern)
Example
SELECT REGEXP_REPLACE(
'<Tag>Message</Tag>',
'\s*</?\w+((\s+\w+(\s*=\s*(".*?"|''.*?''|[^''">\s]+))?)+\s*|\s*)/?>\s*') FROM DUAL;
Just replace "<" with "[" if your tags are different
What you need is this:
SELECT SUBSTRING(ColumnName,CHARINDEX('html_tag',ColumnName)+LEN('html_tag'),CHARINDEX('html_close_tag',ColumnName)-LEN('html_close_tag')) FROM TableName
You'll require to change the html_tag and html_close_tag with your own HTML tag that you want to get rid of.
If the column contains only single tag, simple call of substring function should be enough. Otherwise there will always be some point where regular expression does not suffice since you fall into trap (see this legendary StackOverflow answer).

SQL LIKE to find either -,/,_

Trying to select from table where the format can be either 1/2/2014, 1-2-2014 or 1_2_2014 in a text field. There's other text involved outside of this format but it shouldn't matter, but that's why this is text not a date type.
I tried '%[-,_,/]%[-,_,/]%' which doesn't work, and I've tried escaping the special characters in the brackets such as %[-,!_,/]%[-,!_,/]%' ESCAPE '!' which also doesn't work. Any suggestions?
I wanted to avoid using three searches like,
LIKE '%/%/%'
OR '%-%-%'
OR '%!_%!_%' ESCAPE '!'
EDIT: Using SQLite3
There is no regex like behavior in using the LIKE operator in SQL. You would have use two expressions and OR them together:
select * from table
where column like '%-%-%'
or column like '%/%/%'
Thanks for the information. I ended up switching to the GLOB operator which support [] in SQLite.
The Example was altered to GLOB '?[/-_]?[/-_]??*' Where * serves as % and ? serves as _ for the GLOB function.
Also thanks to Amadeaus9 for pointing out minimum characters between delimiters so that '//' isn't a valid answer.
If you're using T-SQL (AKA SQL Server) you don't want to have commas in the character set - i.e. LIKE '%[/_-]%[/_-]%'. However, keep in mind that this can match ANYTHING that has, anywhere within it, any two characters from the set.
EDIT: it doesn't looke like SQLite supports that sort of use of its LIKE operator, based on this link.
Relevant quote:
There are two wildcards used in conjunction with the LIKE operator:
The percent sign (%)
The underscore (_)
However, you may want to take a look at this question, which details using regex in SQLite.
It is not possible using the LIKE syntax.
However Sqlite3 would support the REGEXP operator; this is syntactic sugar for calling an user defined function that actually does the matching. If provided by your platform, then you could use for example
x REGEXP '.*[/_-].*[/_-].*'

SQL IN clause RegEx

I am trying to write regural expression to validate the SQL IN clause where values inside bracket are numbers (ids) e.g (23,109,1) but NOT (23,109,) or (23,,) or ().
My current expression is:
^\([0-9,]+\)$
but it allows also the wrong values.
I am not really good at regural expressions, also tried something like:
^\(([0-9]+,)+\)$
but I guess it's not the point.
Any ideas?
Your second try is almost there; the problem is, ^\(([0-9]+,)+\)$ would require trailing comma. Let's try ^\([0-9]+(,[0-9]+)*\)$.
No idea on your regex library/dialect; maybe there's much to be improved (\d for digits; allowing spaces between elements; etc).

Insert double quotes into SQL output

After I run a query and view the output, for example
select * from People
My output is as follows
First Last Email
Ray Smith raysmith#whatever.itis
How would I export this data so that it looks as follows?
"Ray","Smith","raysmith#whatever.itis"
Or is there a way to do this within SQL to modify records to contain quotes?
Because when you export, it's going to include the commas anyway, right?
If the columns you're interested in are 128 characters or less, you could use the QUOTENAME function. Be careful with this as anything over 128 characters will return NULL.
SELECT QUOTENAME(First, '"'), QUOTENAME(Last, '"'), QUOTENAME(Email, '"')
FROM People
select '"'+first+'","'+last+'","'+email+'"'
from people
This is the kind of thing best done in code however, you shouldn't query for presentation.
select concat(“\"”,first,“\"”,“\"”,Last,“\"”,“\"”,Email,“\"”) as allInOne
Modifying the records to contain quotes would be a disaster; you don't use the data only for export. Further, in theory you'd have to deal with names like:
Thomas "The Alley Cat" O'Malley
which presents some problems.
In Standard SQL, you'd use doubled-up single quotes to enclose single quotes (with no special treatment for double quotes):
'"Thomas "The Alley Cat" O''Malley"'
Some DBMS allow you to use double quotes around strings (in Standard SQL, the double quotes indicate a 'delimited identifier'; SQL Server uses square brackets for that), in which case you might write the string as:
"""Thomas ""The Alley Cat"" O'Malley"""
Normally, though, your exporter tools provide CSV output formatting and your SQL statement does not need to worry about it. Embedded quotes make anything else problematic. Indeed, you should usually not make the DBMS deal with the formatting of the data.
This worked best for me
SELECT 'UPDATE [dbo].[DirTree1] SET FLD2UPDATE=',QUOTENAME(FLD2UPDATE,'''')
+' WHERE KEYFLD='+QUOTENAME(KEYFLD,'''')
FROM [dbo].[Table1]
WHERE SUBSTRING(FLD2UPDATE,1,2) = 'MX'
order by 2
If you are using MS SQL Server, try something like:
SELECT '"'||Table.Column||'"'
FROM Table
-- Note that the first 3 characters between "SELECT" and "||" are:
' " '
-- The characters are the same after "||" at the end... that way you get a " on each side of your value.

How to get column which might contain special characters in sql server

I have a situation where the name column comprises of many special characters. I have a solution where I do Like with all the special characters mentioned like this '%[''",/#$!-#%^&*.\+-]%'`
But this I think is not a good way to solve the problem. Is there a way where I can use Regular Expression within SQL query itself for checking if the name column contains special characters or not. Special characters would be everything apart from alphabets and numbers.
I know Regex can be used with C# and T-SQL. Looking for something if can be done through native SQL
You can use
WHERE yourcolumn LIKE '%[^0-9a-zA-Z]%' COLLATE Latin1_General_BIN
http://msdn.microsoft.com/en-us/magazine/cc163473.aspx