How to use + in sql sever - sql-server

I am trying to write a join query. Table A has a Primary Key = 123456000000 and table B has foreign key = 123456.
I want to know how can i join the 2 tables using inner join using + in query
I wrote the following -
SELECT RAP.COL1, FRE.COL2 FROM Table 1 AS RAP
INNER JOIN Table 2 AS FRE
ON RAP.COL1+'000000' = FRE.COL2
NOTE - COL2 = 123456000000
COL1 = 123456
I NEED TO ADD 000000 so that i can join. BUT THE ABOVE WRITTEN DOES NOT WORK :(
Please help
Cheers
OZ

Try this, but why the keys are different. Any specific reason.
SELECT RAP.COL1, FRE.COL2 FROM Table 1 AS RAP
INNER JOIN Table 2 AS FRE
ON (Convert(varchar(20),RAP.COL1)+'000000')as COL1 = FRE.COL2

SELECT RAP.COL1, FRE.COL2 FROM Table 1 AS RAP
INNER JOIN Table 2 AS FRE
ON RAP.COL1 = LEFT(FRE.COL2,6)

Related

SQL Join Including NULLs

Using SQL Server Management Studio 2016 (v 13.0).
I have two tables with two distinct keys I can use to join, with the catch being that there are mixed NULLs in both PK columns:
Table 1 Table 2
Acct1 App1 Acct2 App2 Product
----------- ----------------------
1 A NULL A Bed
2 B 2 B Sofa
3 C 3 NULL Bed
4 D 4 D Bed
Desired result in the joined table, only including those where Product = Bed:
Acct App Product
1 A Bed
3 C Bed
4 D Bed
Thank you!
While I agree #d219's answer should be the correct solution, a different approach could use an or in the join such like:
select Acct1,App1,Product
from table1 inner join table2
on App1=App2 or Acct1=Acct2
where Product='Bed'
See this post for discussion on using the or join.
One way would be to do two separate SELECT statements using each key and then UNION them, something like this:
SELECT t1.Acct1, t1.App1, t2.Product
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.Acct1 = t2.Acct2
WHERE t2.Product = 'Bed'
UNION
SELECT t1.Acct1, t1.App1, t2.Product
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.App1 = t2.App2
WHERE t2.Product = 'Bed'

SQL - Multi Table Joining with 1-1 and 1 - Many relationship

I am trying to learn SQL and want to understand a scenario where i can Join 2 separate queries.
I have my APP_MTRX table would join with Cust Table to retrieve all the records whose TYPE_ORD_NUM = 0 or 1
I need to join ADD_DTLS Table with SHR_ADR table based on SHR_ADR_ID and need to retrieve all the columns whose adr_type_id is either 0 or 1 again
joining the result of 1 and 2
below is my SQL
select * from app_mtrx ABC
left join cust on cust.cust_id = ABC.cust_id and abc.cust_ty_ord = 0
left join cust BBC on cust.cust_id = BBC.cust_id and abc.cust_ty_ord = 1
left join add_dtls DEF ON DEF.cust_id=BBC.cust_id
left join shr_adr SHR on shr.shr_adr_id = def.shr_Adr_id
Can you please suggest if this is the correct approach and also if it is joining my 1 & 2 correctly....
Try this:
SELECT * FROM APP_MTRX am INNER JOIN Cust c
ON am.cust_id = c.cust_id
AND am.cust_ty_ord in (0,1)
INNER JOIN add_dtls ad
ON ad.cust_id=c.cust_id
INNER JOIN shr_adr sh
ON ad.shr_adr_id = sh.shr_Adr_id
AND ad.adr_type_id in (0,1)

how to join from 2 fields to 1 field on another table

on sqlServer i have 2 tables:
table1: Students => studentName=david, class1Id=2,class2Id=4
table2: classes=> classId=2, className="class1"
classId=4, className="class2"
class1Id and class2Id relate to classes.classId
i want to do sql query to get :
studentName=david, className1="class1",className2="class2",
I know to do join between 2 table but not like that
thanks!
You just need to JOIN to Classes twice:
Select S.StudentName,
C1.ClassName As ClassName1,
C2.ClassName As ClassName2
From Students S
Join Classes C1 On C1.ClassId = S.Class1Id
Join Classes C2 On C2.ClassId = S.Class2Id

Query junction table-many to many relationship

I'm trying to run a query for a many-many relationship, I created a junction table to do that. Now I'm having issues getting no results when I'm expecting to return 3 rows for each of indexes 122,123,124, can anyone point out my errors, thank you
SELECT * FROM [Moldingdata].[dbo].[mach_part_junction] ORDER BY [machinename] ASC
SELECT MachineList.machinename ,JDEPARTIMGLU.Jde_part_num
from mach_part_junction
INNER JOIN MachineList on mach_part_junction.machinename = machinelist.Machine_ID
INNER JOIN JDEPARTIMGLU on mach_part_junction.machinename = machinelist.Machine_ID
WHERE mach_part_junction.machinename= 'MM01'
Results:
machinename ndx_jde_part_img
----------- ----------------
MM01 122
MM01 123
MM01 124
MM04 122
MM15 124
MM17 122
MM32 122
MM32 123
(8 row(s) affected)
machinename Jde_part_num
----------- --------------------
(0 row(s) affected)
The join conditions on your second and third table are the same.
It looks like the third table's join condition is the one that needs to be changed.
When I troubleshoot queries like this I generally do the following:
SELECT *
FROM mach_part_junction
INNER JOIN MachineList on mach_part_junction.machinename = machinelist.Machine_ID
Then I add the next JOIN:
SELECT *
FROM mach_part_junction
INNER JOIN MachineList on mach_part_junction.machinename = machinelist.Machine_ID
INNER JOIN JDEPARTIMGLU on mach_part_junction.machinename = machinelist.Machine_ID
then the WHERE (if the tables aren't too big)
That way you can see what's going on.

SQL Server date_format in join

I have two tables with two formats date
table 1 :
id time ref
5 1397635972 A
10 1397635975 B
50 1397635976 C
table 2 :
id time ref
10 2013/10/05 D
51 2014/01/02 E
how join two table on table1.id=table2.id and table1.time=table2.time
This is my attempt :
$sql=' select table1.id, table1.time, table1.ref, table2.id, table2.time, table2.ref
from table1 INNER JOIN table2
ON (table1.id = table2.id AND DATE_FORMAT(table1.time,'%y/%m/%d') = table2.time)';
Try this:
select table1.id, table1.time, table1.ref, table2.id, table2.time, table2.ref
from table1 INNER JOIN table2
ON (table1.id = table2.id AND FROM_UNIXTIME(table1.time) = table2.time)
Are you sure that the ID needs to be a part of the join as well? This kind of join with this kind of structure would make almost no sense from a database design standpoint...
No data are not shown
For example FROM_UNIXTIME table2.time :
SELECT FROM_UNIXTIME(1196440219) : '2007-11-30 10:30:19'
I need to format then become : 2013/10/05

Resources