Inserting value to all rows in one column - sql-server

I am using VB.net 2013 and SQL Server 2012. I have a table tblEmployeeInfo with two columns, EmployeeName and Date:
EmployeeName Date
--------------------
Jay Null
Mike Null
Paul Null
When I input a date value in Textbox1 like 3/20/2017, it would insert that value into all rows into column Date.
EmployeeName Date
-------------------------
Jay 3/20/2017
Mike 3/20/2017
Paul 3/20/2017
Please anyone help me out. Still no idea how to code using VB.net. My idea just to insert only using WHERE clause. But how to insert at once? Thank you guys.

A simple approach
DECLARE #NewDate DATETIME=GETDATE();
UPDATE tblEmployeeInfo SET [Date]=#NewDate;
Will set all rows to the same value. Is it really this you are trying to achieve?
If you want to hit only rows, where this value is NULL you can add
UPDATE tblEmployeeInfo SET [Date]=#NewDate WHERE [Date] IS NULL;
You - quite probably - have there some kind of grouping field where you want to set the new value selectively...? In this case just add an appropriate WHERE clause.

Looks like you need an update statement:
update tblEmployeeInfo set Date = #myDate
And add the #myDate as a parameter when calling the query.

Related

Is it possible to start over auto-increment each day, with composite keys: date + int? (MSSQL)

Lets say i have a small db table with only two fields. (MSSQL) Like this:
date (Date) daily_counter (Int)
-------------------------
2021-07-18 0
2021-07-18 1
2021-07-18 2
2021-07-19 0
I want to insert a new fifth row and insert value "2021-07-19" to the date field. And i want to know what the daily_counter is for my new row.
As you perhaps understand by the example, the daily_counter is supposed to auto increase, starting over each day.
So, since there is already a row with that date with the value 0 on the field daily_counter. I want to add 1 to daily_counter for my new row without sending the value 1 to the query.
How can i think when designing this type of table and data. Any hints appreciated. Thanks!
Kr
Gustav
UPDATE:
Ok, i think i got something that could work. The only downside would be when deleting and adding new rows, as the new id could be previosly used and deleted and added again.
A side from that i think i got something that i can use.
It might not be pretty now, but it looks like this.
It seems to work also when there is no row for the current day.
DECLARE #date DATE
SET #date = '2021-07-22'
DECLARE #daily_counter INT
SET #daily_counter = (SELECT MAX(daily_counter) from mytable where date = #date);
SET #daily_counter = #daily_counter + 1;
IF #daily_counter IS NULL
BEGIN
SET #daily_counter = 1;
END
INSERT INTO
mytable
(date, daily_counter)
OUTPUT #daily_counter
VALUES (#date, #daily_counter)
Thanks again for the help!
It's not possible to make the database do this automatically in the row itself. You must have a single counter across all dates (a SEQUENCE would be good for this).
What you can do is use the row_number() function to simulate this at the point where you query the data:
SELECT *, row_number() over (partition by [date] order by [date])
FROM ...
Unfortunately, this will still fail if you need to preserve the original position following deletes, but there's not a good way to do this right now in a database without triggers or application code.

How to get date time from date in SQL Server

I have a table having column order date like this 2019-06-01.
But I need to show date and time also like this 2019-06-01 00:00:00.000
Please suggest
You should be able to do a simple CAST() assuming your type is a date like you mentioned. It is stored as a date, but you want it to be presented as a datetime.
SELECT CAST(your_col AS DATETIME) AS your_col
FROM your_table
There are a few ways to do it. This should be pretty straight forward though:
DECLARE #TEST DATE
SET #TEST = '2019-06-01'
SELECT CONVERT(DATETIME, #TEST)
yields:
2019-06-01 00:00:00.000
Really doing what you are looking for, making your datatype of DATE into DATETIME
However as suggested in the comments depending on how this data is used you might just want to append stuff to it on the presentation layer. Really up to you! Good luck. :)

Insert query result into table - SQL Server Management Studio

This code works great:
select month(period)
from fmdr3.dbo.fmdr_2017
It returns the number of the month from a date column.
I'm trying to figure out how to insert the results into the table FMDR_2017 in a column called Month.
I've been trying permutations of this:
insert into fmdr3.dbo.fmdr_2017(Month)
select month(period)
from fmdr3.dbo.fmdr_2017
but haven't had any success yet.
Thank you.
I'm guessing you'll want to update the Month column based on the period?
UPDATE fmdr3.dbo.fmdr_2017
SET Month = Month(Period);
It sounds like you're trying to update a value in existing records, is that right?
If so, you need something like this:
UPDATE fmdr3.dbo.fmdr_2017
SET [Month] = month(period)
see http://sqlfiddle.com/#!6/7c403/1

Date range based on Column Date

I am using the latest SQL Server. I have a table with a CreatedDate column. I need to write a Query that uses dates that are plus or minus 7 from the Date in CreatedDate. I have no clue how to go about this. My thought was this:
DECLARE #Date datetime
DECLARE #SevenBefore datetime
DECLARE #SevenAfter datetime
SET #Date = CreatedDate
SET #SevenBefore = DATEADD(day,-7,#Date)
SET #SevenAfter = DATEADD(day,7,#Date)
SELECT *
FROM <table>
WHERE <table> BETWEEN #SevenBefore AND #SevenAfter
The issue with this is that I cannot use "CreatedDate" as a SET #DATE because SQL gives an error "Invalid column name 'CreatedDate'"
Any help would be appreciated. I cannot list a date because every date in that column could be different.
Thanks
In this case, you need to stop thinking as a programmer would, and start thinking as a Database programmer would.
Lets work only with this central part of your query:
SELECT *
FROM <table>
WHERE <table> BETWEEN #SevenBefore AND #SevenAfter
Now, you say that the CreatedDate is a column in a table. For this example, I will assume that the CreatedDate is in a table other than the one in your example above. For this purpose, I will give two fake names to the tables. The table with the CreatedDate, I will call tblCreated, and the one from the query above I will call tblData.
Looking above, it's pretty obvious that you can't compare an entire table row to a date. There must be a field in that table that contains a date/time value. I will call this column TargetDate.
Given these assumptions, your query would look something like:
SELECT *
FROM tblCreated tc
INNER JOIN tblData td
ON td.TargetDate BETWEEN DATEADD(day, -7, tc.CreatedDate) and DATEADD(day, 7, tc.CreatedDate)
Looking at this, it is clear that you still need some other associations between the tables. Do you only want all data rows per customer based on the Created date, or perhaps only want Creations where some work was done on them as shown in the Data records, or ??. Without a fuller specification, we can't help with that, though.

insert null date interbase

I need to insert a null or '00/00/00' date value.
Is it possible to do it on interbase?
I already tried to insert '00/00/00' and it wasn't possible.
According to the literature (I have zero experience with InterBase) you can use standard SQL
see section 6-56 "NULLing columns with UPDATE"
UPDATE TABLENAME
SET DATEVALUE = NULL
WHERE SOME_ID = 123
you might have more options depending on your client

Resources