user2306856
user2306856

Reputation: 81

Convert "Thu Sep 22 3:50 2016" to "2016-09-22" in Solaris, without GNU date

Could someone please suggest a simple and short approach to convert "Thu Sep 22 3:50 2016" to "2016-09-22" in Solaris, through a shell script?

I do not have GNU date available on Solaris as discussed in below post: Convert date String to number on Solaris shell script gives No such file or directory

I need to query an sql server db, where date is saved in the format, "2016-09-06", hence I need to convert it

Upvotes: 0

Views: 79

Answers (2)

jlliagre
jlliagre

Reputation: 30823

Actually, you do have GNU date available but here is anyway one way to achieve this by scripting:

#!/bin/ksh
a="Thu Sep 22 3:50 2016"
echo $a | nawk '
BEGIN {
  m=1
  m2m["Jan"]=m++;
  m2m["Feb"]=m++;
  m2m["Mar"]=m++;
  m2m["Apr"]=m++;
  m2m["May"]=m++;
  m2m["Jun"]=m++;
  m2m["Jul"]=m++;
  m2m["Aug"]=m++;
  m2m["Sep"]=m++;
  m2m["Oct"]=m++;
  m2m["Nov"]=m++;
  m2m["Dec"]=m++;
}
{
  printf("%s-%02d-%02d\n",$5,m2m[$2],$3)
}'

output:

2016-09-22

Upvotes: 1

zerohero
zerohero

Reputation: 583

Why not use Oracle's sysdate?

select * from your_table where saved_date >= to_char(sysdate,'yyyy-MM-dd')

Upvotes: 0

Related Questions