Find and Replace - Date within SQL Query

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Tarsinion
Posts: 1
Joined: 11 May 2010 05:15

Find and Replace - Date within SQL Query

#1 Post by Tarsinion » 11 May 2010 05:23

Hi all,

i would want to extract some data from a sql database every day. The date of the scripts needs to be changed to match the current date. Every "find and replace" script is not working on these sql querys, and i'm not quite sure why.

I need help to change this line automaticly every day to both match the current date.
"and (pb2.pdatetime >= '2008/01/20') and (pb2.pdatetime <= '2010/12/20');"

This will "echo" will put out the right date:
set jahr=%date:~-4%
set monat=%date:~-7,2%
set tag=%date:~-10,2%
echo %jahr%/%monat%/%tag%

The whole sql statment has about 720 chars.

Thanks a million for every hint and help :-)

Tarsinion

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Find and Replace - Date within SQL Query

#2 Post by jeb » 11 May 2010 09:11

Hi Tarsinion,

I don't understand your problem exactly.
But I make a guess, as you can echo the date, you should be able to echo your "new line" like

Code: Select all

set jahr=%date:~-4%
set monat=%date:~-7,2%
set tag=%date:~-10,2%

set "newLine=and (pb2.pdatetime ^>= '2008/01/20') and (pb2.pdatetime ^<= '%jahr%/%monat%/%tag%');"
echo %newLine%

results (today) to:
and (pb2.pdatetime >= '2008/01/20') and (pb2.pdatetime <= '2010/05/11');


Ok, but is the date 2008/01/20 fix or in some way does it depends of today?

Or do you have a problem to switch the line in your sql script?

First you can try something like
SQL_Template.tpl

Code: Select all

select irgendwas from tabelle where xyz=3 


Then you can each time append simply your created line like.

Code: Select all

copy SQL_Template.tpl script.sql
echo %newLine% >>script.sql



jeb

Post Reply