I have a two tables in which where one table has a datecolumn and other do not have, i have to main a union to fetch entries from both table
so as of now i am successful, but the empty fields showing invalid date like 1900-01-01, i want it should be dispayed as null, like a cast into varchar
i am trying it like this
SELECT [orderid],CAST('' as [cancelleddate] as CHAR(20)) as [cancelleddate] ,'' as [cancelledtime]
The above is throwing error eventually
and doing this way will it effect performance
SELECT orderid, cancelleddate, cancelledtime FROM TABLE1
UNION
SELECT orderid, null, null FROM TABLE2
Related
I have come across some code that says
select null as UnitCost,
null as MarkUp
What exactly is this doing? Is it getting the field names unitcost and markup?
Why would you use "select null as..."?
New to this sorry.
Thanks.
That code is aliasing the value null and calling it UnitCost/MarkUp. It is not selecting that column from any available table.
You would usually see this when a statement requires matching column sets, e.g. union all.
select id, col1, col2, null as UnitCost, null as MarkUp
from table_1
union all
select id, null as col1, null as col2, UnitCode, MarkUp
from table_2
Just populate NULL to those selected columns
It is useful when you do Insert...Select, the Table you are trying to insert may have more columns than the selected table has, for convenience, you could use select null as col_name to let the numbers of columns you are trying to insert matches the columns that the target table has.
I need to populate reqopt table which have two varchar fields (valeur, categorie), the first shoud be field from another table (mytable) and another should be a simple varchar ('prop').
I've tried this
INSERT INTO reqopt(valeur, categorie) (select distinct name from mytable , 'prop')
I got this error Incorrect syntax near 'prop'.
INSERT INTO reqopt(valeur, categorie)
select distinct name, 'prop'
from mytable
SELECT DISTINCT NAME AS valeur,'PROP' REMARKS AS categorie
INTO reqopt
FROM MYTABLE
--WHERE CONDITION IF REQUIRED
I have to maintain a scary legacy database that is very poorly designed. All the tables have more than 100 columns - one has 650. The database is very denormalized and I have found that often the same data is expressed in several columns in the same row.
For instance, here is a sample of columns for one of the tables:
[MEMBERADDRESS] [varchar](331) NULL,
[DISPLAYADDRESS] [varchar](max) NULL,
[MEMBERINLINEADDRESS] [varchar](max) NULL,
[DISPLAYINLINEADDRESS] [varchar](250) NULL,
[__HISTISDN] [varchar](25) NULL,
[HISTISDN] [varchar](25) NULL,
[MYDIRECTISDN] [varchar](25) NULL,
[MYISDN] [varchar](25) NULL,
[__HISTALT_PHONE] [varchar](25) NULL,
[HISTALT_PHONE] [varchar](25) NULL,
It turns out that MEMBERADDRESS and DISPLAYADDRESS have the same value for all rows in the table. The same is true for the other clusters of fields I have shown here.
It will be very difficult and time consuming to identify all cases like this manually. Is it possible to create a query that would identify if two fields have the same value in every row in a table?
If not, are there any existing tools that will help me identify these sorts of problems?
There are two approaches I see to simplify this query:
Write a script that generates your queries - feed your script the name of the table and the suspected columns, and let it produce a query that checks each pair of columns for equality. This is the fastest approach to implement in a one-of situation like yours.
Write a query that "normalizes" your data, and search against it - self-join the query to itself, then filter out the duplicates.
Here is a quick illustration of the second approach:
SELECT id, name, val FROM (
SELECT id, MEMBERADDRESS as val,'MEMBERADDRESS' as name FROM MyTable
UNION ALL
SELECT id, DISPLAYADDRESS as val,'DISPLAYADDRESS' as name FROM MyTable
UNION ALL
SELECT id, MEMBERINLINEADDRESS as val,'MEMBERINLINEADDRESS' as name FROM MyTable
UNION ALL
...
) first
JOIN (
SELECT id, MEMBERADDRESS as val,'MEMBERADDRESS' as name FROM MyTable
UNION ALL
SELECT id, DISPLAYADDRESS as val,'DISPLAYADDRESS' as name FROM MyTable
UNION ALL
SELECT id, MEMBERINLINEADDRESS as val,'MEMBERINLINEADDRESS' as name FROM MyTable
UNION ALL
...
) second ON first.id=second.id AND first.value=second.value
There is a lot of manual work for 100 columns (at least it does not grow as N^2, as in the first approach, but it is still a lot of manual typing). You may be better off generating the selects connected with UNION ALL using a small script.
The following approach uses unpivot to create triples. It makes some assumptions: values are not null; each row has an id; and columns have compatible types.
select t.which, t2.which
from (select id, which, value
from MEMBERADDRESS
unpivot (value for which in (<list of columns here>)) up
) t full outer join
(select id, which, value
from MEMBERADDRESS
unpivot (value for which in (<list of columns here>)) up
) t2
on t.id = t2.id and t.which <> t2.which
group by t.which, t2.which
having sum(case when t.value = t2.value then 1 else 0 end) = count(*)
It works by creating a new table with three columns: id, which column, and the value in the column. It then does a self join on id (to keep comparisons within one row) and value (to get matching values). This self-join should always match, because the columns are the same in the two halves of the query.
The having then counts the number of values that are the same on both sides for a given pair of columns. When all these are the same, then the match is successful.
You can also leave out the having clause and use something like:
select t.which, t2.which, sum(case when t.value = t2.value then 1 else 0 end) as Nummatchs,
count(*) as NumRows
To get more complete information.
I've got table
Manga
idmanga title idauthor idgenre idmagazine
and table
Author
idauthor name surname
How to get table with fields
fullname title sumofids
name+surname idmanga+idauthor+idgenre+idmagazine
I can get fullname like this
select name+' '+surname as Fullname from Author
But how to get other fields in one query?
select CONVERT(VARCHAR,idmanga)
+CONVERT(VARCHAR,idauthor)
+CONVERT(VARCHAR,idgenre)
+CONVERT(VARCHAR,idmagazine)
That should do it all in one string.
Add + ' ' + to put in spaces.
To add all the values together, your query should work to get the values for every row. If you want to group by the Name to roll the results up, use the SUM() and you'll get one row per unique name combination
create table Test ( Name varchar(10)
,idmanga int
,idauthor int
,idgenre int
,idmagazine int)
insert into Test
select 'Roger',1,2,3,4
union select 'Bob',4,5,6,7
union select 'Roger',8,9,10,11
union select 'Bob',12,13,14,15
union select 'Bill',16,17,18,19
select Name
, idmanga+idauthor+idgenre+idmagazine
from Test
select Name
, SUM(idmanga+idauthor+idgenre+idmagazine)
from Test
group by Name
Im having two tables with attributes like date(datetime),headline(varchar),text(text)
Now i want to UNION ALL these two tables and sort by the datetime. When doing this i'm getting the error:
Only text pointers are allowed in work tables, never text, ntext, or image columns. The query processor produced a query plan that required a text, ntext, or image column in a work table.
After trying back and forth i found out that it is the text attribute which is causing the error. But what to do? I tried casting to VARCHAR with no succes. Both tables uses text format in the text attribute.
Also when removing the ORDER BY it all works fine. What to do?
The original SQL query is below, but you can just reply to the simplified above.
SELECT id, datetime, author, headline, intro, text, type, toppriority,
secondpriority, comments, companyid, '1' source
FROM Table1
UNION ALL
SELECT AutoID AS id, Dato AS datetime,
ID COLLATE SQL_Latin1_General_CP1_CI_AS AS author, NULL AS headline,
NULL AS intro, Notat COLLATE SQL_Latin1_General_CP1_CI_AS AS text,
CAST(NotatTypeID AS VARCHAR) AS type,
NULL AS toppriority, NULL AS secondpriority, NULL AS comments,
Selskabsnummer AS companyid, '2' source
FROM Table2
WHERE (NotatTypeID = '5') OR (NotatTypeID = '6')
ORDER BY datetime DESC
Thanks in advance
One way round this is to run the union as a sup query and order the results afterwards:
SELECT * FROM
(
SELECT id, datetime, author, headline, intro, text, TYPE, toppriority,
secondpriority, comments, companyid, '1' source
FROM Table1
UNION ALL
SELECT AutoID AS id, Dato AS datetime,
ID COLLATE SQL_Latin1_General_CP1_CI_AS AS author, NULL AS headline,
NULL AS intro, Notat COLLATE SQL_Latin1_General_CP1_CI_AS AS text,
CAST(NotatTypeID AS VARCHAR) AS TYPE,
NULL AS toppriority, NULL AS secondpriority, NULL AS comments,
Selskabsnummer AS companyid, '2' source
FROM Table2
WHERE (NotatTypeID = '5') OR (NotatTypeID = '6')
) a
ORDER BY datetime DESC
What about casting the datetime field to some text field in the index? Please note that using 'datetime' and 'text' as field/alias names can be quite confusing, and a source for potential problems.