Hi I have two columns one called BankNo and the other called BranchNo. im running a view that selects the bank numbers along with there branches. One bank doesn't have a branch number so I would like that value to not come up as "null" but to come up as "No Branch". The BranchNo column is a numeric field. Please help
SELECT Coalesce(Cast(BranchNo as varchar(11)), 'No Branch') As BranchNo
...
Coalesce() function: http://msdn.microsoft.com/en-us/library/ms190349.aspx
Try something like this
Select BankNo, ISNULL(Cast(BranchNo as nvarchar(10),'No Branch') from BankTable
Related
I have a table that I need to add the same values to a whole bunch of items
(in a nut shell if the item doesn't have a UNIT of "CTN" I want to add the same values i have listed to them all)
I thought the following would work but it doesn't :(
Any idea what i am doing wrong ?
INSERT INTO ICUNIT
(UNIT,AUDTDATE,AUDTTIME,AUDTUSER,AUDTORG,CONVERSION)
VALUES ('CTN','20220509','22513927','ADMIN','AU','1')
WHERE ITEMNO In '0','etc','etc','etc'
If I understand correctly you might want to use INSERT INTO ... SELECT from original table with your condition.
INSERT INTO ICUNIT (UNIT,AUDTDATE,AUDTTIME,AUDTUSER,AUDTORG,CONVERSION)
SELECT 'CTN','20220509','22513927','ADMIN','AU','1'
FROM ICUNIT
WHERE ITEMNO In ('0','etc','etc','etc')
The query you needs starts by selecting the filtered items. So it seems something like below is your starting point
select <?> from dbo.ICUNIT as icu where icu.UNIT <> 'CTN' order by ...;
Notice the use of schema name, terminators, and table aliases - all best practices. I will guess that a given "item" can have multiple rows in this table so long as ICUNIT is unique within ITEMNO. Correct? If so, the above query won't work. So let's try slightly more complicated filtering.
select distinct icu.ITEMNO
from dbo.ICUNIT as icu
where not exists (select * from dbo.ICUNIT as ctns
where ctns.ITEMNO = icu.ITEMNO -- correlating the subquery
and ctns.UNIT = 'CTN')
order by ...;
There are other ways to do that above but that is one common way. That query will produce a resultset of all ITEMNO values in your table that do not already have a row where UNIT is "CTN". If you need to filter that for specific ITEMNO values you simply adjust the WHERE clause. If that works correctly, you can use that with your insert statement to then insert the desired rows.
insert into dbo.ICUNIT (...)
select distinct icu.ITEMNO, 'CTN', '20220509', '22513927', 'ADMIN', 'AU', '1'
from ...
;
I am trying to solve this exercise:
Under the assumption that receipts of money (inc) and payouts (out) can be registered any number of times a day for each collection point [i.e. the code column is the primary key], display a table with one corresponding row for each operating date of each collection point.
Result set: point, date, total payout per day (out), total money intake per day (inc).
Missing values are considered to be NULL.
After several hours of headbanging I found this solution online:
SELECT X.POINT,X.DATE,SUM(OUT),SUM(INC) FROM (
SELECT I.POINT,I.DATE,NULL AS OUT, SUM(I.INC) AS INC FROM INCOME I
GROUP BY I.POINT,I.DATE
UNION
SELECT O.POINT,O.DATE,SUM(O.OUT) AS OUT , NULL AS INC FROM OUTCOME O
GROUP BY O.POINT,O.DATE) AS X
GROUP BY POINT,DATE
I tried to understand how this works. I googled all variations of "NULL AS OUT" but I could not find any explanation/concept. All results point out to stored procedures which is not what I am looking fro I think.
Can someone explain to me how these lines with "AS OUT" work, please?
SELECT I.POINT,I.DATE,NULL AS OUT, SUM(I.INC) AS INC FROM INCOME I
GROUP BY I.POINT,I.DATE
UNION
SELECT O.POINT,O.DATE,SUM(O.OUT) AS OUT , NULL AS INC FROM OUTCOME O
On the left - The complete version of both tables
On the right the result
out is probably an unfortunate name there, but other than that, there's nothing magical there.
"null" is the literal value of null.
"as out" assigns a column alias to the selected null.
Syntactically, this is the equivalent to any other literal value with any other alias, e.g., SELECT 'some_varchar_literal' AS some_alias or SELECT 123 AS numeric_alias.
Copying comment as answer to mark it as worked
is this SQL server or mysql. from my understanding I think as out means null is referred to a column named OUT, basically alias name for a column. Income table will not have outcome and outcome table will not have income. Hence in both the select statement, the respective values are marked as null (NULL AS OUT & NULL AS INC). when you perform aggregate on these columns then the null values will be ignored.
you can use NULL as when your come across a similar union statement where one of the two tables does not have a column. In that case you can create this missing column as a dummy one and use it in your code.
I have two date columns.
Sometimes they both have dates(Which will be same always in both the columns) and sometimes one is empty and one has date value.
So, instead of two columns, I am trying to get one column.
If one is empty it will take date value from other column and if both have values(which will always be same) it will just take any of the value from the two columns.
I have tried UNION commands but its not giving me the desired result.
SQL Server has a couple different options for this scenario. You can use COALESCE, ISNULL, or a CASE statement.
Based on the information you provided I would use COALESCE. It offers several benefits over ISNULL and is very simple to implement. A CASE statement seems like overkill for what you are trying to do. Check out the link above for more info on each solution.
Welcome to Stack Overflow!
You need Coalesce
Also, in the future, you should put sample data and metadata in text in your question, rather than as attachments.
You could use the ISNULL statement if it is SQL
SELECT ISNULL(ReturnDate,RepartureDate) as dateAct FROM AviationReservation_dev
UPDATE tableName
SET Date1Column = ISNULL(Date1Column, Date2Column);
Context: ISNULL ( check_expression , replacement_value ), if first argument is not null, it will use that argument.
After the update, delete the other column.
It seems there is no case for both column to be empty, then in such condition, you can do something like this:
SELECT
CASE
WHEN column1 IS NULL THEN column2
WHEN column2 IS NULL THEN column1
ELSE column1 orcolumn2
Hello I currently have 2 tables like this:
Parcel which has idParcel,quantityParcel,idProduct
and
Product which has idProduct, nameProduct
When I try to execute Aggr( sum(quantityParcel),[idProduct] ) it works just fine making the sum of quantityParcel by idProduct but when I try to run Aggr( sum(quantityParcel),[nameProduct] ) it just returns the sum of all quantityParcel values without grouping anything, is there any way I can group by nameProduct referencing it from the Product table? The reason I want to do this is because I want to show the actual product name in my dimension instead of just the idProduct number, thanks :)
If you use nameProduct as Dimension why don't just use: sum(quantityParcel)
another option is to use NODISTINCT : Aggr(NODISTINCT sum(quantityParcel),[nameProduct] )
Example: On the phone table, someone has two phone number SQL would give me the a second row of the same person with different phone number instead of second column. What query do I use to check if person_id appears more than once insert second row of data in a separate column?
I hope this make sense. Thanks in advance!
Try something like this:
SELECT person_id, COUNT(person_id) AS 'PersonIDCount' FROM phone_table
GROUP BY person_id
HAVING COUNT(person_id) > 1
The query will return all records where the same person_id key was inserted more than once.