Author |
Topic |
kennmurrah
Starting Member
13 Posts |
Posted - 2007-09-25 : 14:20:48
|
Greetings.I have a database with date stored in text format (YYYYMMDD). I need to select the date field and compute number of day from today.Current query isSELECT AROPNFIL_SQL.apply_to_no, uv_thomas_outstanding_invoices.amount, AROPNFIL_SQL.curr_doc_dt AS invoice_dateI need to add one more item to the selection, that being the number of days from curr_doc_dt till today.Any hope would be most appreciated.Kenn |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-25 : 14:24:23
|
You don't need to do anything fancy since YYYYMMDD is easily converted implicitly. select datediff(d, curr_doc_dt, getdate()) from yourtableTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
kennmurrah
Starting Member
13 Posts |
Posted - 2007-09-25 : 14:37:47
|
I tried that, but get "arithmetic overflow error converting expression to data type datetime" when I execute the following:SELECT AROPNFIL_SQL.apply_to_no, uv_thomas_outstanding_invoices.amount, AROPNFIL_SQL.doc_dt, DATEDIFF(d, AROPNFIL_SQL.doc_dt, GETDATE()) AS date_difference FROM AROPNFIL_SQL INNER JOIN uv_thomas_outstanding_invoices ON AROPNFIL_SQL.apply_to_no = uv_thomas_outstanding_invoices.apply_to_no |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-25 : 14:44:24
|
Could you post the min and max values of AROPNFIL_SQL.doc_dt?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
kennmurrah
Starting Member
13 Posts |
Posted - 2007-09-25 : 14:51:36
|
SELECT MIN(AROPNFIL_SQL.curr_doc_dt) AS minimumdate, MAX(AROPNFIL_SQL.curr_doc_dt) AS maximumdateFROM AROPNFIL_SQL INNER JOIN uv_thomas_outstanding_invoices ON AROPNFIL_SQL.apply_to_no = uv_thomas_outstanding_invoices.apply_to_noresults in 20061107 20070925 |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-25 : 14:55:44
|
You must have some data that doesn't fit into a datetime data type.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-09-25 : 14:58:25
|
You have a value in the table that is not a valid datetime.You can use this to search for it:SELECT curr_doc_dtFROM AROPNFIL_SQLwhere isnull(isdate(curr_doc_dt),0) <> 1 CODO ERGO SUM |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-09-25 : 14:59:44
|
kennmurrah -- and now you have just learned why you should always use the correct data types in your tables ....- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
kennmurrah
Starting Member
13 Posts |
Posted - 2007-09-25 : 15:00:18
|
No, I just checked that ... everything seems to be in the right format ...curr_doc_dt20070509200705092007050920070509200705092007051120070607200706072007061320070619200706192007062620070630200707122007072420070724200707242007073120070731200708102007081020070814200708142007081420070827200708312007083120070831200708312007083120070831200708312007083120070831200708312007083120070919200709192006110720070508200705082007050820070508200705082007050820070508200705082007051120070511200705112007051120070511200705112007051120070511200705112007051120070515200705152007051620070516200705162007051620070516200705252007052520070531200705312007053120070531200706072007060720070607200706072007060720070607200706072007060720070607200706072007060720070607200706262007062620070626200706282007062820070628200706282007062820070628200706282007062820070629200706292007062920070629200706302007063020070630200706302007063020070630200706302007063020070630200707162007071620070716200707162007071820070718200707182007071820070724200707242007072420070724200707312007073120070731200707312007081020070810200708102007081420070814200708272007082720070827200708272007082720070827200708312007083120070831200708312007083120070831200708312007083120070831200708312007083120070831200709192007091920070919200709192007091920070919200709192007091920070919200709242007092420070925200709252007092520070925200709252007092520070925 |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-09-25 : 15:05:41
|
So what happened when you ran the query I suggested?What is the actual datatype for the column?CODO ERGO SUM |
 |
|
kennmurrah
Starting Member
13 Posts |
Posted - 2007-09-25 : 15:07:33
|
I appreciate all your suggestions and your points are well taken. But in this case, I ran the suggested query and it returned zero rows ...this query:SELECT curr_doc_dtFROM AROPNFIL_SQLwhere isnull(isdate(curr_doc_dt),0) <> 1 |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-25 : 15:09:49
|
Put a SET DATEFORMAT YMD first in your query and try again. E 12°55'05.25"N 56°04'39.16" |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-09-25 : 15:13:16
|
It works fine for me:create table table1 (column1 char(8))insert into table1 values ('20070919')select datediff(d, column1, getdate())from table1drop table table1Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-09-25 : 15:14:12
|
What is the actual datatype of your column?Happens when you run this query?select convert(datetime,a.curr_doc_dt), datediff(Day,a.curr_doc_dt, GETDATE()) from ( select top 100 percent curr_doc_dt from AROPNFIL_SQL where isnull(isdate(curr_doc_dt),0) = 1 ) a CODO ERGO SUM |
 |
|
kennmurrah
Starting Member
13 Posts |
Posted - 2007-09-25 : 15:17:57
|
Okay, first of all, thanks to all of you for your help. Michael, your last post caused me to question my assumptions that it was a text field. Upon checking, I found an integer, instead. So the following worked:SELECT AROPNFIL_SQL.apply_to_no, uv_thomas_outstanding_invoices.amount, AROPNFIL_SQL.doc_dt, DATEDIFF(d, convert(char(8),AROPNFIL_SQL.doc_dt), GETDATE()) AS date_differenceFROM AROPNFIL_SQL INNER JOIN uv_thomas_outstanding_invoices ON AROPNFIL_SQL.apply_to_no = uv_thomas_outstanding_invoices.apply_to_noonce again, thanks to all of you.Kenn |
 |
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-09-26 : 00:38:39
|
Wow... all that just because someone didn't say what the datatype of the column was... --Jeff Moden |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-09-26 : 02:19:02
|
quote: Originally posted by Michael Valentine Jones What is the actual datatype of your column?Happens when you run this query?select convert(datetime,a.curr_doc_dt), datediff(Day,a.curr_doc_dt, GETDATE()) from ( select top 100 percent curr_doc_dt from AROPNFIL_SQL where isnull(isdate(curr_doc_dt),0) = 1 ) a CODO ERGO SUM
Like ISNUMERIC(). ISDATE() also should be dealt with careSELECT ISDATE(2007),ISDATE('2007'),ISDATE('2007-10-10'),ISDATE(2007-10-10) If the values are stored in YYYYMMDD format, you need not only to use ISDATE() but LEN() as wellSelect .........where ISDATE(col)=1 and len(col)=8MadhivananFailing to plan is Planning to fail |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-26 : 02:47:03
|
quote: Originally posted by Jeff Moden Wow... all that just because someone didn't say what the datatype of the column was...
He didquote: Originally posted by kennmurrah I have a database with date stored in text format (YYYYMMDD).
E 12°55'05.25"N 56°04'39.16" |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-09-26 : 02:55:27
|
quote: Originally posted by Peso
quote: Originally posted by Jeff Moden Wow... all that just because someone didn't say what the datatype of the column was...
He didquote: Originally posted by kennmurrah I have a database with date stored in text format (YYYYMMDD).
E 12°55'05.25"N 56°04'39.16" But it sounded that OP used char or varchar datatype than Integer MadhivananFailing to plan is Planning to fail |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-09-26 : 09:32:06
|
quote: Originally posted by madhivanan
quote: Originally posted by Peso
quote: Originally posted by Jeff Moden Wow... all that just because someone didn't say what the datatype of the column was...
He didquote: Originally posted by kennmurrah I have a database with date stored in text format (YYYYMMDD).
E 12°55'05.25"N 56°04'39.16"
But it sounded that OP used char or varchar datatype than Integer MadhivananFailing to plan is Planning to fail Sometimes, you have to be like a detective and start cross examining them when their story doesn’t hold together.CODO ERGO SUM |
 |
|
johnsql
Posting Yak Master
161 Posts |
Posted - 2007-09-27 : 11:13:28
|
quote: Originally posted by kennmurrah Okay, first of all, thanks to all of you for your help. Michael, your last post caused me to question my assumptions that it was a text field. Upon checking, I found an integer, instead. So the following worked:SELECT AROPNFIL_SQL.apply_to_no, uv_thomas_outstanding_invoices.amount, AROPNFIL_SQL.doc_dt, DATEDIFF(d, convert(char(8),AROPNFIL_SQL.doc_dt), GETDATE()) AS date_differenceFROM AROPNFIL_SQL INNER JOIN uv_thomas_outstanding_invoices ON AROPNFIL_SQL.apply_to_no = uv_thomas_outstanding_invoices.apply_to_noonce again, thanks to all of you.Kenn
I have a question here, why converting the column to the type of VACHAR(8) solves the problem while to datetime type doesn't?Thanks. |
 |
|
Next Page
|