I have one table with the following structure and some values :
PK_ID Webinar_Name Start_Date Is_Active
1 w1 3/1/2016 True
2 w2 1/7/2016 True
3 w3 4/9/2016 True
Now i want the Is_Active column value to be updated dynamically (i.e : NOT after update,insert and delete) based on the date.. if its matches the current date so the Is_Active column value will set to be false.
I've tried to use triggers but it should be after some action like insert and update which is conflicts with my requirements.
I appreciate any help . thanks
It looks like you should be able to use a computed column for this, though I've never tried it.
https://msdn.microsoft.com/en-GB/library/ms188300.aspx
I guess it'd look something like this:
ALTER TABLE webinars ADD ActiveFlag AS
CASE WHEN Start_Date = CAST(GetDate() AS date) THEN 'True'
ELSE 'False' END
Use a computed column:
alter table t add Is_Active as (case when cast(start_date as date) = cast(getdate() as date) then 0 else 1 end)
Of course, you might need to remove the column first.
Note: this uses 1 for true and 0 for false. These constants are not defined in SQL Server. You can use whatever values you like.
Related
I have a BIT parameter that I am hoping to set to 1 if two values exist in a column of my temporary table.
I have had a look online and most people suggest using the CONTAINS function, however that requires me to change the settings in my environment which isn't an option. Elsewhere, I've seen LIKE mentioned, but when I've tried this its been no use as my aim is to make sure both values exist in the column but since LIKE is working on a row by row basis its not working as I am hoping. Here is what I have so far:
CREATE TABLE tempTable (Description nvarchar(100))
INSERT INTO tempTable
VALUES ('Word1'), ('Word2')
DECLARE #bValuesExist BIT
IF EXISTS(SELECT Description
FROM tempTable
WHERE Description LIKE 'Word1'
AND Description LIKE 'Word2')
SET #bValuesExist = 1
SELECT #bValuesExist
http://www.sqlfiddle.com/#!18/7fbe1c/6
The results I'd hope for from the above code snippet is for the bValuesExist variable to be set to true since both values exist in the description column of tempTable. However, the code is currently checking whether the description column contains "Word1" and "Word2" on a row by row basis, how can I do this check so its on the whole column and not just the row?
Description LIKE 'Word1' AND Description LIKE 'Word2' can never be true, as a column's value (which is a scalar value) cannot be 2 different values at the same time. What you want here is a HAVING and a conditional aggregate:
IF EXISTS(SELECT 1
FROM tempTable --This isn't a temporary table.
HAVING COUNT(CASE Description WHEN 'Word1' THEN 1 END) > 0
AND COUNT(CASE Description WHEN 'Word2' THEN 1 END) > 0)
SELECT 1;
If I understand correctly, you want to check a list of values in the table. One method uses aggregation:
SET #bValuesExist = (SELECT (CASE WHEN COUNT(DISTINCT Description) = 2 THEN 1 ELSE 0 END)
FROM tempTable tt
WHERE Description in ('Word1', 'Word2')
);
The 2 is the number of words. The DISTINCT is to account for the possibility of duplicates in tempTable.
Note: This does not require an IF in the code.
I am trying to update a data type in the table I work with. It currently is stored as an int but it really is a date column.
It looks like 20191012 right now. For some reason instead of using null, they had columns with no dates be 0.
When I query the table I use
case
when bthdat = 0
then '9999-12-31'
else convert(date, convert(varchar(10), bthdat)) as dob
end
Can I use the same logic to update the actual table itself? If so, how? Thanks.
You're not going to be able to UPDATE your existing column, as int and date aren't compatible.
What you can do, however, is change the datatype a couple of times, with an UPDATE in the middle. This, however, assumes that all the values are valid (for example don't have a value like 20190229) and in the ISO format yyyyMMdd
ALTER TABLE dbo.YourTable ALTER COLUMN bthdat varchar(10);
UPDATE dbo.YourTable
SET bthdat = NULL
WHERE bthdat = '0';
ALTER TABLE dbo.YourTable ALTER COLUMN bthdat date;
You can use as follows
update
a
set
bthdat = case
when bthdat = 0 then '9999-12-31'
else convert(date,convert(varchar(10),bthdat))
end
from
YourTable as a
Do not forget to set the else for records that are not updated to keep the existing value.
I have a table called Releases with a StartDate and an EndDate, as well as a column IsExpired which is by default set to 0. Is there any possible way to set IsExpired to 1 if the current date is > EndDate automatically by the SQL Server? Or do I have to do it manually?
Change the column to a computed column:
ALTER TABLE Releases DROP COLUMN IsExpired;
GO
ALTER TABLE Releases ADD IsExpired AS CASE WHEN GETDATE() > EndDate THEN 1 ELSE 0 END;
Note that you cannot persist this column, due to the use of GETDATE(), so you won't be able to index it. As a result, if you do need to query the table is "active" rows, I would still perform the query against EndDate.
I have one table with 2 columns. One is called "houranddate" and is datetime type and the other is called "status" and is int type.
I want the value in "status" column to be updated to "1" when the the server datetime reaches the same datetime value saved int the column "houranddate".
Is there any chance that I set this in sql server MS or the only way is to use some web service?
In SQL Server, you can use a computed column:
alter table t
add new_status as (case when datetimecol >= getdate() then 1 else status end);
(Or, what I would do in this case is name the existing column something like _status and the new one status.)
If status is 0 before that time, then drop the existing status column and instead:
alter table t
add status as (case when datetimecol >= getdate() then 1 else 0 end);
The computed column is calculated when it is used. So, it is always up-to-date, without the use of jobs or update.
You could use an SQL Agent scheduled job to do this, but I would not recommend it. Better to handle such things in the select statement:
SELECT houranddate, case when houranddate > getdate() then 1 else [status] end as [status]
FROM tablename
I'd like to have two columns in a database, one for tracking whether or not the user has submitted something, and another for the timestamp of that submission.
How can I structure the table definition so that the state of these two columns is never inconsistent?
Basically, I'd like the boolean field to be driven by whether or not a SubmittedDate column is null. Here's a snippet of the table definition:
CREATE TABLE SomeSchema.SomeTable
(
...
SubmittedDate datetime NULL,
Submitted bit NOT NULL DEFAULT(0), -- Drive off of SubmittedDate?
...
)
What's the best way to accomplish this?
Thanks!
Use only one column - the DATETIME one. It serves double duty - the column being null means it wasn't submitted, but if the value exists - you also know when.
Use a computed column:
CREATE TABLE SomeSchema.SomeTable
(
...
SubmittedDate datetime NULL,
Submitted as cast(case when SubmittedDate is null then 0 else 1 end as bit)
)