SQL Procedure needs to fill in extra columns - sql

INSERT INTO Test EXEC p4_Proc
But my table have two extra columns a date with a getdate() datatype and another column that needs to be updated from the application.
Please explain how I can run the query to fill in the date and empty column.

Related

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')

Alter table add column with data computed from the same table

I have a table with column "datetime2_asstring". I would like to add a column "datetime2_asdatetime2" to that table.
For this I execute queries:
1) add column datetime2_asdatetime2
2) update column datetime2_asdatetime2 with values from cast(datetime_asstring TO datetime2)
I wonder if there is SQL Syntax to add a column and at the same time set computed value for every row on this column in the table, all in one query (ALTER TABLE) in SQL Server?
The code below would work for the versions that you have specified:
ALTER TABLE datetime2_asstring
ADD datetime2_asdatetime2 AS CAST(datetime_asstring AS datetime2)

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

how to insert a column with value row number in an access table

I have a large access table . I need to have a column which will have value as row number
how can I write an sql query fill an empty column inserted by me with row numbers
Write a sequence for your table . Then have default value as sequence .
So, whenever the column does not get users values , it will take the sequence values.
Im sorry, im a little confused at what you're getting at, but could you not just do an alter table?
ALTER TABLE my_table
ADD row_number int;
Make the Field DataType AutoNumber it will do it for you automatically.