Data pattern SQL - sql

This I am sure is a fairly simple question. Clients are entering data into a column that looks like this 600/4768/4. I need to be able to remove the / once the data has been entered. How would I do this?
It is usually entered in this format as it is being referenced from another source in this pattern.

You could do it either in program before data is submitted, or in sql. Exactly how depends on what you're programming with, an what database you're using.
in MySql you can do this: replace('00/4768/4', '/', ''); Most any rdbms will have a similar function.
http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_replace
I usually find it's easier to do this kind of thing in the program than in sql though.

Using SQL SERVER you can try REPLACE (Transact-SQL)
Replaces all occurrences of a specified string value with another
string value.
Usage:
SELECT REPLACE('abcdefghicde','cde','xxx');

You can use REPLACE function in your SQL query:
replace( string1, string_to_replace, [ replacement_string ] )

select REPLACE('600/4768/4','/','')
While user enters the data take it and then replace the "/" and then store in the table

While inserting client data in to column you can use the REPLACE function, I believe, you are using SQLServer. Below is the example how you can use replace.
REPLACE('600/4768/4','/','');

USe REPLACE if you want to update in SQL
like
REPLACE(<column_Name>,'/',' ')
whole query will look like
Update <table_name>
set <column_Name> = REPLACE(<column_Name>,'/','')
hope this helps

Related

Create a query to ignore case? VB.net

I'm looking for a way to create a query through a winform application to query an Oracle database but ignoring the case of the data. Is this possible to do without having to modify anything in Oracle itself?
You could simply upper- or lowercase all:
SELECT Columns From Table WHERE UPPER(ColName) = :UpperValue
Then use ToUpper on the value:
yourOracleCommand.Parameters.AddWithValue("UpperValue", value.ToUpper())

How to modify SQL SELECT results

I need to write a SQL function that will allow me to strip an email address to the bare domain name. EX: I would make JoeSchmoe#mail.google.com read as JoeSchmoe#google.com. This is most likely very simple but, I cannot seem to find any information on it.
If you are using Oracle:
select substr('test#test.com',instr('test#test.com','#')+1,length('test#test.com')) as domain from dual;
You might want to try creating a CLR SQL function and use regex from your .net library to parse the string. From the looks of it you may have to parse the ccTLD if it exists and then parse the generic TLD/domain name.

Sophisticated JPQL String Query

I am trying to execute a pretty-sophisticated query on a string field in the database. I am not very experienced at JPQL, so I thought I would try to get some help.
I have a field in the database called FILE_PATH. Within the FILE_PATH field, there will be values such as:
'C:\temp\files\filename.txt'
'file:\\\C:\testing\testfolder\innerfolder\filename2.txt'
I need to be able to do a search from a user-given query on the file name only. So, instead of simply doing a SELECT Table FROM Table AS t WHERE t.filePath LIKE '%:query%', things will have to get a bit more complicated to accomodate for just the filename portion of the path. The file path and file name are dynamic data, so I can't just hard-code a prefix string in there. This has me pretty confused, but I know there are some string expressions in JPQL that might be able to handle this requirement.
Basically, I just need to return all rows that match the given query on whatever comes after the last '\' in the FILE_PATH field. Is this possible?
Thanks for the help.
EDIT: Database that is being used is SQL Server.
Probably the best solution is to add a separate column that contains just the file name. If you can't, then this might work (depending on the database you use):
drop table test;
create table test(name varchar(255));
insert into test values('C:\temp\name2\filename.txt');
insert into test values('file:\\\C:\\innerfolder\filename2.txt');
select * from test
where substring(name, locate('\', name, -1)) like '%name2%'
This is pure SQL, but as far as I understand all the functions are supported within JPQL: http://www.datanucleus.org/products/accessplatform/jpa/jpql_functions.html
One problem is the locate(,,-1). It means 'start from the end of the string'. It works for the H2 database, but not MySQL and Apache Derby. It might work for Oracle, SQL Server (I didn't test it). For some databases may need to replace '\' with '\\' (MySQL, PostgreSQL; not sure if Hibernate does that for you).
Final WHERE Clause:
LOWER(SUBSTRING(fs.filePath, LENGTH(fs.filePath) - (LOCATE('\\', REVERSE(fs.filePath)) - 2), (LOCATE('\\', REVERSE(fs.filePath)) - 1))) LIKE '%:query%'
NOTE: For performance, you might want to save the location of the slash.
Thanks to Thomas Mueller for the assistance.

How to update database when using wild characters for example like

I have column which consists of path for example \\Abc\F\E\record_123, now i need to update the path in database where only one change i need to do is this:
\\Abc\F\E\record_123 = \\Abc\F\G\record_123 - i want to update E to G .
how to do that with update query in database?
REPLACE('\Abc\F\E\record_123', '\E\', '\G\')
You have clear delimiters that allows you to pick out the bits you want to replace
See SQL Server string functions on MSDN. REPLACE() would be a good candidate.

writing sql queries

I am trying to write an sql query and I am having a problem. When we want to write a query with a where clause to narrow down our results, we can do
... where name = 'John'
(Where name is a column in the table).
Now I am trying to insert a clause like this except the name is "O'Malley". So I thought the query would be
... where name = 'O'Malley'
but this gives me a null pointer exception.
Does anyone know how you could solve this problem?
Thanks for your help in advance.
Your problem is that the single quote in the string "O'Malley" is interpreted by SQL as the string terminator. To escape a single quote, replace it with two single quotes, like this:
where name = 'O''Malley'
Edit: If the string "O'Malley" came from a user input, your code is vulnerable to an SQL injection exploit. To avoid this risk, use a parameterized query.
Use bind variables to avoid thinking about quotation problems.
Bind variables beware of sql injection to.
Depending on the database you could escape the ' I think. Have a look at http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html
If you use two apostrophes together in you search string SQL will realise that it is part of the string and isn't part of the SQL syntax.
where name = 'O''Malley'