Oracle SQL: write manually a text - sql

I am currently trying to just get a certain format of a bill, but I don't know how to select a custom test.
For Example, I Select something like this
Name Age Birthday Gender
Jonas 26 01.01.2000 Boy
The Boy in this one is no Information that is in a table but manually made and I don't know how to do that. Every other Information is from a Table (that was easy).
Summary: How do I manually write a text into a Select?

select name, age, birthday, 'Boy' as gender
from your_table ---------------
like this!

Related

Dynamic query based on user selection

How can I generate a dynamic query based on user selection of data. I.E.
Table 1 has basic personnel data: name, social sec num... table 2 has address info: addr, city, state, zip
and table three has a work history of contracts that have been worked.
The user has choice of data to return:
Name, Address, Current contract, current name of place contracted, etc...
I have presented them with the list of options and I am storing in the database where the information is - tableName and fieldName ...
But I am not sure how to compile the data into a query dynamically. I get dynamic sql but this is a question of structure and retrieval.
Anyone done something like this or no a place to look?
If you need to feed this to a report all the time, I would suggest returning all the data to your report regardless, and then dynamically hiding the columns you don't need in the front-end application. That is generally easier.
If you absolutely have to do it from the SQL side, you could do something like:
SELECT
CASE WHEN #paramDisplayName = 1 THEN Name ELSE NULL END AS Name,
CASE WHEN #paramDisplayAddress = 1 THEN Address ELSE NULL END AS Address
...
...
...
FROM
Wherever
This would still return the column, though it would be empty.

Select both uppercase and lowercase from column

I was wondering how I could get both uppercase and lowercase from a sql query.
I've got lets say these values in my database.
John
Marco
jason
nico
So I would like to get both John and jason as a result.
At the moment I am searching with:
$pdo->prepare("SELECT name FROM users WHERE name LIKE 'J%'");
However this only gives me John.
SELECT name FROM users WHERE UPPER(name) LIKE 'J%'
Make sure your user table's name column isn't set to use case sensitivity. http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

Hora Keep tool change column name

I'm using Hora Keep Tool for SQL queries. When I use operations for example like
avg(age) to build the average value of a few values for the "group by" functionality, than it changes during the query in the output the original column name from "age" to "avg(age)".
I know that a string after the operation for example:
avg(age) age
would rename the column name, but I have a lot of rows, which I have to change than like this.
Is there a function which would not allow that during the query the column name would change from age to avg(age) ?
I would be happy if somenone could give me helpfully tips.
Thank you forward.
You can use ' as ' keyword in sql to name a column e.g
select user_name as username from users;
this query will result the column name as username.
---------------
USERNAME
---------------
hello
hello1
hello3

SQL replacing a field with specific text

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

Oracle - Select where field has lowercase characters

I have a table, users, in an Oracle 9.2.0.6 database. Two of the fields are varchar - last_name and first_name.
When rows are inserted into this table, the first name and last name fields are supposed to be in all upper case, but somehow some values in these two fields are mixed case.
I want to run a query that will show me all of the rows in the table that have first or last names with lowercase characters in it.
I searched the net and found REGEXP_LIKE, but that must be for newer versions of oracle - it doesn't seem to work for me.
Another thing I tried was to translate "abcde...z" to "$$$$$...$" and then search for a '$' in my field, but there has to be a better way?
Thanks in advance!
How about this:
select id, first, last from mytable
where first != upper(first) or last != upper(last);
I think BQ's SQL and Justin's second SQL will work, because in this scenario:
first_name last_name
---------- ---------
bob johnson
Bob Johnson
BOB JOHNSON
I want my query to return the first 2 rows.
I just want to make sure that this will be an efficient query though - my table has 500 million rows in it.
When you say upper(first_name) != first_name, is "first_name" always pertaining to the current row that oracle is looking at? I was afraid to use this method at first because I was afraid I would end up joining this table to itself, but they way you both wrote the SQL it appears that the equality check is only operating on a row-by-row basis, which would work for me.
If you are looking for Oracle 10g or higher you can use the below example. Consider that you need to find out the rows where the any of the letter in a column is lowercase.
Column1
.......
MISS
miss
MiSS
In the above example, if you need to find the values miss and MiSS, then you could use the below query
SELECT * FROM YOU_TABLE WHERE REGEXP_LIKE(COLUMN1,'[a-z]');
Try this:
SELECT * FROM YOU_TABLE WHERE REGEXP_LIKE(COLUMN1,'[a-z]','c'); => Miss, miss lower text
SELECT * FROM YOU_TABLE WHERE REGEXP_LIKE(COLUMN1,'[A-Z]','c'); => Miss, MISS upper text
SELECT *
FROM mytable
WHERE FIRST_NAME IN (SELECT FIRST_NAME
FROM MY_TABLE
MINUS
SELECT UPPER(FIRST_NAME)
FROM MY_TABLE )
for SQL server where the DB collation setting is Case insensitive use the following:
SELECT * FROM tbl_user WHERE LEFT(username,1) COLLATE Latin1_General_CS_AI <> UPPER(LEFT(username,1))