|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
converting datetime from character string
Can anyone tell me what this error message means and how I can correct it on my statement:
Code:
Server: Msg 241, Level 16, State 1, Line 1 Syntax error converting datetime from character string. Here's my query statement: Code:
SELECT animalid AS "Animal ID", name AS "Name", categoryid AS "Category ID", DATENAME(MONTH, dateBorn) + ' ' + DATENAME(DAY, dateBorn) + ', ' + DATENAME(YEAR, dateBorn) AS "Date of Birth" FROM animal WHERE categoryid = 'Cat' AND dateBorn = 'May%' I know it has something to do with: dateBorn = 'May%' because when I took out Quote:
|
|
#2
|
|||
|
|||
|
Code:
AND DATENAME(MONTH, dateBorn) = 'May' |
|
#3
|
|||
|
|||
|
Quote:
Code:
SELECT customerid AS "Customer ID", lastname AS "Last Name", phone AS "Phone", DATENAME(MONTH, saleDate) + ' ' + DATENAME(DAY, saleDate) + ', ' + DATENAME(YEAR, saleDate) AS "Sale Date" FROM Customer, Sale WHERE saleDate BETWEEN (DATENAME(MONTH, saleDate) = 'June' DATENAME(DAY, saleDate) = '1') AND (DATENAME(MONTH, saleDate) = 'June' DATENAME(DAY, saleDate) ='7') The error message is: Line 9: Incorrect syntax near '='. I followed the same format as my first query. The error is on the WHERE line: Code:
WHERE saleDate BETWEEN (DATENAME(MONTH, saleDate) = 'June' DATENAME(DAY, saleDate) ='1') AND (DATENAME(MONTH, saleDate) = 'June' DATENAME(DAY, saleDate) ='7') I only need the list of customers who shopped at the store the first seven days in June. Please let me know how I could correct the problem? |
|
#4
|
|||
|
|||
|
What data do you want? From june 1st to june 7th or what?
Code:
SELECT customerid AS "Customer ID", lastname AS "Last Name", phone AS "Phone", DATENAME(MONTH, saleDate) + ' ' + DATENAME(DAY, saleDate) + ', ' + DATENAME(YEAR, saleDate) AS "Sale Date" FROM Customer, Sale WHERE month(saleDate) = 6 and day(saledate) between 1 and 7 |
|
#5
|
|||
|
|||
|
I see that you are missing the join criteria as well. What is the relation between customer and sale?
|
|
#6
|
|||
|
|||
|
[QUOTE=swampBoogie]What data do you want? From june 1st to june 7th or what?
Yes, June 1 - 7. I updated my query with the corrections that you made and got this error: Ambiguous column name 'customerId'. It seems that customerId both appears on Customer & Sale tables. So, I declared customerId, but here's another error that I get: Line 9: Incorrect syntax near 'MONTH'. Am I declaring customerId incorrectly here: Code:
SELECT c.customerId AS "Customer ID", lastName AS "Last Name", phone AS "Phone", DATENAME(MONTH, saleDate) + ' ' + DATENAME(DAY, saleDate) + ', ' + DATENAME(YEAR, saleDate) AS "Sale Date" FROM Customer AS c, Sale AS s WHERE (c.customerId = s.customerId) MONTH(saleDate) = 6 AND DAY(saleDate) BETWEEN 1 AND 7 |
|
#7
|
|||
|
|||
|
Quote:
Both has customerId. Hence, they are joint through customerId. customerId is the PK for Customer and therefor it's a FK for Sale. Anyway, I think I figured out how to solve the problem that I just addressed earlier. Here's my updated query statements: Code:
SELECT c.customerId AS "Customer ID", lastName AS "Last Name", phone AS "Phone", DATENAME(MONTH, saleDate) + ' ' + DATENAME(DAY, saleDate) + ', ' + DATENAME(YEAR, saleDate) AS "Sale Date" FROM Customer AS c, Sale AS s WHERE (c.customerId = s.customerId) AND MONTH(saleDate) = 6 AND DAY(saleDate) BETWEEN 1 AND 7 ORDER BY saleDate By running this query, I was able to get the results that I needed. I just added the ORDER BY statement so the date is sorted in ascending order THANK YOU SO MUCH for your HELP ![]() |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > converting datetime from character string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|