3 inputs for a oracle sql query [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I Have below query:
where POSTDATE between to_date('&START_DATE','DD-MM-YYYY') and to_date('&END_DATE','DD-MM-YYYY')
and username='&username'
But my requirements are:
I need all usernames between Start date & End date(3rd Input is
blank) AND / OR
specified usernames(in 3rd Input) between provided
start date and End date in query

You want the date range check anyway, then you either want all users or a specified user; so you can check if you were given a username:
where POSTDATE between to_date('&START_DATE','DD-MM-YYYY') and to_date('&END_DATE','DD-MM-YYYY')
and ('&&username' is null or username='&&username')
If no user name is given then the second line evaluates to:
and ('' is null or username='')
In Oracle '' is equivalent to null (though they have always warned that could change one day...) so the first clause is true.
You haven't said if you're explicitly prompting for the inputs (with accept); if you are then you could use single & or double &&, but if not using double && will prevent you being asked twice.

Related

DATE BETWEEN issues [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Trying to select data only between a range.
select ITM_NBR,
TOT_IVO_ITM_QTY,
Count(*)
FROM dataset
WHERE
bus_dt BETWEEN '2-14-2020' AND '2-15-2021'
Failed conversion to numeric value. Tried without single ticks and it returned zero rows which makes me believe that the column is stored as a VARCHAR. Tried Casting the bus_dt column into a date format.
CAST(bus_dt AS DATE FORMAT 'mm/dd/yyyy') BETWEEN 2/14/2020 AND 2/15/2021
Again failed conversion.
I feel as if I’ve tried every combination and can’t get anything to work of casting and putting the date values in yyyy-mm-dd, mm/dd/yyyy, etc. formats.
And also when I HELP VIEW to see the column type I get “?”.
Kind of at a loss right now.
After I thought I tried everything I figured it out....
I was not formatting my date literals correctly .... faceplam
DATE'yyyy-mm-dd'....

Is there valid statement that don’t return a recordset? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In the question Difference between a statement and a query in SQL, the accepted answer says:
A statement is any text that the database engine recognizes as a valid command.
and
A query is a statement that returns a recordset (possibly empty).
For example, I know that update and insert can return a recorset when they use a returning clause, so I guess they return an empty returnset in the other case. Also, according to the postgres documentation, the update command returns a “command tag”. But I don’t understand if the tag is attached to an empty recordset or if it’s really all that is returned.
My question is: Considering this list of SQL commands, how do I know which one does not a return a resultset (not even an empty one) and what are they returning instead exactly?
Generally accepted version is that
Query(ies) are select statement which may or may not return rows
but does not make any changes to database.
Statement(s) are instructions when executed and successful will make
changes in the database(after commit)

SQL select Statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am on SQL Server 2012, I am trying to write a SELECT statement that uses LEFT() and RIGHT() functions to display a ProductName if the 3rd letter from the end of the product name is a particular letter.
Example the 3rd letter is an 'a'
The simplest optio here is to use like:
select ProductName from Products where ProductName like '%a__'
% is a wildcard matching any number of characters, and _ matches a single characters. like '%a__' means the third-to-last letter is an a.
Working example: http://www.sqlfiddle.com/#!6/6b479/8
If you want to use left and right, you should use:
select ProductName from Products
where len(ProductName)>=3 AND left(right(ProductName,3),1)='a'
Working example: http://www.sqlfiddle.com/#!6/6b479/2
left(right(ProductName,3),1) means: take the last three letters, and take take the first letter out of them. We also check for len(ProductName)>=3 to make sure there are at least three letters, otherwise we may return short product names that begin with an a.

How do I show ☺♦♦ characters in SQL Server? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
You can I display special Unicode characters in my result set on my SQL Server?
e.g. How can i display those characters ☺♦♦?
Well due to the fact that your question is very unspecific here are some solutions.
You need to specify a string as unicode string. This can be achieved by adding an N in front of the string. See example:
SELECT N'☺♦♦' as a, '☺♦♦' as b
Result:
a b
---- ----
☺♦♦ ???
If you want to store those symbols, you need a type as nvarchar or nchar.
Hopefully this helps you.

Fill with zero to complete a defined number in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to complete cards numbers in sql. I have the prefix =11111 and the number of the card which is variable, therefore it could be '25' or '2130' but at the end I must have 14 numbers. So I need to fill spaces with zeros.
I've read about 'LPAD' but I don't understand very well this method.
You could use lpad, but if you're starting with a number you could use a 9-digit format model instead, and concatenate that onto your prefix:
select '11111' || to_char(25, 'FM000000000') from dual;
11111000000025
The FM format modifier stops Oracle adding a space for a potential +/- sign indicator.
SQL Fiddle demo
Use the ZEROFILL attribute.
But your database should only be responsible for saving data and not changing it before saving.
The best way would be to send the zerofilled data to the database server.