Question

I need use join 2 table show all data.

Exam

Table Contact

numCode | fullName
00001   | Midna 
00002   | Klog
00003   | Porla
00004   | Seka
00005   | Mila

Table dateFile

numCode | dateCurr
00001   | 2012-10-29 00:00:00.000
00002   | 2012-10-29 00:00:00.000
00005   | 2012-10-29 00:00:00.000

Code Sql Server

SELECT df.numCode as 'numCode', tf.dateCurr as 'dateCurr'
FROM dateFile df Full Outer join Contact ct On ct.numCode = df.numCode
WHERE df.dateCurr = '2012-10-29'

Output

numCode | dateCurr
00001   | 2012-10-29 00:00:00.000
00002   | 2012-10-29 00:00:00.000
00005   | 2012-10-29 00:00:00.000

But I need output get:

numCode | dateCurr
00001   | 2012-10-29 00:00:00.000
00002   | 2012-10-29 00:00:00.000
00003   | 2012-10-29 00:00:00.000  (Insert Date from choose datetime)
00004   | 2012-10-29 00:00:00.000  (Insert Date from choose datetime)
00005   | 2012-10-29 00:00:00.000
Was it helpful?

Solution

Try:

SELECT df.numCode as 'numCode', 
       coalesce(tf.dateCurr, '2012-10-29') as 'dateCurr'
FROM dateFile df Full Outer join Contact ct 
On ct.numCode = df.numCode and df.dateCurr = '2012-10-29'

OTHER TIPS

try this:

select c.numCode,ISNULL(d.dateCurr,'2012-10-29 00:00:00.000') from Contact c left join dateFile d
on c.numCode = d.numCode 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top