Populate column using single cell value from a different column with SQL - sql

I am trying to populate an entire date column in an Access database with a single date value for when the data was collected. The date value is contained in each table, but I want a separate field containing the date in each row. When I used this code it only populated the one row in my new field (sampling_date) where the date is located. The date is located in the sampling_info field on row 4
UPDATE table SET [sampling_date]=[sampling_info] WHERE [point]=4;
Thanks for any help,
Paul

It sounds to me like this is what you're after
UPDATE [table] SET sampling_date = DLookup("sampling_info","table","point=4")

Related

SQL: Update values in column based on another

I have an SQL table, 1 column has stuff written in this way 'AAAA-BB-CC', and the second one has values written in the following format 'XXXX YYYYYY'
How can I make a SQL query that would make the 2nd column 'AAAA-BB-CC YYYYYY'. Basically I want to take the value in column 1 + the 2nd part of the value in column 2 and update column 2 to have this new value

Copying data from one column to another in the same table sets data to null in original column

I created a new column [LastLoginDate-NoTime] with the data type Date. I already have another column [LastLoginDate] that is of Datetime datatype.
Columns with the values
I am trying to copy values from the LastLoginDate column to the LastLoginDate-NoTime column using this query:
UPDATE [dbo].[SapUsersExt]
SET [LastLoginDate] = [LastLoginDate-NoTime]
But the problem I am having is that when I execute this query, it sets the data to null in the original column.
Screenshot: Error
I am also trying to convert the data from the LastLoginDate to just date format in the new column LastLoginDate-NoTime so that I can use it in my application. How would I do that?
I am trying to copy values from the LastLoginDate column to the LastLoginDate-NoTime column using this query
In that case, you're doing it exactly backwards - you should use this SQL instead:
UPDATE [dbo].[SapUsersExt]
SET [LastLoginDate-NoTime] = [LastLoginDate]
The first column - right after the SET - is the target column into which your values will be written.
The second column, after the = symbol, is where the data comes from (column or expression).
You had it backwards - setting the column with the actual values, to all NULL ....
This of course only works for a "one time" update - this will not keep your columns in sync over time, when new data is being inserted. For such a case, you'd need a computed column
ALTER TABLE dbo.SapUsersExt
ADD LastLoginDateOnly AS CAST(LastLoginDate AS DATE) PERSISTED;
or a trigger.
Or maybe, you don't even really need to actually store that date-only value - just use
SELECT
CAST(LastLoginDate AS DATE),
.......
if you need to date-only value from LastLoginDate

An issue related to changing varchar to datetime in SQL

I have a question related to data type. Now I'm trying to import a flat file into my destination database.
I import the flat file into one table A inside my staging database. All the columns in table A are in varchar(50) data type.
I wrote a SQL query to change the data type and clean the data in table A, and finally insert the clean data into table B inside the destination database.
Here is the question: there is one column in this file containing date data. It is in varchar(50) data type in table A. But it also contains empty rows. So in table A it looks fine: some rows are date and some rows are empty. However, after I run the SQL query. In table B, all the empty rows in table A are changed into 1900-01-01 00:00:00.000. Please note I set this column in table B to be datetime data type.
Now I want the rows with date in this column to show date and the empty rows to be empty in the destination database. I don't want the empty rows to be 1900-01-01 00:00:00.000. How can I modify
my SQL code to finish my goal?
Presumably, you can do something like this:
nullif(<expression to convert col to date/time>, '1900-01-01 00:00:00.000')

Copy Contents of One Column Of a Table To another of a different Table SQL

I want to copy the content of one column in table A and replace the contents (not insert into it - the number of rows will be the same) of another column in another table.
I can't a where condition, the table has only just been created at this point with one empty timestamp column. it will be populated via pyodbc class after the timestamps have been added - this query will fill the timestamps for me
What is the SQL command for this?
Thanks!
After discussion, this is the query needed : INSERT INTO OCAT_test_table (DateTimeStamp) SELECT DateTimeStamp FROM DunbarGen

Get values based on newly inserted value using SQL

I want to make filtration on a column after selecting a specific value of another column in the same table, I tried to use #... special character followed by the column's name to get the address of this value.
My SQL statement is like the following :
SELECT ATTRIBUTE FROM TABLE WHERE FIELD = '#FIELDNAME';
If I used a specific value instead of #FIELDNAME, it will work properly but it will be static but I need it to be dynamic based on the selected value.
Create another table which will have the list of values that are in the FIELDNAME and give each record a unique id ,then retrieve the value depending on what you have selected by the name of the new table's field preceded by '#...'
I don't know if that what are you looking for, please let me know.
If no triggers are allowed, do you have any date/time column in the table? Is it possible to have that extra column anyway to see the time of a newly inserted row?
You may have to check the lastest row entered, save its field value into a variable. Then do the select based on the variable value.
Based on the vague last row id you could try the following (it's not pretty). But again, if you have date/time that's more accurate.
select attribute from table
where field = (select field from table
where rowid =(select max(rowid) from table))
;
upate
Do you have the priviledge to set up your insert command as below:
insert into table (id, col1, col2,...) values (1,'something', 'something',...)
returning id into variable; -- you may either save field or id depending on your table
Then you may use this variable to select the records you want.