My Assistant
![]() ![]() |
|
|
Jan 20 2012, 06:19 PM
Post
#1
|
|
|
UtterAccess Guru Posts: 858 |
Hello All,
I'm trying to write a sql statement and i'm having trouble dealing with a date field that is stored as text. An example of how it is stored is this, '20110916161946'. It is YYYYMMDDHHMMSS How would I go about converting that field into a date, and pulling dates that are >=Date - 30 based on that field? Thanks, UtterAccess |
|
|
|
Jan 20 2012, 06:52 PM
Post
#2
|
|
|
UtterAccess Editor Posts: 15,974 From: Northern Virginia, USA |
You could create an expression column that returns a date typed result with this ...
CDate(Format([yourTextFieldThatLooksLikeADate],"0000-00-00 00:00:00")) Then use the criteria of ... >= DateAdd("d",-30,Date()) This will result in a WHERE clause that looks like this: WHERE CDate(Format([yourTextFieldThatLooksLikeADate],"0000-00-00 00:00:00")) >= DateAdd("d",-30,Date()) |
|
|
|
Jan 20 2012, 09:05 PM
Post
#3
|
|
|
UA Admin Posts: 19,246 From: Newcastle, WA |
You've posted this in the SQL Server forum, so I assume you are looking for a T-SQL solution.
I think that would be Cast(left('20110916161946',4) + '/' +SUBSTRING('20110916161946',5,2)+ '/'+ SUBSTRING('20110916161946',7,2)+ ' ' + SUBSTRING('20110916161946',9,2)+':' + SUBSTRING('20110916161946',11,2) +':' + SUBSTRING('20110916161946',13,2) as DATEtime) |
|
|
|
Jan 20 2012, 09:07 PM
Post
#4
|
|
|
UtterAccess Editor Posts: 15,974 From: Northern Virginia, USA |
Oh ... sorry SQL Server ... (IMG:style_emoticons/default/dazed.gif)
Something like this should do the trick (at least with SQL Server 2008 or higher): ... WHERE CAST(LEFT([yourTextDateField],8) as SMALLDATETIME) >= DATEADD(dd,CAST(GETDATE() AS INT) - 30,0) |
|
|
|
Jan 23 2012, 05:10 AM
Post
#5
|
|
|
UtterAccess Editor Posts: 13,753 From: England (North East / South Yorks) |
2005 too. (Can't say about earlier.)
|
|
|
|
Jan 24 2012, 03:55 PM
Post
#6
|
|
|
UtterAccess Guru Posts: 858 |
I'm not sure if i'm doing something wrong but i'm trying to apply your solutions with no luck. Here is my current sql statement, can someone help me apply the solutions>? PSPEC.PSRCVDT being the date field i'm using.
SELECT PDEM.PID2, PSPEC.PSID2, PSPEC.PSPECNO, PSPEC.PSRCVDT FROM PDEM INNER JOIN PSPEC ON (PDEM.PUID = PSPEC.PUID) AND (PDEM.PEFDT = PSPEC.PEFDT) GROUP BY PDEM.PID2, PSPEC.PSID2, PSPEC.PSPECNO |
|
|
|
Jan 24 2012, 05:05 PM
Post
#7
|
|
|
UtterAccess Editor Posts: 15,974 From: Northern Virginia, USA |
You could either put a WHERE clause prior to your GROUP BY clause or you can put a HAVING clause after your GROUP BY clause. In this case, I like the use of a WHERE clause.
SELECT <your field list> FROM <your joined tables> WHERE <an expression in the form I showed above> GROUP BY <your group by list> {note that the SELECT field list should ideally match the GROUP BY list -- at least for non-aggregated columns} |
|
|
|
Jan 24 2012, 05:11 PM
Post
#8
|
|
|
UtterAccess Guru Posts: 858 |
I do this but i still get dates that are older than 30 days ago???
SELECT PDEM.PID2, PSPEC.PSID2, PSPEC.PSPECNO, PSPEC.PSRCVDT, CAST(LEFT(PSPEC.PSRCVDT,8) as SMALLDATETIME) FROM PDEM INNER JOIN PSPEC ON (PDEM.PUID = PSPEC.PUID) AND (PDEM.PEFDT = PSPEC.PEFDT) WHERE CAST(LEFT(PSPEC.PSRCVDT,8) as SMALLDATETIME) >= DATEADD(dd,CAST(GETDATE() AS INT) - 30,0) GROUP BY PDEM.PID2, PSPEC.PSID2, PSPEC.PSPECNO This post has been edited by ry94080: Jan 24 2012, 05:18 PM |
|
|
|
Jan 24 2012, 06:29 PM
Post
#9
|
|
|
UtterAccess Editor Posts: 15,974 From: Northern Virginia, USA |
Can you give us a sample dump of the data ... in Excel/Access/CSV or something, so we can try to duplicate your experience?
|
|
|
|
Jan 25 2012, 02:06 PM
Post
#10
|
|
|
UtterAccess Guru Posts: 858 |
i got it to work with this:
SELECT PDEM.PID2, SUBSTRING(PDEM.PID2, 3) as MRNumber, PSPEC.PSID2, PSPEC.PSPECNO, PSPEC.PSRCVDT, CAST(LEFT(PSPEC.PSRCVDT,8) as SMALLDATETIME) FROM PDEM INNER JOIN PSPEC ON (PDEM.PUID = PSPEC.PUID) AND (PDEM.PEFDT = PSPEC.PEFDT) WHERE CAST(LEFT(PSPEC.PSRCVDT,8) as SMALLDATETIME) >= DATEADD(d, -30, getdate()) GROUP BY PDEM.PID2, PSPEC.PSID2, PSPEC.PSPECNO |
|
|
|
Jan 26 2012, 09:01 AM
Post
#11
|
|
|
UtterAccess Editor Posts: 15,974 From: Northern Virginia, USA |
Awesome! Glad to here of your success! (IMG:style_emoticons/default/thumbup.gif)
|
|
|
|
Jan 26 2012, 02:13 PM
Post
#12
|
|
|
UtterAccess Editor Posts: 15,974 From: Northern Virginia, USA |
Whoa!! ...
<<< CAUTION >>> This ... WHERE ... >= DATEADD(d, -30, getdate()) will yeild a time component, where as this ... CAST(LEFT(PSPEC.PSRCVDT,8) as SMALLDATETIME) Will not. ----- So, I would suggest that you work with the expression I provided ... but I did notice, the casting to INT did not TRUNCATE as I would have expected, so use FLOAT instead .. DATEADD(d, CAST(getdate() AS FLOAT) - 30,0) or ... DATEADD(d, CAST(getdate() AS FLOAT), -30) |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 23rd May 2013 - 01:59 PM |