Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Extract only the last three days Expand / Collapse
Author
Message
Posted Tuesday, March 5, 2013 2:20 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, February 20, 2014 9:07 AM
Points: 52, Visits: 131
Extract only the last three days

hi all, hope in your help.

this is my procedure for export in txt file the values of the table in db sql server 2008.

the table is on a remote server and the field [myDateString] is nvarchar 255 and I'm not admin.

I need extract only the last three days for the table, in this moment extract all current year.

Can you help me ?
thank you
 EXEC master.dbo.sp_configure 'show advanced options',
1 RECONFIGURE EXEC master.dbo.sp_configure 'xp_cmdshell',
1 RECONFIGURE EXEC xp_cmdshell 'bcp "SELECT * FROM tbl WHERE YEAR(CAST(SUBSTRING([myDateString], 7, 4) AS DATETIME)) = 2013;"
queryout "\\myserver\public\tkt.txt" -T -c -t;'

Post #1426653
Posted Tuesday, March 5, 2013 3:10 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 6:17 PM
Points: 1,255, Visits: 1,743
Looks like your date data is stored as a string instead of a date data type. Can you post up some of the date string values?

The current query is finding the 7th character in the myDateString string, then grabbing the next 4 characters to use as a year string value, then converting it to a datetime data type. The year function gets the year value as an integer to compare to the 2013 integer on the right of the = sign.
SELECT * FROM tbl WHERE YEAR(CAST(SUBSTRING([myDateString], 7, 4) AS DATETIME)) = 2013;

Depending on what else is stored in the string, you could just use CONVERT(datetime, myDateString,103) > 3 days ago (getdate - 3)
SELECT * FROM tbl WHERE CONVERT(datetime, [myDateString], 103) > DATEADD(day, -3, getdate())



Post #1426681
Posted Tuesday, March 5, 2013 3:12 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 6:17 PM
Points: 1,255, Visits: 1,743
replace
> 

with >
Post #1426682
Posted Tuesday, March 5, 2013 3:17 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, February 20, 2014 9:07 AM
Points: 52, Visits: 131
thank you, but:

[SQL] SELECT * FROM tbl WHERE CONVERT(datetime, [myDateString], 103) > DATEADD(day, -3, getdate())

[Err] 22007 - [SQL Server]Conversion failed when converting date and/or time from character string.

the value of [myDateString] is e.g. 31/12/2012 09:01
Post #1426685
Posted Tuesday, March 5, 2013 3:35 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 6:17 PM
Points: 1,255, Visits: 1,743
Painful dealing with dates stored as strings..

You'll have to use the same method as the YEAR query.

SELECT * 
FROM tbl
WHERE CAST(SUBSTRING([myDateString], 1, 10) AS DATETIME) >= DATEADD(d, -3, getdate());

Post #1426697
Posted Tuesday, March 5, 2013 3:41 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, February 20, 2014 9:07 AM
Points: 52, Visits: 131
thanks a lot!
Post #1426700
Posted Tuesday, March 5, 2013 3:41 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 6:17 PM
Points: 1,255, Visits: 1,743
Oh just noticed I gave you the wrong syntax for that first reply (DAY instead of d)

[SQL] SELECT * FROM tbl WHERE CONVERT(datetime, [myDateString], 103) > DATEADD(d, -3, getdate())

Post #1426701
Posted Tuesday, March 5, 2013 4:38 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, February 20, 2014 9:07 AM
Points: 52, Visits: 131

thanks a lot!
Post #1426746
Posted Tuesday, March 5, 2013 4:49 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:32 AM
Points: 6,828, Visits: 14,068
cms9651 (3/5/2013)
Extract only the last three days



Anything from saturday+sunday+monday, or anything since 72 hours ago from now?


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1426753
Posted Tuesday, March 5, 2013 6:36 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, February 20, 2014 9:07 AM
Points: 52, Visits: 131
Anything since 72 hours ago from now.
Post #1426788
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse