SQL - Query Improvement MAX(date), IN

Jul 23, 2005

Hi,
I wonder if you could shed some light into this.

I have the following table.
Id, ContentId, VersionDate, ContentXml

There are several ContentIds in the table.

SELECT *
FROM tblVersions
WHERE (VersionDate =
(SELECT MAX(tblVersions2.VersionDate)
FROM tblContentVersion AS
tblVersions2
WHERE tblVersions2.ContentiD =
tblVersions.ContentiD))
ORDER BY ContentId


This query works to select the latest versions (MAX) of every content,
but I do not like it, any other way to do this properly?

I also want to do this knowing a set of ids (probably using IN )

SELECT *
FROM tblVersions
WHERE (VersionDate =
(SELECT MAX(tblVersions2.VersionDate)
FROM tblContentVersion AS
tblVersions2
WHERE tblVersions2.ContentiD =
tblVersions.ContentiD AND tblVersions.ContentiD IN (1, 2, 3, 6, 7, 8)
))
ORDER BY ContentId


Any ideas for improvements on this query?

ContentXml is of ntext type

Thanks,
/ jorge

View 4 Replies


ADVERTISEMENT

Query Improvement

Nov 14, 2007

In our business we receive debt from clients that we work in order to try and reclaim some of the debt on their behalf. When a debt file comes in from a client it is loaded onto the system and every entry in that debt file is stamped with the same batch id. Using the batch id we track each debt file and monitor how well we are working that debt file. So as an example we could receive a debt file with 100 records in it each of these records would be stamped with a batch id of say 100001, the next file that is loaded onto the system, each record would then be stamped with 100002. so we can track how each batch is doing by grouping by batch id.

I have written my query that basically groups all the accounts on the system by batch id. This is fine when I only want to return totals and sums in the select list. The way I have the written the query If I want to calculate the payments we have received for each batch or the commission we will make on the payments (or any other info per batch) I have to use subquerys in the select list using the batchid I have grouped by . Is this the best/only way to achieve what I want to do.


Any input most welcome.



sql below:




Select c.Name,ib.ImportBatchID BatchID,
---------------------------------------------------------------
CONVERT(VARCHAR(10),ib.UpdatedOn,103) LoadedOn,
---------------------------------------------------------------
Count(d.DebtID) Reg_Count, --No. Reg
---------------------------------------------------------------
Sum(d.DebtValue) Reg_Value, --Value Reg
---------------------------------------------------------------


(Select sum(da.Amount) from dbo.DebtAdjustment da WITH (NOLOCK), dbo.ImportBatchItem bi WITH (NOLOCK) where
bi.ItemID=da.DebtID And bi.ImportBatchID=ib.ImportBatchID) Adjustments,
---------------------------------------------------------------
(Select count(dh.DebtID) from dbo.Debthistory dh WITH (NOLOCK), dbo.ImportBatchItem bi WITH (NOLOCK) where --No. OBC's
bi.ItemID=dh.DebtID And dh.Note like 'OBC:%' And bi.ImportBatchID=ib.ImportBatchID) OBC_Num,
---------------------------------------------------------------
(Select count(dh.DebtID) from dbo.Debthistory dh WITH (NOLOCK), dbo.ImportBatchItem bi WITH (NOLOCK) where --No. ICC's
bi.ItemID=dh.DebtID And dh.Note like 'ICC:%' And bi.ImportBatchID=ib.ImportBatchID) ICC_Num,
---------------------------------------------------------------
(Select count(dh.DebtID) from dbo.ImportBatchItem bi WITH (NOLOCK), dbo.DebtHistory dh WITH (NOLOCK)
Where dh.DebtID=bi.ItemID AND bi.ImportBatchID=ib.ImportBatchID AND dh.UserName='Letter Server' AND dh.Note like '%Letter%') ItemsMailed,
---------------------------------------------------------------
Cast((Select sum(CASE
WHEN dp.ReceivedByID = 1
THEN dp.Amount * (tF.Rate /100)
WHEN dp.ReceivedByID = 2
THEN dp.Amount * (tD.Rate /100)
ELSE
dp.Amount * ((tF.Rate + tFe.Rate) / 100)
END)
From
dbo.DebtPayment dp JOIN dbo.Debt d ON d.DebtID=dp.DebtID
JOIN dbo.ImportBatchItem bi ON dp.DebtID=bi.ItemID

LEFT JOIN dbo.mTrackerFeeChange tF ON tF.ClientID=d.ClientID
AND tF.ContractID=d.ContractID AND dp.ReceivedByID=tF.RateType
AND (
(dp.PaymentOn >= tF.StartDate AND dp.PaymentOn <= tF.EndDate)
OR
(dp.PaymentOn >= tF.StartDate AND tF.EndDate IS NULL)
)

LEFT JOIN dbo.mTrackerDirectChange tD ON tD.ClientID=d.ClientID
AND tD.ContractID=d.ContractID AND dp.ReceivedByID=tD.RateType
AND (
(dp.PaymentOn >= tD.StartDate AND dp.PaymentOn <= tD.EndDate)
OR
(dp.PaymentOn >= tD.StartDate AND tD.EndDate IS NULL)
)

LEFT JOIN dbo.mTrackerFieldChange tFe ON tFe.ClientID=d.ClientID
AND tFe.ContractID=d.ContractID AND tFe.RateType=dp.ReceivedByID
AND (
(dp.PaymentOn >= tFe.StartDate AND dp.PaymentOn <= tFe.EndDate)
OR
(dp.PaymentOn >= tFe.StartDate AND tFe.EndDate IS NULL)
)
where bi.ImportBatchID=ib.ImportBatchID) AS decimal(10,2)) ComRate,

---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 1 AND Debt.OutstandingValue > 0) Girobank,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 2 AND Debt.OutstandingValue > 0) StandingOrder,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 3 AND Debt.OutstandingValue > 0) CreditCard,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 4 AND Debt.OutstandingValue > 0) DirectDebit,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 5 AND Debt.OutstandingValue > 0) Cheque,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 6 AND Debt.OutstandingValue > 0) PostalOrder,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 7 AND Debt.OutstandingValue > 0) Cash,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 8 AND Debt.OutstandingValue > 0) BankersDraft
---------------------------------------------------------------



From
dbo.Client c WITH (NOLOCK), dbo.Debt d WITH (NOLOCK), dbo.ImportBatchItem ib WITH (NOLOCK)
Where
c.ClientID=d.ClientID
AND
d.DebtID=ib.ItemID
AND
(@ClientID IS NULL OR c.ClientID = @ClientID)
Group by c.Name, ib.ImportBatchID,CONVERT(VARCHAR(10),ib.UpdatedOn,103),DATENAME(Month, ib.UpdatedOn) + ' ' + DATENAME(Year, ib.UpdatedOn)
Order by ib.ImportBatchID

View 2 Replies View Related

DTA: Expected Improvement 0%

Dec 31, 2007

I did a trace on a production DB for many hours, and got more than 7 million of "RPC:Completed" and "SQL:BatchCompleted" trace records. Then I grouped them and obtained only 545 different events (just EXECs and SELECTs), and save them into a new workload file.

To test the workload file, I run DTA just for 30 minutes over a restored database on a test server, and got the following:
Date 28-12-2007
Time 18:29:31
Server SQL2K5
Database(s) to tune [DBProd]
Workload file C:Tempfiltered.trc
Maximum tuning time 31 Minutes
Time taken for tuning 31 Minutes
Expected percentage improvement 20.52
Maximum space for recommendation (MB) 12874
Space used currently (MB) 7534
Space used by recommendation (MB) 8116
Number of events in workload 545
Number of events tuned 80
Number of statements tuned 145
Percent SELECT statements in the tuned set 77
Percent INSERT statements in the tuned set 13
Percent UPDATE statements in the tuned set 8
Number of indexes recommended to be created 15
Number of statistics recommended to be created 50
Please note that only 80 of the 545 events were tuned and 20% of improvement is expected if 15 indexes and 50 statistics are created.

Then, I run the same analysis for an unlimited amount of time... After the whole weekend, DTA was still running and I had to stop it. The result was:
Date 31-12-2007
Time 10:03:09
Server SQL2K5
Database(s) to tune [DBProd]
Workload file C:Tempfiltered.trc
Maximum tuning time Unlimited
Time taken for tuning 2 Days 13 Hours 44 Minutes
Expected percentage improvement 0.00
Maximum space for recommendation (MB) 12874
Space used currently (MB) 7534
Space used by recommendation (MB) 7534
Number of events in workload 545
Number of events tuned 545
Number of statements tuned 1064
Percent SELECT statements in the tuned set 71
Percent INSERT statements in the tuned set 21
Percent DELETE statements in the tuned set 1
Percent UPDATE statements in the tuned set 5
This time DTA processed all the events, but no improvement is expected! Neither indexes/statistics creation recomendation.

It does not seem that Tuning Advisor crashed... Usage reports are fine and make sense to me.

What's happening here? It looks like DTA applied the recomendations and iterated, but no new objects where found in DB.

I guess that recomendations from the first try with only 80 events were invalidated by the remaining from the long run.

I couldn't google an answer for this. Help!!!

Thanks in advance.

++Vitoco

View 1 Replies View Related

Improvement Over Subquery?

Feb 15, 2006

I am in the process of evaluating 2005 to upgrade a DTS and A/S solution for accounts receivable data imported from a legacy application on various AS/400s in our enterprise. Our current process uses staging and mart databases with views providing a layer of abstraction. Instead of using upgrade wizards, I would prefer to implement a "ground up" rearchitecture that better incorporates the 2005 toolset.

One of the challenges is building a fact table for historical a/r balances since the legacy application will only provide transactions, meaning that there is no data entry for a month where there might be a balance, but no transaction (payment, invoice, credit, etc) occurred. Our current implementation uses a date table as its base, perfoming a left join to the transaction table. I have to use subqueries to calculate previous balances, which is also used to calculate ending balance.

Should I replace the current view with data flow logic that would perform this calculation? If so, what would be the best method? A merge join? A calculated column? If it helps to provide sample schema snipets, I would be happy to do so.

View 1 Replies View Related

Improvement Sysdtslog90?

Apr 12, 2007

hello



im trying to create a ssis monitoring tool (using sysdtslog90, reporting services and system tables) including sql server agent ssis schedules/history. but im having some problems evaluating sysdtslog90. im using the built in SSIS logging without any custom logging tasks (i only log OnError and OnWarning) to keep the logging table as small as possible (one sysdtslog90 table for all packages running on the server).

and there is the problem:

there are some executions without any events except OnError. the source of this event is the name of the failed task, so how do i get the name and id of the failed package?



"normal" executions have "PackageStart" and "PackageEnd" where the sourceid and source are equal with the id and name of the package



i could add logging for OnPreValidate and catch the first line, because no package can fail before validation!?

but isnt there any better solution?

i would love it to have two more columns in sysdtslog90.. package id and name. would be much easier to evaluate sysdtslog90 :/



- paul



View 8 Replies View Related

Replication Performance Improvement

Oct 26, 1999

Can anyone suggest hwo can I improve the performance of the replication process and make it faster.
Pran

View 1 Replies View Related

DB2 64-bit Driver: Big Performance Improvement!

Aug 31, 2007

After reading an article today on SQL Server Central about choosing the best connectors for ssis (obviously written to drive sales, but hey it looks like it's working) I downloaded an evaluation copy of DataDirect's 64-bit DB2 driver and started some preliminary testing.

http://www.sqlservercentral.com/columnists/mfrost/3209.asp

Here are some inital findings/opinions:

-Rather easy to install (downloaded, briefly read documentation, and was up and running in about 10 mintues)
-With Microsoft's IBM OLE DB provider for DB2 (32-bit) we can extract 1 million records in approx. 9 minutes
-With the 64-bit driver we can extract 1 million records in approx. 2.25 minutes! 4 times as fast as the 32-bit driver!
-Was unable to get the driver to work through linked server. Tech support opened an issue to look into it.
-Sales rep didn't have exact pricing, but she thought they charged per core.
-DataDirect also has 64-bit Oracle and Sybase drivers available.

For those of you extracting large amounts of data from DB2, Oracle, or Sybase with SSIS and are looking to improve performance I'd recommend at least checking this product out.

Also, I'd be interested to hear if anyone else started testing this or any other 64-bit driver for DB2.

View 2 Replies View Related

How Can I Use Dual Core CPU For Performance Improvement

Jul 17, 2007

Hi,



I am running MSSQL 2005 Standard edition on a two processor Intel Xeon 3GHz (dual-core) with 8GB RAM.



I notice in "Windows task manager CPU performance" while running a long SQL statement (takes 1.5 hours), only 1 logical (out of 4) is utilised at >70%. The remaining 3 logical processors hover around 10%



Using Performance monitor, the average read queue, write queue, and pages/sec also hovers around 25%, indicating no heavy physical disk/memory loading.



How can I set to utilise more physical/logical processor to improve the MSSQL performance ?



Thanks.

View 2 Replies View Related

SQL Tools :: How To Disable Customer Improvement Program From Menu For Server MGMT Tools 2012

Mar 25, 2014

Am customizing SQL server MGMT tools 2012 for Mass deployment.Client had asked to remove Customer Feedback option from help menu.how to disable that.

View 6 Replies View Related

Transact SQL :: How To Write A Query To Get Current Date Or End Of Month Date

Sep 29, 2015

how to write a query to get current date or end of month date if we pass year and month as input

Eg: if today date is 2015-09-29
if we pass year =2015 and month=09 then we have to get 2015-09-29
if we pass year =2015 and month=08 then we have to get 2015-08-31(for previous months we have to get EOMonth date & for current month we have to get current date).

View 3 Replies View Related

Query Help - Giving A Date Range Given The Start Date, Thanks!

Jul 23, 2005

Hi Group!I am struggling with a problem of giving a date range given the startdate.Here is my example, I would need to get all the accounts opened betweeneach month end and the first 5 days of the next month. For example, inthe table created below, I would need accounts opened between'5/31/2005' and '6/05/2005'. And my query is not working. Can anyonehelp me out? Thanks a lot!create table a(person_id int,account int,open_date smalldatetime)insert into a values(1,100001,'5/31/2005')insert into a values(1,200001,'5/31/2005')insert into a values(2,100002,'6/02/2005')insert into a values(3,100003,'6/02/2005')insert into a values(4,100004,'4/30/2004')insert into a values(4,200002,'4/30/2004')--my query--Select *[color=blue]>From a[/color]Where open_date between '5/31/2005' and ('5/31/2005'+5)

View 2 Replies View Related

SQL Query Filtering Date Field By Today's Date?

Nov 3, 2006

Can someone tell me sql query for filtering date field for current day,not last 24hours but from 00:00 to current time?

View 2 Replies View Related

Date Query (pulling System Date)

Nov 1, 2013

I'm currently using the SQL to find records older than todays date in the SSD_SED field. I'm having to update the date manually each day. Is there a way I can automate this?

AND (SSD_SED < '2013-11-01')order by SSD_SED DESC

View 3 Replies View Related

SQL Server 2008 :: Query To Select Date Range From Two Tables With Same Date Range

Apr 6, 2015

I have 2 tables, one is table A which stores Resources Assign to work for a certain period. The structure is as below

Name StartDate EndDate
Tan 2015-04-01 08:30:00.000 2015-04-01 16:30:00.000
Max 2015-04-01 08:30:00.000 2015-04-01 16:30:00.000
Alan 2015-04-01 16:30:00.000 2015-04-02 00:30:00.000

The table B stores the item process time. The structure is as below

Item ProcessStartDate ProcessEndDate
V 2015-04-01 09:30:10.000 2015-04-01 09:34:45.000
Q 2015-04-01 10:39:01.000 2015-04-01 10:41:11.000
W 2015-04-01 11:44:00.000 2015-04-01 11:46:25.000
A 2015-04-01 16:40:10.000 2015-04-01 16:42:45.000
B 2015-04-01 16:43:01.000 2015-04-01 16:45:11.000
C 2015-04-01 16:47:00.000 2015-04-01 16:49:25.000

I need to select the item which process in 2015-04-01 16:40:00 and 2015-04-01 17:30:00. Beside that I need to know how many resource is assigned to process the item in that period of time. I only has the start date is 2015-04-01 16:40:00 and end date is 2015-04-01 17:30:00. How I can select the data from both tables. There is no need for JOIN, just seperate selections.

Another item process time is in 2015-04-01 10:00:00 and 2015-04-04 11:50:59.

The result expected is

Table A

Name StartDate EndDate
Alan 2015-04-01 16:30:00.000 2015-04-02 00:30:00.000

Table B

Item ProcessStartDate ProcessEndDate
A 2015-04-01 16:30:10.000 2015-04-01 16:32:45.000
B 2015-04-01 16:33:01.000 2015-04-01 16:35:11.000
C 2015-04-01 16:37:00.000 2015-04-02 16:39:25.000

Scenario 2 expected result

Table A

Name StartDate EndDate
Tan 2015-04-01 08:30:00.000 2015-04-01 16:30:00.000
Max 2015-04-01 08:30:00.000 2015-04-01 16:30:00.000

Table B

Item ProcessStartDate ProcessEndDate
Q 2015-04-01 10:39:01.000 2015-04-01 10:41:11.000
W 2015-04-01 11:44:00.000 2015-04-01 11:46:25.000

View 8 Replies View Related

Get Max Date Query

Nov 16, 2006

I have the following 4 rows in a table
Company   JobNumber       BeginDate          ModifyDate
1                 2                   12/12/2005         11/12/2006
1                 2                   12/12/2005         11/15/2006  
2                 3                   11/12/2005         1/12/2006
2                 3                   11/12/2005         9/15/2006
The company and Job Number make up the key so yes this table has duplicate keys.  My question is how would I return the two keys with the max modify date?
So the results would look like this:
1                 2                   12/12/2005         11/15/2006
2                 3                   11/12/2005         9/15/2006    
Thanks,
 
  
 

View 2 Replies View Related

Date Query

Mar 9, 2007

Good Morning to all
 
I wrote a query to access some data from sql server ,my query is as follows
strsel = "select * from schedule where sector_id='" & str_sec_id & "' And dep_date = " & bb & ""
bb is the date transferred from the other module
i want to check the dep_date as short date format.because bb is in short date format.
in the above checking i am not getting the query result
please look on this code
Regards
Unni

View 2 Replies View Related

Sql Max(date) Query Help

Apr 10, 2007

I have a single table with the following columns:  

rowid  (pk)
whenloaded
tablename
records
 comments
 I want to write a query to get the max(whenloaded)  date and then each individual related column corresponding to that date IE. tablename, records, comments 
Query I have now - Select a.tablename, max(a.whenloaded) From dbo.table_load_tracking a  group by a.tablename
 This returns the list that I desire although I am not sure how to add the additional columns and maintain the origional listed max(whenloaded).
I would like to understand the concept behind building a query like this so a detailed explanation would be greatly appreciated.
Thank You,
Fullyii

View 1 Replies View Related

SQL Query By Date

Apr 7, 2005

I have a table with the following structure: FacilityID, DepartmentID, NumberBeds, DateCreated. I record gets added per FacilityID-DepartmentID. Not every facility and every department within the facility needs to have an entry and there can me multiple entries on the same date. I want to create a pivot table of sorts, which I think I can do, as well as the joins to get the Facility Name and Department Name from other tables.
What I really am having problems figuring out right now is how to pull the last entered date per FacilityID-DepartmentID. Here is a sample of what the data looks like in the database and what I want to pull:
Hospital1    Cardiology       1    4/6/05 4:24 PMHospital1    Critical Care     8    4/6/05 4:24 PMHospital1    Med-Surg        3    4/6/05 4:24 PMHospital1    Pediatrics         0    4/6/05 4:24 PMHospital1    Psychiatry         0    4/6/05 4:24 PMHospital1    Telemetry          0    4/6/05 4:24 PM  Hospital1    Cardiology       8       4/7/05 9:04 AMHospital1    Critical Care    6       4/7/05 9:04 AMHospital1    Pediatrics         4       4/7/05 9:04 AMHospital1    Psychiatry        0       4/7/05 9:04 AM
I need to have as output (items in red above):Hospital   Card  CritCare  Med-Surg  Peds  Psych   Telem     Last UpdatedHospital1   8          6                -           4          0         -         4/7/05 9:04 AM
I'm truly stumped on this one and any help, examples or links to documentation would be greatly appreciated!  

View 1 Replies View Related

Date Query

Sep 19, 2001

Hi All

Could someone please give me some guidance , I think I have totally lost it.

I am trying to get an age from a dateofbirth field.
1.This is what I have done :

select datepart(year,getdate())- datepart(year,cast(dateofbirth as datetime))
from table

The values are all correct expect everything that has a year of 1949 or below.
This comes thru as 2049 , 2048 , 2047 etc instead of 1949 etc.
I am converting the dateofbirth field to a datetime as this is orginally a character field.

Why would this be happening?

2.If I use this sort of query :

select dateofbirth , datepart(year,getdate())- substring(dateofbirth,1,2)
from table

I get the right age result but in this format - 1948 , 1951, which could then just use the last two digits for the age.

Would there be a simpler way of doing this?

Thanks in advance

Tanya.

View 4 Replies View Related

How Do You Query By Date??

Sep 22, 1999

The user enters in a date which is a varchar type and I'm trying to query the
database for all the birthdays of that date. The problem with SQL server is that when you do a search by the date, it makes a match with the timestamp of 12:00AM. So, if you search by a certain date, it also does a search by the time.
If John's birthday in the database is 'Jan 1 1999 08:00AM' and I use this

Select * from people where birthday = @UserInputDate,

the query returns NOTHING because the defualt time is 12AM and the date on that birthday is 08:00AM.

The only way I can get it to return ALL the birthdays of 1/1/1999 is with this statement:

select * from people where birthdate like 'Jan 1 1999 %:%'

But how do I convert the '1/1/1999' to the Jan 1 1999 format and append '%:%' at the end?

I've tried variations of the following:

select * from people where birthdate like @UserInputDate + ' %:%' but for some
reason that only appends one percentage sign '%:' and not '%:%'

Help!
Angel

View 3 Replies View Related

Between Date Query Help.

Dec 3, 2004

Trying to perform a query where it will return all cases between the current day and the first day of the same month, regardless of whether it is this month or 6 months down the road.

WHERE Response_Flag = '1' AND Begun_By_Id = '" & Session("Emp_Id")& "' AND Begin_Date Between GetDate AND ???????

Any help would be greatly appreciated

Chris

View 1 Replies View Related

SQL Date Query

Sep 27, 2005

I have to run a sql query to find out whether an employee has a birthday during the month of September. The date is in the format dd-mmm-yyyy (no time) and no matter what I do I can't get it to give me an answer.

So far I've tried LIKE, BETWEEN, >, <. Appreciate any help.

Thanks.

View 4 Replies View Related

Date Query

Nov 24, 2005

how would i query the table with dateid for a particular date

DateID
2005-11-24 14:40:00
select * from tbl1 where dateid like '2005-11-24%' ?

how about date and time like 9-11am of something

View 9 Replies View Related

Need Help In Query Using Date

Jan 11, 2006

Hi!

I have a DB with this fields:
MID - primary key
EVENT - varchar(2)
EVENT_DATE datetime (format mm/dd/yyyy)

I need to design a query that will pickup records from DB with EVENT = '02' and EVENT_DATE equal or earlier than 18 days from the date the query was executed.

In this example, the query should display these 2 records if ran today:
MID EVENT EVENT_DATE
01 02 12/25/2005
02 02 12/20/2005

But not this:

MID EVENT EVENT_DATE
03 03 12/25/2005
04 02 01/01/2006

Any help will be greatly appreciated.

View 8 Replies View Related

Date Query

Jun 19, 2007

Hi all:

I am trying to get data that wasn't used in the last six month. Created_on (date field)

select Category_tree_value, Created_on from workitem
where Created_on (field was used from the last six months)

Created_on format (2005-10-01 00:00:00.000)


Thanking everyone in advance.

Lystra

View 1 Replies View Related

Date Query

May 17, 2008

Hi

I have two regarding questions regarding queries involving dates.

I have a number of tables in the database i am using, one of which holds date periods of a financial year so it looks like something below:

DatePeriod:
ID, Begin, End
Jan08,01/01/2008,25/01/2008
Feb08,01/02/2008,29/02/2008
Mar08,03/03/2008,28/03/2008
...
...

The tables actual data begins from Jan07.

Another table shows transactions made, something which looks like:

Transactions:
ID, DateMade, CustomerID, ProductID
22,24/01/2008,111,222
23,27/01/2008,117,222
24,27/02/2008,011,233
...
...

The first query I am trying to do is to perform a count of all transactions made during a financial month period as specified by the user. So i guess I would use a variable such as @month which links to the DatePeriod table. This is what I'm not sure on how to do.

The second query is performing a count of all the transactions made within the last 12 months of the month specified by the user.

Can anyone help with this?

Thanks

View 3 Replies View Related

Date Query Help

Jun 3, 2008

I had data that went in wrong

the date in the db went in as

2008-01-06 08:54:18.000

when it should have been 2008-06-01 08:54:18.000

is there an sql statement that i can run to just change all these records to the reverse - meaning to change the date from 01-06 to 06-01
and 02-06 to 06-02 leaving the time as is.

can someone help me with this?

View 5 Replies View Related

MAX Date Sub-query

Nov 19, 2013

I am trying to write a query pulling the last te_event from a table by the latest te_date for a set of records. My query is below

SELECT tickets.t_number, tickets.t_reference,ticket_events.te_event, ticket_events.te_date, FROM ticket_events INNER JOIN
tickets ON ticket_events.te_system_ref = tickets.t_number INNER JOIN
warrants_due ON tickets.t_number = warrants_due.wd_system_ref

[code]...

There are 25,000 tickets in the subquery. I can't work out where to put the MAX (ticket_events.te_date) statement.

View 13 Replies View Related

Date Query

Dec 19, 2005

Hello, this is my first time with SQL.
I have a Mysql database of 3 tables
1)CUSTOMER (cust#, name, address, tel#, etc)
2)PRODUCT (prod#, desc, price, etc)
3)BOOKINGS (cust#, prod#, order_date)

I also have an admin page with options to display:
1)all bookings
2)bookings for next 7 days
3)bookings for next 14 days

How do I query the database to show the cust#, prod# and order_date from the BOOKINGS table, and the name, address, price from the CUSTOMER table for the three date options above?
I think I need to join two tables within a SELECT statement but am stuck on how to use todays date in the query.
Many thanks
Clam

View 3 Replies View Related

Date Query

Sep 3, 2007

Hi all :),

I am trying to do a query so that i can identify an inactive customer. Now if the customer shopped in Jan 2005 and did not shop in 2006 he/she would be deemed as inactive.I have no idea where to start does any one have any ideas???

View 2 Replies View Related

Date Only Query

Oct 24, 2007

Ive been trying to pull some data from a table by date and time based on todays date but im having a few probs.

i have managed to convert the datetime to just date but im still stuck.


Here is my query

Select DATEADD(dd, 0, DATEDIFF(dd, 0, FILE_DATE_TIME)) as DL_Date, NONE63 As RAcc, REF_1, CUSTOMER_NAME, NONE5 as DOB
From D.dbo.TORY
Where DL_Date = DATEADD(dd, 0, DATEDIFF(dd, 0, getdate()))

i have also tried

Select DATEADD(dd, 0, DATEDIFF(dd, 0, FILE_DATE_TIME)) as DL_Date, NONE63 As RAcc, REF_1, CUSTOMER_NAME, NONE5 as DOB
From D.dbo.TORY
Where DATEADD(dd, 0, DATEDIFF(dd, 0, FILE_DATE_TIME)) = DATEADD(dd, 0, DATEDIFF(dd, 0, getdate()))


Please help

View 3 Replies View Related

Date Query

Jan 21, 2008

Afternoon all !!!

Was wondering if somebody could help me am trying to write a sql query, but i'm trying to do is run a searh with a where cluase that only brings back data for the current month. And every other time i run it it will do the same. I know i can use this for results from yesterday "cast(convert(varchar(8),getdate()-1,1) as datetime)"

Any help would be great.

Lee

View 2 Replies View Related

Date - Query

Mar 3, 2008

Hello folks,

i have one table salestable in which tehre is field -salesdate
in this column sometimes date does not exist..but still i want output of that datecolumn.

id no num salesdate salesquantity
-------------------------------------
1 101 s101 1/1/2008 50.00
2 501 s501 1/5/2008 100.00
3 611 s611 2/3/2008 150.00

so i want results like:

id no num salesdate salesquantity
-------------------------------------
1 101 s101 1/1/2008 50.00
- - - 1/2/2008 -
- - - 1/3/2008 -
- - - 1/4/2008 -
2 501 s501 1/5/2008 100.00

----------------- and so on...

can you help me to wrie this query?
thanks.

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved