SQL Server - 2008 Column Data Type - sql

I have the below Case Statment and I'm inserting this into a new table. The column under the new table is Varchar but I need it as an INT. I have changed the data type using the Alter statment but I frequenctly delete and create the same table. Is there a way to have the new table create the data type of INT instead of varchar for the below syntax?
CASE WHEN F.END_DATE IS NOT NULL OR F.REASON IS NOT NULL THEN '0' ELSE '1' END Enrolled'

Get rid of the single quotes around 0 and 1:
CASE WHEN F.END_DATE IS NOT NULL OR F.REASON IS NOT NULL THEN 0 ELSE 1 END Enrolled'

Try Following:
CASE WHEN coalesce(F.END_DATE,F.REASON ,0)=0 THEN 0 ELSE 1 END Enrolled

Related

How to fetch a result of an int column data type from a table and if no record found select 0 as default in microsoft sql azure

How to fetch a result of an int column data type from a table and if no record found select 0 as default in microsoft sql azure
I had tried ISNULL but it does not work.
In case there is a table named student.
Column_Name data_type
roll_nbr int
name varchar(30)
Select ISNULL(roll_nbr,0) FROM student where name = 'Sam;
I am expecting the query to return roll_nbr from the table if exists else return 0 as default if no rows is found.
You can modify your Azure Sql Query as below
Using COALESCE()
SELECT COALESCE((SELECT Roll_nbr FROM student WHERE NAME = 'Sam'), 0);
(http://sqlfiddle.com/#!18/9a6eaf/2)
The subquery will return the RollNumber if satisfies the where condition else it will return blank/null result. Use of COALESCE function on subquery will return first not null value.
Using ISNULL()
SELECT ISNULL((SELECT Roll_nbr FROM student WHERE NAME = 'Sam'), 0)
http://sqlfiddle.com/#!18/9a6eaf/6
Instead of COALESCE() ISNULL() can be used.
ISNULL function accepts only 2 arguments, used to replace null value and it is T-SQL function.
COALEASCE function is ANSI SQL Standard based and it accepts 1-n arguments it returns first not null value.
SELECT TOP 1
CASE
WHEN EXISTS(SELECT 1 FROM Student WHERE name = 'Sam') THEN roll_nbr
ELSE 0
END
FROM Student

How do I update two variables in a oracle sql server update using case statement

I am trying to update two variables in a Oracle server as follows:
UPDATE UserTable
SET user_email='asdf#company.com',
(CASE WHEN reason != '' THEN why_update= 'change email server' END)
WHERE user_id = 123
I want to update why_update column only if the user provided a reason for update otherwise leave the column as it is (which is varchar type and can be NULL).
Or any other better solution?
If you're trying to update multiple columns, you would need to do it like:
UPDATE UserTable
SET user_email = 'asdf#company.com',
why_update = CASE WHEN reason IS NOT NULL THEN 'change email server' END
WHERE user_id = 123;
You would always end up updating both columns - in this case, if reason is null, then why_update would be set to NULL, losing any previous value. If you don't want that to happen, you would need to add in an ELSE why_update clause into the CASE expression.
N.B. I have changed your != '' into IS NOT NULL because an empty string ('') is recognised as being a NULL in Oracle. NULL will never match an equals or not equals check, so your above CASE expression won't work as I suspect you want it to work.
THis will work:
UPDATE UserTable
SET user_email='asdf#company.com',
why_update= CASE WHEN reason != '' THEN 'change email server' else why_update END
WHERE user_id = 123

SQL: converting a column of numeric and null values to FLOAT

I have a table with a column of varchar type. The column contains 568710 records of numeric (I mean the ISNUMERIC() returns 1) and 91 records of null values (i.e., the ISNUMERIC() returns 0). Now, I need to convert the column to FLOAT without losing the null records or replacing them with any other value. Is it possible in SQL?
When I use CONVERT(FLOAT, [Value]) conversion, I get the following error:
Msg 8114, Level 16, State 5, Line 48
Error converting data type varchar to float.
I read that the null can be converted to any type. So it should be possible.
You can use this
SELECT CONVERT(float, CASE WHEN ISNUMERIC(columnName) = 1 THEN columnName ELSE NULL END) FROM TableABC
Try :::
ALTER TABLE tablename
ADD NewFloatColumn FLOAT
UPDATE TableName
SET NewFloatColumn =
CASE WHEN ISNUMERIC(VarcharColumn) =1 THEN CAST (VarcharColumn AS float)
WHEN UPPER(VarcharColumn) = 'NULL' THEN null
END
Select (CASE WHEN ISNUMERIC(c) = 1
THEN (CASE WHEN c LIKE '%.%' THEN c ELSE c + '.00' END)
ELSE '0.00'
END)from table_name
I just spent some time scratching my head at the same problem. I was able to resolve this by converting first to int, then to float. Testing shows no data loss as my float column did not contain decimal, it simply had to match the dtype of another column for a later UNION operation.
This was completed on SQL Server 2016.
ALTER TABLE table1
ALTER COLUMN column1 int;
ALTER TABLE table1
ALTER COLUMN column1 float;

SQL - Setting parameters for Default Column Value in Table

Using SQL Server 2012.
I have a column in my member table that is created using the following script:
[EnableMemberWebAccess] [bit] NOT NULL DEFAULT ((1))
I want this column to only have 1 as a default if another column in this same table is a certain value. Let's call this column MemDesc, and I want the EnableMemberWebAccess to be defaulted to 1 when MemDesc = 'Founder', and for it to default to 0 when MemDesc != 'Founder'.
Any help much appreciated!
There is probably no way to achieve a default value that can be changed afterwards. Either you have a value that you insert in the beginning. You will then need to take care of consistency within the application:
ALTER TABLE *table* ADD COLUMN EnableMemberWebAccess bit NULL
UPDATE *table* SET *table.*EnableMemberWebAccess = CAST(CASE WHEN *table*.MemDesc = 'Founder' THEN 1 ELSE 0 END AS bit)
ALTER TABLE *table* ALTER COLUMN EnableMemberAccess bit NOT NULL
Or you have to use a computed column. This will not allow you to change the value of the column except if you change the value of the column it depends on.
Computed column should work for you:
ADD EnableMemberWebAccess AS cast((CASE WHEN MemDesc='Founder' THEN 1 ELSE 0 END) as bit)

Sql error when casting char column to varchar value

I have a char(1) column but I want select this column like this:
SELECT CAST(CASE WHEN [ENABLED] = 'Y' THEN 'Yes' ELSE 'No' END AS EnabledTitle)
FROM [ICS_USERS]
I am getting this error:
Conversion failed when converting the varchar value 'Yes' to data type bit.
Is there any way to show varchar values in char columns?
Thanks
You don't need to call CAST()
SELECT CASE WHEN [ENABLED] = 'Y' THEN 'Yes' ELSE 'No' END AS EnabledTitle
FROM [ICS_USERS]
CAST converts a value. Your conversion takes place on the CASE statement.