Need to solve this problem, substring('PA-1501200', 4 , 99), sql thinks its a date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sl1ver
    New Member
    • Mar 2009
    • 196

    Need to solve this problem, substring('PA-1501200', 4 , 99), sql thinks its a date

    i got this
    Code:
    substring('PA-1501200', 4 , 99)
    now for some reason i think it sees the number as a date, it should be n varchar and then convert it into a int, then i get this error

    Conversion failed when converting datetime from character string.

    and here is my sql
    the ponumber will be the same as the grid number if i get it to work
    Code:
    declare @PONumber varchar(50)
    declare @GridNumber varchar(50)
    
    
    --set @PONumber = 
    set @GridNumber = substring('PA-1501200', 4 , 99)
    
    select * from int_err_PutAway_writeback
    where 
    ((refno = @PONumber) and isnull(@PONumber,'') <> '')
    or
    ((refno = @GridNumber) and isnull(@PONumber,'') = '')
    and err_datetime is not null
    union all
    select *, ' ' ab_res from int_err_Receive_writeback
    where 
    ((refno = @PONumber) and isnull(@PONumber,'') <> '')
    or
    ((refno = @GridNumber) and isnull(@PONumber,'') = '')
    and err_datetime is not null
    union all
    select * from int_err_ibtPutAway_writeback
    where 
    ((refno = @PONumber) and isnull(@PONumber,'') <> '')
    or
    ((refno = @GridNumber) and isnull(@PONumber,'') = '')
    and err_datetime is not null
    union all
    select *, ' ' ab_res from int_err_ibtReceive_writeback
    where 
    ((refno = @PONumber) and isnull(@PONumber,'') <> '')
    or
    ((refno = @GridNumber) and isnull(@PONumber,'') = '')
    and err_datetime is not null
    order by err_datetime desc
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Here are the possible reason for your error:

    1. If your code passes the line
    Code:
       set @GridNumber = substring('PA-1501200', 4 , 99)
    then the error is on your select.

    2.Your REFNO column is a date data type.

    3. One of the field on your table that you're trying to UNIONize is a date and you're trying to UNION it with a char/varchar column.

    Happy Coding!!!

    ~~ CK

    Comment

    Working...