Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi

I wonder is someone could quickly help me out put some SQL query together. I have some data that i would like to return back into a report, selecting some fields from the database table.

Fields as follows:

VERSION_ID_FK
DATE_TYPE
FROM_DAY
FROM_MONTH
FROM_YEAR
TO_DAY
TO_MONTH
TO_YEAR


So basically what i want to get out of this is that the client has got a way in which they enter dates in system and sometimes they only just enter the year without the day and month or they only enter month and year without the day.

So i want the sql please if someone can help to check first is there a day then return either or both FROM_TO, FROM_MONTH, FROM_YEAR, TO_DAY, TO_MONTH, To_YEAR if these fields are not null. These fields are linked bu the Version_ID_FK.

Then next check would be if FROM_DAY or TO_DAY is blank return FROM_MONTH, FROM_YEAR TO_MONTH, TO_YEAR. and if both day and month are null just return the year for either both or just one of them depending which one has got data in them.

I hope that is clear and hope someone will be able to help me out quickly.

Thanks in advance. Let me know if you have any questions

So here is the current SQL that someone had already put in place, but i have now added the fields mentioned above to the table

SQL
select p.prod_date_id, p.version_id_fk, p.DATE_TYPE_ID, p.date_type, p.date_type, p.date_from, p.date_to, p.circa, p.notes, p.created_by, p.created_date, p.modified_by, p.modified_date from
mdd_version_production_dates p, mdd_versions v
where v.version_id = p.version_id_fk
and v.version_id1 = (case when :VERSIONID1 is null then v.version_id1 else :VERSIONID1 end)
Posted
Updated 4-Nov-14 2:05am
v2
Comments
DamithSL 4-Nov-14 7:45am    
what you have tried so far? can you update the question with that sql query?
Maciej Los 4-Nov-14 7:45am    
What version of MS SQL server?

1 solution

Try this:
SQL
SELECT <List_Field>
FROM <TableName>
WHERE NOT DateField IS NULL


or use ISNULL[^] or COALESCE[^] function to replace NULL with default new value.

SQL
SELECT COALESCE(NULL, '1899-01-01') AS DefaultNewDate
 
Share this answer
 
v2
Comments
Shweta N Mishra 4-Nov-14 9:32am    
+5 :)
Maciej Los 4-Nov-14 9:33am    
Thank you ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900