Link to home
Start Free TrialLog in
Avatar of Susmitha Sajja
Susmitha Sajja

asked on

Convert in psql

I created a sql statement in Microsoft sql server as
Update [dbo].[custeqip]
Set [yearofmfg] = convert(varchar(4), cast(‘01/01/‘ +right ( cast([yearofmfg] as int )’2) as datatime )’120) where [ yearofmfg] <> 0
It worked fine in ms sql server
I was trying to use the same statement in psql and it’s giving me an error like
The second parameter yearofmfg for convert is invalid
Can some one help me with this
Thanks
Avatar of Mirtheil
Mirtheil
Flag of United States of America image

Are you using PostgreSQL or Pervasive / Actian PSQL?
What exactly are you trying to accomplish?  What's the [yearofmfg] column defined as?  What are you expecting the result to be?
Avatar of Susmitha Sajja
Susmitha Sajja

ASKER

It's pervasive sql. Yearofmfg is a column with type smallint.
What was I was trying to do is that yearofmfg is the manufacture year of parts and some fields in it are 99 or 97 which should be 1999 or 1997 and some are 7 or 5 or 1 which should be 2007 or 2005 or 2001 I was trying to convert them. The above sql statement worked in ms sql
Try this instead:  IF(yearofmfg<50,yearofmfg+2000,yearofmfg+1900)
You can adjust the "50" in there to change your cut-over year.
But I don't want to change if it's 0.
IF(yearofmfg<50,IF(yearofmfg>0,yearofmfg+2000,0),yearofmfg+1900)

This version leaves a 0 at 0.
I'm getting a syntax error as if<< ?? >> yearofmfg < 50, if ( yearofmfg >0, yearofmfg + 2000, 0 ) , yearofmfg +1900)
Looks like you are missing a paren.  I've added spaces to make it easier to read:
   IF( yearofmfg < 50 , IF( yearofmfg > 0, yearofmfg + 2000 , 0 ) , yearofmfg + 1900 );

You an test with these three simple statements:
  select IF( 0 < 50 , IF( 0 > 0, 0 + 2000 , 0 ) , 0 + 1900 );
  select IF( 7 < 50 , IF( 7 > 0, 7 + 2000 , 0 ) , 7 + 1900 );
  select IF( 77 < 50 , IF( 77 > 0, 77 + 2000 , 0 ) , 77 + 1900 );
I'm still getting the same error
IF << ??? >> ( yearofmfg < 50 ......
What about when you run the other three statements?  Do those evaluate properly?
Omg it worked but the date has some fields that are 2007 now they are 4007! Is there anyway I can go back to the way it is or change the 4007 to 2007
Since you don't post your entire query, let me take a stab:

Update custeqip
Set yearofmfg = IF(yearofmfg<50,yearofmfg+2000,yearofmfg+1900)
WHERE yearofmfg <> 0

Note that with the WHERE clause involved, you don't need the extra IF, which is why I didn't include it the first time.

As with any update, you can test like this:
SELECT yearofmfg, IF(yearofmfg<50,yearofmfg+2000,yearofmfg+1900)
FROM custeqip
WHERE yearofmfg <> 0
Hello,

I don't know exact difference between PostgreSQL and Pervasive. But it you are trying to do some this in PostgreSQL.

You can use below statement instead of "convert(varchar(4), cast(‘01/01/‘ +right ( cast([yearofmfg] as int )’2) as datatime )’120)".

EXTRAcT(YEAR FROM TO_DATE('01/01/'||'97', 'dd/MM/yy'));

Open in new window


Exact statement to just verify the sql is.

SELECT EXTRAcT(YEAR FROM TO_DATE('01/01/'||'97', 'dd/MM/yy'));

Open in new window


Hope it will help you.
ASKER CERTIFIED SOLUTION
Avatar of Bill Bach
Bill Bach
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How to update for 1900
I don't understand your last question.  Please clarify.