Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Server Query

Posted on 2015-01-30
7
Medium Priority
?
136 Views
Last Modified: 2015-01-30
Hi Experts,
I have a table where the the columns are named Day1, Day2, Day3, etc.
I have 35 datetime data type columns like this.
When I search the Day1 column for February dates I do it like this:

Select * from VacationBid WHERE DATEPART(mm,Day1) = 2 and DATEPART(yyyy,Day1)<>1900

My goal is to display all records where to month is February in any of the 35 columns not only in the Day1 column.
Is it possible to retrieve all February records from all the 35 columns with one single query, instead of doing it one by one, like it is done below?

Select * from VacationBid WHERE DATEPART(mm,Day1) = 2 and DATEPART(yyyy,Day1)<>1900
Select * from VacationBid WHERE DATEPART(mm,Day2) = 2 and DATEPART(yyyy,Day2)<>1900
Select * from VacationBid WHERE DATEPART(mm,Day3) = 2 and DATEPART(yyyy,Day3)<>1900

Thank you for your help?
0
Comment
Question by:romsom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 79

Expert Comment

by:arnold
ID: 40581026
An option is to convert the table from multiple columns, to multiple rows.

An example of an unpivot is displayed at an MS

Such that you have the
Entries,datetime as a single column per row.

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/bee65313-cfd0-43bb-bbf6-30ff9ddc0c9a/sql-query-rows-to-columns?forum=transactsql


Pivot rows to column
Unpivot columns to rows.


https://technet.microsoft.com/en-us/library/ms177410(v=sql.90).aspx

The example on social ms is to be used an example to first convert the table data (unpivot) having the day1-day35 shifted out such that there will be one datetime per row
Then you would use the resulting table to run your query searching for the dates of interest in the single datetime column.
0
 

Author Comment

by:romsom
ID: 40581066
This is interesting but I'm not able to customize it to my needs.
Would I have to start with something like this?

DECLARE @t TABLE (Day1 datetime, Day2 datetime, Day3 datetime, Day4 datetime, Day5 datetime)

INSERT INTO @t (Day1, Day2, Day3, Day4, Day5)

SELECT Day1 WHERE DATEPART(mm,Day1) = 2 from VacationBid UNION ALL

SELECT Day2 WHERE DATEPART(mm,Day2) = 2  from VacationBid UNION ALL
0
 
LVL 79

Expert Comment

by:arnold
ID: 40581073
The MS converts the table you have into a dynamic table of the layout you want
Declare @t table (columns preceeding the ,days as varchar , date as datetime)
What columns preceed the datetime columns?

I.e. You have 1,day1,day2,day3
End up
ID,days,date
1,day1,date
1,day2,date
1,day3,date

This will be the table you would query the date for the range you want.
0
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 

Author Comment

by:romsom
ID: 40581080
I have columns named ID, First_Name, Last_Name, Emp_Id, Day1, Day2, Day3... up to Day35

I just don't know how to declare them to insert them into the dynamic table.
ID is int
First_Name is varchar(25)
Last_Name is varchar(25)
The day columns are datetime
0
 
LVL 79

Accepted Solution

by:
arnold earned 2000 total points
ID: 40581103
select * from yourtablename
unpivot ( "day" FOR days in (day1,day2,day3,day4,day5,day6,day7,day8,day9,....day35)
) rows_only
where  DATEPART(mm,"Day") = 2 and DATEPART(yyyy,"Day")<>1900

Open in new window


I think this should do it for you.
you will get the output as
id,firstname,lastname,emp_id,day,days
2,firstuser,lastname,24235432,'2014-02-25 12:35:45',day23

Depending on what you want to do with the results.


The dynamic table was used to illustrate/load the data into it to test the output.  In your case, you have a complete table with the data.
0
 
LVL 79

Expert Comment

by:arnold
ID: 40581105
Had to correct, day is a reserved word, so when using it as a column, it has to be in quotes (")
0
 

Author Closing Comment

by:romsom
ID: 40581111
This is perfect, thank you very much for walking me through. I've learned something very useful.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question