March 5, 2013 at 2:20 am
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;'
March 5, 2013 at 3:10 am
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())
March 5, 2013 at 3:12 am
replace >
with >
March 5, 2013 at 3:17 am
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
March 5, 2013 at 3:35 am
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());
March 5, 2013 at 3:41 am
thanks a lot!
March 5, 2013 at 3:41 am
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())
March 5, 2013 at 4:38 am
thanks a lot!
March 5, 2013 at 4:49 am
cms9651 (3/5/2013)
Extract only the last three days
Anything from saturday+sunday+monday, or anything since 72 hours ago from now?
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
March 5, 2013 at 6:36 am
Anything since 72 hours ago from now.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply