Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help

Author  Topic 

Mohamed Faisal
Yak Posting Veteran

51 Posts

Posted - 2013-08-10 : 14:49:54
My code is

select *
from Student
where DATEDIFF(YY,[Date],getdate())>=17
AND YEAR = 1
group by StudentName LIKE 'IT%'
order by Name ASC, Age DESC;

And having a problem to view the DATEDIFF as age and also having error:

Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'LIKE'.

Anyone could could point out what is wrong.

Thanks

Mohamed Faisal
Yak Posting Veteran

51 Posts

Posted - 2013-08-10 : 16:01:50
I have correct the SQL but need help in how to use the Order by for a virtual column Age:

Select *,DateDiff(YY,[DateOfBirth],getdate())As Age
from Student
Where Year = 1 and StudentNumber like 'IT%' and Age >= 17
order by Name Asc,Age Desc;
Go to Top of Page

pascal_jimi
Posting Yak Master

167 Posts

Posted - 2013-08-10 : 16:06:49
must
case when
not
group by

my friend

http://sql-az.tr.gg/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-08-10 : 16:12:45
[code]SELECT *
FROM dbo.Student
WHERE [Date] >= DATEADD(YEAR, DATEDIFF(YEAR, '19170101', GETDATE()), '19000101')
AND [Year] = 1
AND StudentName LIKE 'IT%'
ORDER BY Name,
Age DESC;[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

Mohamed Faisal
Yak Posting Veteran

51 Posts

Posted - 2013-08-10 : 16:29:11
How could i use the Order by Age Desc i am getting error
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-08-10 : 16:39:13
quote:
Originally posted by Mohamed Faisal

How could i use the Order by Age Desc i am getting error



Change SwePeso's code as follows:

[CODE]

SELECT *
FROM dbo.Student
WHERE [Date] >= DATEADD(YEAR, DATEDIFF(YEAR, '19170101', GETDATE()), '19000101')
AND [Year] = 1
AND StudentName LIKE 'IT%'
ORDER BY Name,
[Date] DESC;
[/CODE]
Go to Top of Page
   

- Advertisement -