An issue related to changing varchar to datetime in SQL - 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')

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

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]

SQL Procedure needs to fill in extra columns

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.

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

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