Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

find and replace yesterday's with todays date in sql file

Highlighted
diwakar_4
Frequent Advisor

find and replace yesterday's with todays date in sql file

Hi All,

I have sql file which contains one confition like:

OR ((n_works_notices.local_rec_date = "2008-11-11")

Everyday this date is replqced with today's date. For that i am using sed command:

sed -e 's/((n_works_notices.local_rec_date = ............/((n_works_notices.l
ocal_rec_date = "2008-11-11"/1' mysql.sql > temp.sql

Now i have new sql file which contains condition as

AND ("2008-11-08">=from_date) AND ("2008-11-08"<=to_date)

Now how to replace these to dates(2008-11-08) with todays date.
Everyday i have to find existing date and replace with today's date.

Can some one please help me?

Thanks
7 REPLIES
Suraj K Sankari
Honored Contributor

Re: find and replace yesterday's with todays date in sql file

Hi,

You can store current date into a variable and use this variable where you need current date see the below example:

$ dt=`date +%Y"-"%d"-"%m`
$ echo $dt
2008-11-11


Suraj
diwakar_4
Frequent Advisor

Re: find and replace yesterday's with todays date in sql file

Hi Suraj,

I am doing same. But can you please show the complete sed statement.
Dennis Handly
Acclaimed Contributor

Re: find and replace yesterday's with todays date in sql file

>can you please show the complete sed statement.

The problem is how to compute yesterday.

The simplest way is to start with a template file where you don't have yesterday but you have a template: YYYY-MM-DD

Then you just have a sed command that changes that to today:
$ dt=$(date +%Y-%d-%m)
$ sed "s/YYYY-MM-DD/$dt/g" template.sql > temp.sql
OldSchool
Honored Contributor

Re: find and replace yesterday's with todays date in sql file

if "from_date" and "to_date" are "date" fields and not "character" or "var_char", then depending on *whose* sql this is, the "date" functions should be capable of handling this directly.

Look for something like "date('today')" in your implementation. In addition, you might need to fiddle the date format

diwakar_4
Frequent Advisor

Re: find and replace yesterday's with todays date in sql file

Hi dennis,

I was just thinking of idea to find pattern like YYYY-MM-DD in sql and replacing with current date.
Can you sugget how to write pattern and dates following this pattern will be getting changed.

It means it if it fileds date in statement :
OR ((n_works_notices.local_rec_date = "2008-11-11")

it will update to current date.

Thanks
Dennis Handly
Acclaimed Contributor

Re: find and replace yesterday's with todays date in sql file

>I was just thinking of idea to find pattern like YYYY-MM-DD in sql

I'm suggesting you use EXACTLY that string and change it every time you need it.

I suppose you could look for DDDD-DD-DD (D is a digit) but why bother?
diwakar_4
Frequent Advisor

Re: find and replace yesterday's with todays date in sql file

Hi Dannis,

Thanks a lot.

Looks it will help.

Thanks