SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trouble with date conversion


Trouble with date conversion

Author
Message
jcobb 20350
jcobb 20350
Old Hand
Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)

Group: General Forum Members
Points: 384 Visits: 143
I want to match the year field in a table and pull out all that are a year old. I'm trying to use the GETDATE and look back 1 year but I'm missing something in the conversion. Here's my script. I get an error "Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric."

SELECT ih.customer_id
, ih.invoice_no
FROM invoice_hdr ih
WHERE ih.period = 12
AND ih.year_for_period = CONVERT(varchar(10),DATEADD(YEAR, -1, GETDATE()),101)
J Livingston SQL
J Livingston SQL
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27132 Visits: 41336
jcobb 20350 (7/13/2013)
I want to match the year field in a table and pull out all that are a year old. I'm trying to use the GETDATE and look back 1 year but I'm missing something in the conversion. Here's my script. I get an error "Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric."

SELECT ih.customer_id
, ih.invoice_no
FROM invoice_hdr ih
WHERE ih.period = 12
AND ih.year_for_period = CONVERT(varchar(10),DATEADD(YEAR, -1, GETDATE()),101)


any good?

ih.year_for_period = DATEPART(yy,DATEADD(YEAR, -1, GETDATE()))

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

jcobb 20350
jcobb 20350
Old Hand
Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)

Group: General Forum Members
Points: 384 Visits: 143
No. Now I get the error "Msg 156, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'SELECT'." which I'm sure has to do with the SELECT in your suggestion.
J Livingston SQL
J Livingston SQL
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27132 Visits: 41336
remove the "SELECT"

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

lshanahan
lshanahan
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3219 Visits: 438
I'm kind of curious why you're converting to a character string. The error message seems to indicate the ih.year_for_period field is numeric.

____________
Just my $0.02 from over here in the cheap seats of the peanut gallery - please adjust for inflation and/or your local currency.
jcobb 20350
jcobb 20350
Old Hand
Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)Old Hand (384 reputation)

Group: General Forum Members
Points: 384 Visits: 143
J Livingston SQL (7/15/2013)
remove the "SELECT"

Perfect. That's what I wanted, just couldn't figure it out without a little help. Thanks.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search