Solved

SQL Query problems

Posted on 2016-08-30
2
55 Views
Last Modified: 2016-08-30
My query is suppose to search for "Music" in the fields strEventAbstrace or strEventType within the specified date range but it returns records where strEventAbstrace or strEventType contains "Music" from all dates. What am I doing wrong?

SELECT 
      intEventID
     ,dtmEventDateAndTime AS Date
     ,dtmEventDateAndTime AS Time
     ,strEventAbstract AS [Event Description],"
     ,tEventTypes.strEventType AS [Event Type]
     ,strIsOnsite AS [On Campus]
     ,strTransportationProvided AS Transprotation,monCost AS Cost
FROM tEvents
   "INNER JOIN tEventTypes 
      ON tEvents.intEventTypeID = tEventTypes.intEventTypeID
WHERE      strEventAbstract LIKE '%Music%' 
       OR  strEventType Like '%Music%'
       AND dtmEventDateAndTime >= '08/30/2016’ 
       AND dtmEventDateAndTime <= '09/13/2016’ 
ORDER BY dtmEventDateAndTime

Open in new window

0
Comment
Question by:OhioWoodWright
2 Comments
 
LVL 12

Accepted Solution

by:
Missus Miss_Sellaneus earned 500 total points
ID: 41776915
You need parentheses:

SELECT
      intEventID
     ,dtmEventDateAndTime AS Date
     ,dtmEventDateAndTime AS Time
     ,strEventAbstract AS [Event Description],"
     ,tEventTypes.strEventType AS [Event Type]
     ,strIsOnsite AS [On Campus]
     ,strTransportationProvided AS Transprotation,monCost AS Cost
FROM tEvents
   "INNER JOIN tEventTypes
      ON tEvents.intEventTypeID = tEventTypes.intEventTypeID
WHERE      (strEventAbstract LIKE '%Music%'
       OR  strEventType Like '%Music%')
       AND (dtmEventDateAndTime >= '08/30/2016’
       AND dtmEventDateAndTime <= '09/13/2016’)
ORDER BY dtmEventDateAndTime
0
 
LVL 41

Expert Comment

by:Sharath
ID: 41776948
Also, there are double quotes in your query. Not sure if that is intentional.
SELECT 
      intEventID
     ,dtmEventDateAndTime AS Date
     ,dtmEventDateAndTime AS Time
     ,strEventAbstract AS [Event Description]
     ,tEventTypes.strEventType AS [Event Type]
     ,strIsOnsite AS [On Campus]
     ,strTransportationProvided AS Transprotation,monCost AS Cost
FROM tEvents
   INNER JOIN tEventTypes 
      ON tEvents.intEventTypeID = tEventTypes.intEventTypeID
WHERE     (strEventAbstract LIKE '%Music%' 
       OR  strEventType Like '%Music%')
       AND dtmEventDateAndTime >= '20160830' 
       AND dtmEventDateAndTime <= '20160913'
ORDER BY dtmEventDateAndTime

Open in new window

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GeoClustering  and AOG 25 42
SSIS GUID Variable 2 31
Union & Crosstab qrys 101! 6 55
Related to SQL Query 5 19
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

740 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