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

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

Related

Finding row of value X in column A, and inserting value Y in the same row, in column B

I have two columns: Timestamp and Data. From Excel I will have a Timestamp datapoint (e.g., 2017-11-25 10:25:00) and a value related to it. Meanwhile in the database I will have the Timestamp column populated with different timestamps and I want to find the one that comes from Excel in this column, and insert the value in that same row in the database.
You really need to learn about UPDATE statement and WHERE clause for SQL but here is an example, (this is assuming you are using SQL Server, but overall the syntax should be quite similar if not using SQL Server):
UPDATE [yourTable]
SET [DATA] = [Excel value]
WHERE [TimeStamp] = [ExcelTimeStamp]

Changing Data Type to an existing Column with data already stored

Im using SQL Oracle developer.
I have a table called "Vaccine", Within Vaccine I have a column called 'Vaccine_Number' Currently Vaccine Number is a NUMBER datatype size(2). I need to change this to varchar2(10).
Is this possible without having to lose my data in vaccine number?
The following is predicated on you not having any foreign key references on Vaccine_Number:
Add a new column to the table with the datatype that you want.
Run an update statement that sets the value of the new column with the other column's value.
Update Vaccine set New_Vaccine_Number=Vaccine_Number where New_Vaccine_Number is null
Also, you may need to use the To_Char function to convert your number into a varchar.
Delete the other column.
Rename new column to Vaccine_Number

Populate column using single cell value from a different column with 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")

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.

sql server 2008 function on two date columns

I would like to create a function for a table that has three columns amongst others as follows:
insertDate datetime
updateDate datetime
activity integer
I want to update the activity column by taking the difference of the two date columns...basically updateDate - insertDate = how many days of activity in the activity column. I have no idea how to start this and it needs to run whenever a new insertDate or updateDate is inserted.
You can populate the [InsertDate] with a default value of GETDATE() and populate [UpdateDate] with the current date when you update the column (because you're using procedures (wink), this is really easy to control). If you aren't using procedures and want to control. the [UpdateDate] column, you can use a trigger to populate that column.
Let the Activity column be a calculated field:
DATEDIFF(day, [InsertDate], [UpdateDate])
DATEDIFF
Computed Columns
From MSDNabout computed columns:
Unless otherwise specified, computed columns are virtual columns that are
not physically stored in the table. Their values are recalculated every
time they are referenced in a query. The Database Engine uses the PERSISTED
keyword in the CREATE TABLE and ALTER TABLE statements to physically store
computed columns in the table. Their values are updated when any columns
that are part of their calculation change. By marking a computed column as
PERSISTED, you can create an index on a computed column that is
deterministic but not precise.
Since all the data required for this is in the same row of the table, you could create a computed column. If you want to have an actual column value that is updated whenever the row is updated then you need to look at triggers.
Place this code in a trigger.
update MyTable
set updateDate = GETDATE()
, activity = select (DATEDIFF(DAY, insertDate, GETDATE()))