in my table, I got a column where is written the company code
example
DE06 or DE07
now I need to add a new column to this table where will have as value 'YES' for DE06 and 'NO' for DE07
I try to add a condition just for De06 but I cant make it work
ALTER Table dbo.DE06_PROJECT$
add [PROJECT_COMPANY_CODE2] nvarchar(50) null
GO
UPDATE dbo.DE06_PROJECT$ SET [PROJECT_COMPANY_CODE] ='YES' where([COMPANY_CODE]='DE06')
GO
I need a new column [PROJECT_COMPANY_CODE2] with result 'YES' or 'NO' based on the value of the column [COMPANY_CODE]
I forget to mention that in the future I could need 3 possibilities 'YES' for DE06 and 'NO' for DE07 and 'MAYBE" for DE08
If you have SQL Server then you can go for CASE
UPDATE dbo.DE06_PROJECT$
SET [PROJECT_COMPANY_CODE] = CASE WHEN [COMPANY_CODE]='DE06' THEN 'YES' ELSE 'NO' END
Use a computed column to ensure data consistency:
ALTER TABLE dbo.DE06_PROJECT$
ADD [PROJECT_COMPANY_CODE] AS CASE [COMPANY_CODE] WHEN 'DE06' THEN 'YES'
WHEN 'DE07' THEN 'NO'
WHEN 'DE08' THEN 'MAYBE'
ELSE 'whatever' END
Related
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 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 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.
I'm having one table, it's containing the column name like flag, that data's are looks like 'Y' or 'N'.
When I'll take a data from that table using "Select Flag from tablename" query means it shows only 'Y' or 'N'.
But what I need is, if it's 'Y' means 'Yes' or 'N' means 'No.
If we will use Stored procedure means we will do this concept easily using 'If' or 'Case' condition.
But I want to take that data using single query.
How can I do this?
Please help me friends...
One way is with a case statement:
select (case when flag = 'Y' then 'Yes'
when flag = 'N' then 'No'
else 'Oops!'
end) as FullFlag
from table t
You can also do this with a calculated column. You could define the table and store the flag as a bit (which is as efficient as possible) and then use a computed column to retrieve another value:
create table . . .
FlagBit bit,
Flag as (case when FlagBit = 1 then 'Yes' else 'No' end)
. . .
i'm new to Analysis Services. I have created a dimension on a boolean column. Now users want to have 'yes' and 'no' instead of 'true' and 'false' as result.
Thanks.
Create a calculated column in your DSV which provides the label you want.
case when column = 0 then 'No' else 'Yes' end