SQL replacing a field with specific text - sql

I have the following fields for my sql view:
name | description | date
where I have to query for the max and min dates and print the new view as
status | name | description | starting
where in the status field is a text field will show longest time and shortest time. This field is completely new, and isn't built into any of the tables, how would I go in creating this field in the view?
EDIT:
I want to add the extra field status to my view, so make an extra column. As such, I have
create or replace view one (name, description, starting) as
-- SQL STUFF HERE...
from this view, I need to grab the max and min from it and union those two selections together, but add an extra column which describes whether that row has the longest time or the shortest time. To get
create or replace view two (status, name, description, starting) as
at the moment I've written up
select name, longname, max(starting) from one
union
select name, longname, min(starting) from one;
and this prints out the three columns, but I need to add the extra fourth column status but I don't know how to do this.

Just use a simple string:
select 'longest time' AS status, name, longname, max(starting) from one
union
select 'shortest time' AS status, name, longname, min(starting) from one;

Not sure what you are wanting Starting to show as you don't describe it but group by is what you are looking for and you can run aggregating functions over your date to get the information you desire. You may want a different date format so you might want to use Convert rather than cast to get the dates out in the format you want.
SELECT
CAST(MAX(Date) AS NVARCHAR(20)) + ' ,'
+ CAST(MIN(Date) AS NVARCHAR(20)) AS Status
Name
Description
FROM tblMyTables
GROUP BY
Name,
Description

Related

How to replace date value in SQL?

I am using Oracle and trying to create a view in which I will replace one date value with another date value. I am trying to use case when statement, however, I need to use the same column name as an alias and I get the error that I have duplicate column names. Do you know how to fix it or propose the alternative for replacing date value in a column and creating view? I need to keep the original table unchanged.
Code:
create view name_of_view as
select t.*,
(case when birth_date = to_date('4.12.2015', 'DD.MM.YYYY')
then to_date('4.12.1950', 'DD.MM.YYYY')
else birth_date end) as birth_date
from table t;
As #Lukasz Szozda has suggested in the comments, when you try t.* it will retrieve all columns from your table, including birth_date.
So when you add another birth_date as part of your case when, you receive the duplicate column name error.
What you need to do is:
You either change the case when column name to something like: birth_date_new or whatever then you will have both of the columns.
You retrieve all columns by their names and when retrieving birth_date you apply case when.

Oracle GroupBy of worlds from text field

I have a table of
Full Name - varchar2(200)
Age - Number
can I get the count of name for example that inside the textfield
and I dont mean to Select Count(*) from table;
not sure about the requirements but I guess this is what you want
select count(*), name from table group by name;
What is the "textfield"? A bind variable? A value in a column in a different table? The standard solution is
select count(*) from <your_table_name> where <textfield> like '%' || "First Name" || '%' ;
I used double-quotes around First Name - that is the only way column names in Oracle may contain spaces (but I hope you didn't actually do that, it is a very poor practice). Also, if there is the risk that "Jackson" appears in the textfield and you don't want that to count as "Jack" (first name), then replace the last '%' with ' %' (single-quote, SPACE, %, single-quote).
This will give the total number of names from your table that are present in the text field. It will not count duplicates (if John appears five times, it will still be only counted once). If this is not your requirement, please state your requirement more clearly. For example, you may instead want to show how many times each of the names in your table appears in the textfield... or any number of other possible interpretations. What do you really need?

Ignore a SELECT LIKE statement if an identical one has already been satisfied.

I have a table with 4 entries.
CREATE TABLE tab(
name Text
);
INSERT INTO "tab" VALUES('Intertek');
INSERT INTO "tab" VALUES('Pntertek');
INSERT INTO "tab" VALUES('Ontertek');
INSERT INTO "tab" VALUES('ZTPay');
Pntertek & Ontertek are fuzzy duplicates of the correctly spelt Intertek. I wish to create a list consisting of fuzzy duplicates and the correctly spelt names.
As I have 4 names, I have 4 search criteria:
SELECT name FROM tab WHERE name LIKE '%ntertek'
AND (SELECT COUNT(*) FROM tab WHERE name LIKE '%ntertek') >1;
SELECT name FROM tab WHERE name LIKE '%ntertek'
AND (SELECT COUNT(*) FROM tab WHERE name LIKE '%ntertek') >1;
SELECT name FROM tab WHERE name LIKE '%ntertek'
AND (SELECT COUNT(*) FROM tab WHERE name LIKE '%ntertek') >1;
SELECT name FROM tab WHERE name LIKE '%TPay'
AND (SELECT COUNT(*) FROM tab WHERE name LIKE '%TPay') >1;
This produces 3 lists containing the same information. I would like to ignore the 2nd and 3rd identical SELECT statements if the first one returns a result. Is this possible using SQLite and how would I do this?
I'm very much a beginner when it comes to sqlite and programming in general so any help would be greatly appreciated.
Thanks in advance.
What do you want the query to return? Just potential duplicates? If so you could do the above with one query by including a having statement. However, the method that you are using at the moment only allows for differences at the start of the name. I would suggest looking into something like an edit-distance algorithm (sometimes referred to as Levenshtein distance) to identify the number of characters you would need to change on one field to make it the same as another.
There are details of a possible SQLite implementation in the following link: http://www.sqlite.org/spellfix1.html

Extract all except part of the END of the string using PostgreSQL

I have records in a Postgres database table in which I have extraneous data in a particular column. I want to select everything except that piece of text from that column. That piece of text is at the end of the data in the column. If that text appears before or in the middle of the product_name, it should not be removed.
How can I compose the select statement? Below is the data and I want to remove the word 'test2' from the result set.
id|product_name |owner
---------------------
12|sissors test2 |23
13|Sample test2 |43
14|test2scoop test2 |43
Something like following should work:
SELECT id, replace(product_name, 'test3', '') AS product_name, owner FROM ...
What does the PostgreSQL manual section on string functions suggest to you?
regexp_replace(string text, pattern text, replacement text [, flags text])
Replace substring(s) matching a POSIX regular expression. See Section 9.7.3 for more information.
regexp_replace('Thomas', '.[mN]a.', 'M')
Hence:
SELECT id, regex_replace(product_name, 'test3', '') AS product_name, owner
FROM data -- since the table is anonymous in the question
And that's the complicated one — there's also replace for straight-text mapping (a little further through the list of functions on the manual page), which would suffice for the task on hand.
SELECT id, replace(product_name, 'test3', '') AS product_name, owner
FROM data -- since the table is anonymous in the question
I'm just guessing about what you want, but perhaps this will do:
select id
, replace(product_name,' test2,'') as product_name
, owner
from my_table
I'm also guessing that you meant "test2" instead of "test3".
Also note I'm showing a leading blank in the search string. That's based on the example data provided.

SQL : Varchar add space to the end of the text

When I add element to column (varchar) I get extra space. For example if I have a table Student with name varchar(10) and I do:
INSERT INTO Student (id,name) VALUES (1,'student')
SELECT name FROM Student WHERE id=1
I get student[space][space][space].
How can I fix without changing the type to text?
Most databases output results from a query in a fixed tabular format. To see where a string really begins and ends, you need to concatenate another character. Here are three ways, depending on the database:
select '"'+name+'"'
select '"'||name||'"'
select concat('"', name, '"')
You'll probably find that the spaces are an artifact of the query tool.