I just recently started learning about SQL in MS Access and SQL Server so I have very limited knowledge, but what I'm looking for is help with a query in MS Access.
I know how to merge 2 columns into 1 and have the final result separated by a comma or whatever symbol I'd like. But, how do I do the opposite?
In my case, I have a column (LastFirstName) in my table (MEMBERS) where the data would look something like this: "Smith, Middle John" etc.
What I'm having trouble with is figuring out how to permanently separate the data into 2 separate columns within the same table (LastName and FirstName) and not just using a query to display them like that.
Any help would be greatly appreciated, thanks!
Starting with
memberID LastFirstName LastName FirstName
-------- ------------- -------- ---------
1 Doe, John
the query
UPDATE Members SET
LastName = Trim(Left(LastFirstName, InStr(LastFirstName, ",") - 1)),
FirstName = Trim(Mid(LastFirstName, InStr(LastFirstName, ",") + 1))
will result in
memberID LastFirstName LastName FirstName
-------- ------------- -------- ---------
1 Doe, John Doe John
Related
Let's say I have a column in my users table called name:
name
----
Brian
Jill Johnson
Sarah
Steven Smith
I want to write a PostgreSQL query to fetch just the first name. This is what I have so far:
select substr(name, 0, position(' ' IN name) | length(name) + 1) as first_name from users;
name
----
Brian
Jill
Sarah
Steven
This appears to work but I feel like there much be an easier way.
Although I don't follow the logic, your code looks like Postgres. If so, use split_part():
select split_part(name, ' ', 1)
from users;
I need to do masking of my data in order to hide it from the target audience.
Please find below existing table that I have right now. There are 3 columns in that table.
SNO FIRSTNAME LASTNAME
1 JAYVADAN DARJI
2 KAPIL CHAUDHARY
In the desired output, I want 4 columns with masking done as below. MASKED_NAME column would be an addition column the desired output.
The desired output that I want
SNO FIRSTNAME LASTNAME MASKED_NAME
1 JAYVADAN DARJI JXXXXXXN DXXXI
2 KAPIL CHAUDHARY KXXXL CXXXXXXXY
Thanks a lot in advance.
I am trying to write SQL query to get records that have different values between the customer and the external columns.
login customer external
-------- ---------- --------
william will200 will201
haymen hay100 hay100
norman nor345 nor346
bernie ber23 ber23
william1 will100 will101
max max65 max65
norman1 nor789 nor790
Output should be
login customer external
-------- -------- --------
william will200 will201
william1 will100 will101
norman nor345 nor346
norman1 nor789 nor790
I tried different queries but couldn't retrieve the desired output.
Any suggestions?
Thanks
I think your question is: "get records that DO NOT have duplicate values on 2 columns". This is based on your result set and sample data.
If so:
select t.*
from t
where login <> customer and customer <> external and login <> external;
At the very least, this returns the rows for your desired results.
I would like to UPDATE my table to replace / insert in the INITIALS column, the first letter in the first name column
Table name: Mano
Title Firstname Lastname Telephone Initial Gender More columns
1 Mr Adam Smith 001
2 Mrs Angela Evans 002 AE
3 Mr Bill Towny 003
4 Miss Dame Beaut 004
I am interested in transforming it as per below
Title Firstname Lastname Telephone Inital Gender More columns
1 Mr Adam Smith 001 A
2 Mrs Angela Evans 002 A
3 Mr Bill Towny 003 B
4 Miss Dame Beaut 004 D
Many thanks
This seems like a simple update:
update t
set initials = left(firstname, 1);
I should point out that you don't even need a column. You can declare this as a computed column:
alter t add initials as (left(firstname, 1));
This would provide a column called initials (assuming that name is not already used) that always has the first letter of the firstname column -- without an update.
You can use substring function,
SELECT SUBSTR(Firstname, 1, 1) from mango;
Hope using this select statement you can update the table.
If you need the update query, let me know will give you
Left is the best and would more better if we use this with TRIM to prevent the extra spaces if any:
UPDATE TABLE table1 SET Initial = LEFT(LTRIM(Firstname), 1)
Add a computed column, will never be inconsistent:
alter table Mano add Initial as SUBSTR(Firstname, 1, 1)
It's a bit of a long and convoluted story why I need to do this, but I will be getting a query string which I will then be executing with this code
EXECUTE sp_ExecuteSQL
I need to set the aliases of all the columns to "value". There could be a variable number of columns in the queries that are being passed in, and they could be all sorts of data types, for example
SELECT
Company, AddressNo, Address1, Town, County, Postcode
FROM Customers
SELECT
OrderNo, OrderType, CustomerNo, DeliveryNo, OrderDate
FROM Orders
Is this possible and relatively simple to do, or will I need to get the aliases included in the SQL queries (it would be easier not to do this, if it can be avoided and done when we process the query)
---Edit---
As an example, the output from the first query would be
Company AddressNo Address1 Town County Postcode
--------- --------- ------------ ------ -------- --------
Dave Inc 12345 1 Main Road Harlow Essex HA1 1AA
AA Tyres 12234 5 Main Road Epping Essex EP1 1PP
I want it to be
value value value value value value
--------- --------- ------------ ------ -------- --------
Dave Inc 12345 1 Main Road Harlow Essex HA1 1AA
AA Tyres 12234 5 Main Road Epping Essex EP1 1PP
So each of the column has an alias of "value"
I could do this with
SELECT
Company AS 'value', AddressNo AS 'value', Address1 AS 'value', Town AS 'value', County AS 'value', Postcode AS 'value'
FROM Customers
but it would be better (it would save additional complexity in other steps in the process chain) if we didn't have to manually alias each column in the SQL we're feeding in to this section of the process.
Regarding the XY problem, this is a tiny section in a very large process chain, it would take pages to explain the whole process in detail - in essence, we're taking code out of our database triggers and putting it into a dynamic procedure; then we will have frontends that users will access to "edit" the SQL statements that are called by the triggers and these will then dynamically feed the results out into other systems. It works if we manually alias the SQL going in, but it would be neater if there was a way we could feed clean SQL into the process and then apply the aliases when the SQL is processed - it would keep us DRY, to start with.
I do not understand at all what you are trying to accomplish, but I believe the answer is no: there is no built-in way how to globally predefine or override column aliases for ad hoc queries. You will need to code it yourself.