Any SQL language ex...
 

MegaSack DRAW - This year's winner is user - rgwb
We will be in touch

[Closed] Any SQL language experts in the house?

32 Posts
12 Users
0 Reactions
75 Views
 DezB
Posts: 54367
Free Member
Topic starter
 

Got a simple question, but I'm sh1t at SQL! Can ye help?


 
Posted : 11/03/2009 3:05 pm
Posts: 0
Full Member
 

Fire away - I'm sure there are a few SQL geeks on here (not that good myself mind!).


 
Posted : 11/03/2009 3:06 pm
Posts: 3329
Free Member
 

Not without the question we can't.


 
Posted : 11/03/2009 3:06 pm
Posts: 621
Free Member
 

depends what the question is!


 
Posted : 11/03/2009 3:06 pm
 DezB
Posts: 54367
Free Member
Topic starter
 

Just fishing first!

Well, er. I suppose I'll have to paste the thing to explain it. I'm making an SMS collection

I've got:
[b]from SMS_R_System where SystemOUName like "%thcg.net/EU/Gosport Data Center/%Workstations" and (NetbiosName like "%-1%" [/b]..etc

with me so far?


 
Posted : 11/03/2009 3:11 pm
 DezB
Posts: 54367
Free Member
Topic starter
 

sorry thats just a section of the query, in case you were wondering!


 
Posted : 11/03/2009 3:14 pm
Posts: 0
Free Member
 

I have one if you want to help

this is my query

SELECT Patient.Provider, Count(Patient.[Date entered]) AS [Patients Assigned], [b]DatePart("y",DateAdd("d",+1,Now()))[/b] AS Expr1
FROM Patient
WHERE (((Right(Left(Patient.[Date entered],5),2))=DatePart("m",(DateAdd("m",-5,Now())))))
GROUP BY Patient.Provider;

The part in bold is where i want to display just the year?

Cheers,
Nezbo


 
Posted : 11/03/2009 3:14 pm
Posts: 0
Free Member
 

I have tried

DatePart("Y", now())

with no joy...


 
Posted : 11/03/2009 3:15 pm
 DezB
Posts: 54367
Free Member
Topic starter
 

all I want to do is select from more SystemOUNames.

so.. [b]where SystemOUName like "%thcg.net/EU/Gosport Data Center/%Workstations" or "%thcg.net/EU/Gosport Data Center/%Laptops" and (NetbiosName... [/b] etc.

Can I do that with wildcard values?
Apologies if this is really badly explained!! So you get me drift?


 
Posted : 11/03/2009 3:16 pm
 DezB
Posts: 54367
Free Member
Topic starter
 

Bugger off Nezbo!

I think I need to do this - [b]where SystemOUName like "%thcg.net/EU/Gosport Data Center/%Workstations" or SystemOUName like "%thcg.net/EU/Gosport Data Center/%Laptops" and (NetbiosName... [/b]

Do I need brackets somewhere?
This is making me sweat.


 
Posted : 11/03/2009 3:18 pm
Posts: 91097
Free Member
 

Brackets round the Or clauses if I were you.


 
Posted : 11/03/2009 3:19 pm
Posts: 0
Free Member
 

if you past the whole statement we will ahve a better understanding?

🙂


 
Posted : 11/03/2009 3:21 pm
 DezB
Posts: 54367
Free Member
Topic starter
 

where SystemOUName like [b]([/b]"%thcg.net/EU/Gosport Data Center/%Workstations" or SystemOUName like "%thcg.net/EU/Gosport Data Center/%Laptops"[b])[/b] and (NetbiosName...

just that?


 
Posted : 11/03/2009 3:22 pm
 DezB
Posts: 54367
Free Member
Topic starter
 

[i]if you past the whole statement we will ahve a better understanding?[/i]

OK. After what mogrips said, will this baby work?

select SMS_R_System.ResourceID,SMS_R_System.ResourceType,SMS_R_System.Name,SMS_R_System.SMSUniqueIdentifier,SMS_R_System.ResourceDomainORWorkgroup,SMS_R_System.Client from SMS_R_System where (SystemOUName like "%thcg.net/EU/Gosport Data Center/%Workstations" or SystemOUName like "%thcg.net/EU/Gosport Data Center/%Sales laptops" or SystemOUName like "%thcg.net/EU/Gosport Data Center/%Laptops") and (NetbiosName like "%-1%" or NetbiosName like "%-2%" or NetbiosName like "%-3%")


 
Posted : 11/03/2009 3:24 pm
Posts: 0
Free Member
 

(SystemOUName like "%thcg.net/EU/Gosport Data Center/%Workstations" or SystemOUName like "%thcg.net/EU/Gosport Data Center/%Laptops")

I would start with the brcket movement...


 
Posted : 11/03/2009 3:25 pm
 DezB
Posts: 54367
Free Member
Topic starter
 

That looks the same as mine? (Which returns nothing)

EDIT: Hang on, looks like it's worked!

Thanks folks!


 
Posted : 11/03/2009 3:28 pm
Posts: 31206
Full Member
 

Nezbo,
[code]DatePart("y", now())[/code] gives you "day of year".

For the year use:
[code]DatePart("yy", now())[/code]
or
[code]DatePart("yyyy", now())[/code]

Assuming this is [url= http://msdn.microsoft.com/en-us/library/ms174420.aspx ]DatePart in Microsoft Transact SQL[/url]


 
Posted : 11/03/2009 3:34 pm
Posts: 0
Free Member
 

Pretty ugly and inefficient query performing wildcard searches on character fields. But if you're stuck with a poorly designed database schema then what can you do 😀


 
Posted : 11/03/2009 3:36 pm
 DezB
Posts: 54367
Free Member
Topic starter
 

[i]andywhit - Member
Pretty ugly and inefficient query performing wildcard searches on character fields. But if you're stuck with a poorly designed database schema then what can you do
[/i]

Ugly as hell, but that's Microsoft Active Directory for ya. [/geek=off]


 
Posted : 11/03/2009 3:38 pm
Posts: 0
Free Member
 

I am useing MS access 😳 yes i know...

but my code is now

SELECT Patient.Provider, Count(Patient.[Date entered]) AS [Patients Assigned]
FROM Patient
WHERE Right(Left(Patient.[Date entered],5),2)=DatePart("m",(DateAdd("m",-5,Now()))) [b]And Right(Patient.[Date entered],2)=FORMAT(Now(),'YY')[/b]
GROUP BY Patient.Provider;

It will just not display any results 🙁

The bolded part is where i am having the problem...


 
Posted : 11/03/2009 3:52 pm
Posts: 31206
Full Member
 

What's with all the Left, Right stuff??

Is Patient.[Date entered] a text string? If so, why isn't it a Date??


 
Posted : 11/03/2009 4:02 pm
Posts: 0
Free Member
 

I am not to sure how to convert it to a date, because MS Access does not use CONVERT ?

Patient.[Date entered] is a text string.


 
Posted : 11/03/2009 5:48 pm
Posts: 31206
Full Member
 

Right, there is your first problem. Why is something called "[u]Date[/u] entered" being stored as text??

That rather points to a larger problem with your system design. What if "Date entered" is "25/12/2007", "12/25/2007", "2007/12/25", "Monday", "last week" etc etc etc

The application should be dealing with what the user enters. By the time it gets to the database it should be in a sane format.


 
Posted : 11/03/2009 5:53 pm
Posts: 0
Free Member
 

The problem is that I have inherited the database and there is around 30,000 records so I guess the date format in MS Access Database is going to stop at DD/MM/YYYY 🙁

I know it is not the best database design practised.


 
Posted : 11/03/2009 6:08 pm
Posts: 17
Free Member
 

I had all sorts of problems with a column called "date" - boy did it screw up SQL, it got its knickers in a twist due to it conflicting with a function.


 
Posted : 11/03/2009 6:10 pm
Posts: 0
Free Member
 

UPDATE EMP
SET SALARY = '1000000'
WHERE EMP_NAME = 'tommo';

Job Done.


 
Posted : 11/03/2009 6:14 pm
Posts: 45
Free Member
 

A column called date?! I'm an Oracle developer and had to develop using SQL Server once; was an upsetting experience for me....


 
Posted : 11/03/2009 6:16 pm
Posts: 31206
Full Member
 

You could change the database design pretty easily and migrate the data - it would just depend how much other code expected that column to be text.

But if you're stuck with it..

The easiest way to figure out what's going on here is to try another query:

[code]select Right(Patient.[Date entered],2), FORMAT(Now(),'YY')
from Patient[/code]

..will show you what you are actually trying to compare. The most obvious thing I can see it that I think it should be [code]'yy'[/code] and not [code]'YY'[/code]


 
Posted : 11/03/2009 6:17 pm
Posts: 0
Free Member
 

SELECT * FROM EMP WHERE EMP_NAME = 'tommo';

EMP_NAME SALARY SALARY_CURRENCY_CODE
tommo 1000000 ZWL

SELECT * FROM CURRENCY WHERE CURRENCY_CODE = 'ZWL';

CURRENCY_CODE DESCRIPTION
ZWL ZIMBABWEAN DOLLAR

Pwned!


 
Posted : 11/03/2009 8:04 pm
Posts: 54
Free Member
 

Nezbo, if its an Access query you could prolly use CDate(String) in the statement. Then use date functions on the value. Thou probably best to check if the CDate is creating the right 'Date' first.


 
Posted : 11/03/2009 8:29 pm
Posts: 0
Free Member
 

WAhooooooooooooo got it working with the cDate 🙂 Cheers damitamit 🙂

SELECT Patient.Provider, Count(Patient.[Date entered]) AS [Patients Assigned]
FROM Patient
WHERE DatePart("m",cDate(Patient.[Date entered]))=DatePart("m",(DateAdd("m",-1,Now()))) AND DatePart("yyyy",cDate(Patient.[Date entered]))=DatePart("yyyy",Now())
GROUP BY Patient.Provider;

Cheers,
Nezbo


 
Posted : 12/03/2009 9:55 am
Posts: 31206
Full Member
 

So is now a good time to point out that this query won't work in January? 🙄


 
Posted : 12/03/2009 10:03 am
Posts: 54
Free Member
 

lol, indeed


 
Posted : 12/03/2009 12:13 pm