How to get some value from the sql database based on certain criteria - sql

Suppose I have given such a table in sql
According to Gmail (which I have given in advance) I want to get the Username value.
For example, if "Gmail" would be "Gela1#gmail.com" I should get string - "Gela gela".
if "Gmail" would be "mamuka#gmail.com" I should get string - "Mamuka snaia".
How to do this?

You can easily make an SQL query like:
SELECT Username from tableName where Gmail = 'Gela1#gmail.com'
Replace tableName with your table name.
https://www.w3schools.com/sql/sql_select.asp

Related

Select statement in SQL Server returns null while fetching values using email in where; unable to fetch data using values with dots using select

I'm trying to get password from email address but the problem is that the selection query won't read the the values after dots in the emails column and returns null results. I've tried PARSENAME() and RIGHT() functions to get it read the full email address. But no luck there. I've put my query statement below.
select Password
from Faculty
where Email = 'john.carson#outlook.com'
I'm not getting the password that belongs to that email address, instead all I get is null. The datatype is both email and password fields are nvarchar(100). And there are 5 records with different emails one of which is this. I'm sure the problem's rising because of the dots, because I tried using dummy data in email as like johncarson#com and I got the password respective to that. I'm using SQL Server as my database engine.
I Hope, It will be work
select Password
from Faculty
where Email like 'john.carson#outlook.com'
I checked with dummy data but it's working please check with Like clause may be any space is exists in Email field.
Declare #Faculty Table (Email Varchar(300),Password varchar(20))
Insert into #Faculty VALUES ('john.carson#outlook.com','123'),('john1.carson#outlook.com','12335'),('john2.carson#outlook.com','12356')
select Password
from #Faculty
where Email = 'john.carson#outlook.com'

Update a specific column with a specific value

I have a table with 3 columns: username, password and ID.Every password and username have a specific ID. Table name is "Account" for example.
I want to update a password with a specific ID. I have tried:
UPDATE Account SET password = "newPassword"
where id = 1
However, it does not work. It will complain that "newPassword" is not a valid column name. I try my queries in SQL management studio.
Use single quotes for string literals in MS SQL Server:
UPDATE Account
SET password = 'newPassword'
WHERE id = 1;
Double quotes in SQL are generally reserved for database objects, such as table and column names (sometimes called identifiers). Your current update is attempting to assign the password column to a column named newPassword. This column doesn't exist, hence you are getting an error.

Why The Query Against HashKey returns no records

I am working on a new sql table. The table has a column [varbinary(8000)], where we are storing hash of a certain text. Now, I am trying to retrieve the same record back by using a where clause against the hashkey, but that yields zero records.
I have added a similar query here: http://sqlfiddle.com/#!18/be996/11
Try without the single quotes, like this
SELECT id, description
FROM ForgeRock
where id = 0x94EE059335E587E501CC4BF90613E0814F00A7B08BC7C648FD865A2AF6A22CC2
and you will get the expected result.

DB2 SQL - Set variable value based on user input

In DB2-LUW SQL, I can write a query to do this:
select * from customers
&input
When a user runs that they'll be prompted for input and can type:
where name = 'Bill'
And the query that actually gets executed will be:
select * from customers
where name = 'Bill'
I am trying to figure out a way to vary the value of &input to alter the query based on more basic user input - so they won't need to type in where name = 'Bill'.
For example, the user could be prompted to enter either YesBill or NoBill and depending on what they've entered, the value of &input will be set and the executed query would be either:
select * from customers
where name = 'Bill'
or
select * from customers
where name <> 'Bill'
The example is meaningless, I'm mostly wondering if it's possible to vary the value of &input without forcing the user to type in SQL code.
Hope that makes sense. Thanks for any help!
The answer to this question really depends on the SQL client that you are using.
For example, if you use DBeaver, then you can simply use : to prefix your variable names and then when executing the query, you will get a prompt to window to enter your variable names.
Other query tools may, nor may not, provide a similar capability

Oracle SQL Query to find the existence of characters in string

I am using oracle database and trying to find out the query which should return the result when there is a special character(',`,(,)) exist on the string.
I am trying something like this,
select username from users where username like (',`,~,(,));
I tried to achieve the same using the below query,
select username from users where (username like '%`%' OR username like '%~%');
It doesn't consider the second condition and returns the value to the first condition only.
Is there any function/methods using which this result can be fetched?
You can use regular expressions and check all special characters with one condition:
SELECT username
FROM users
WHERE regexp_instr(username,'[''`\(\)]') > 0
Old school style without regexp
where length(translate(username, '_''`~()', '_')) <> length(username)