Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Date problem

 
   Database Help (Home) -> Programming RSS
Next:  SQL 2005 Performance  
Author Message
GW

External


Since: Mar 22, 2010
Posts: 3



(Msg. 1) Posted: Mon Mar 22, 2010 6:01 pm
Post subject: Date problem
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Need help.

Having problem to bring the date into string. Currently, I have to declare
@cp_dt in sql string where @cp_dt is within the quote. I dont want to
re-declare @cp_dt (double work). Is there any other way to do the following
:-


declare @cp_dt datetime,
@cp_dt_cv varchar(10),
@cp_dt_txt varchar(Cool,
@cp_file varchar(6)
set @cp_dt = (select top 1 position_date from glob.dbo.cp order by
position_date desc)
set @cp_dt_cv = convert(varchar(10),@cp_dt,103)
set @cp_dt_txt = replace(@cp_dt_cv,'/','')
set @cp_file = (select right(ltrim(rtrim(@cp_dt_txt)),6) )
declare @sqlcp01 nvarchar(4000)
declare @sqlcp02 nvarchar(4000)
set @sqlcp02 = 'drop table moon.dbo.CP_'+@cp_file
set @sqlcp01 = 'declare @cp_dt datetime '+
' set @cp_dt = (select top 1 position_date from glob.dbo.cp order by
position_date desc) ' +
'select a, b, c, d, '+
'position_date, e, f, '+
'g, h, i '+
'into moon.dbo.CP_'+@cp_file+
' from glob.dbo.cp '+
'where position_date = @cp_dt'

if exists ( select * from dbo.sysobjects where id =
object_id(N'moon.dbo.CP_'+@cp_file)
and OBJECTPROPERTY(id, N'IsUserTable') = 1)

exec sp_executesql @sqlcp02

exec sp_executesql @sqlcp01

declare @sqlcp03 nvarchar(4000)
set @sqlcp03 = 'ALTER TABLE CP_'+@cp_file +
' ADD j VARCHAR(10), '+
'k VARCHAR(10), m VARCHAR(3), n VARCHAR(1), '+
'p varchar(2), q varchar(3) '

exec sp_executesql @sqlcp03

 >> Stay informed about: Date problem 
Back to top
Login to vote
TheSQLGuru

External


Since: Jan 11, 2008
Posts: 579



(Msg. 2) Posted: Mon Mar 22, 2010 8:19 pm
Post subject: Re: Date problem [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Not sure how you expect us to help you without a single definition of the
table(s) involved nor sample data for those tables nor expected outcomes...

--
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net


"GW" wrote in message

> Need help.
>
> Having problem to bring the date into string. Currently, I have to declare
> @cp_dt in sql string where @cp_dt is within the quote. I dont want to
> re-declare @cp_dt (double work). Is there any other way to do the
> following
> :-
>
>
> declare @cp_dt datetime,
> @cp_dt_cv varchar(10),
> @cp_dt_txt varchar(Cool,
> @cp_file varchar(6)
> set @cp_dt = (select top 1 position_date from glob.dbo.cp order by
> position_date desc)
> set @cp_dt_cv = convert(varchar(10),@cp_dt,103)
> set @cp_dt_txt = replace(@cp_dt_cv,'/','')
> set @cp_file = (select right(ltrim(rtrim(@cp_dt_txt)),6) )
> declare @sqlcp01 nvarchar(4000)
> declare @sqlcp02 nvarchar(4000)
> set @sqlcp02 = 'drop table moon.dbo.CP_'+@cp_file
> set @sqlcp01 = 'declare @cp_dt datetime '+
> ' set @cp_dt = (select top 1 position_date from glob.dbo.cp order by
> position_date desc) ' +
> 'select a, b, c, d, '+
> 'position_date, e, f, '+
> 'g, h, i '+
> 'into moon.dbo.CP_'+@cp_file+
> ' from glob.dbo.cp '+
> 'where position_date = @cp_dt'
>
> if exists ( select * from dbo.sysobjects where id =
> object_id(N'moon.dbo.CP_'+@cp_file)
> and OBJECTPROPERTY(id, N'IsUserTable') = 1)
>
> exec sp_executesql @sqlcp02
>
> exec sp_executesql @sqlcp01
>
> declare @sqlcp03 nvarchar(4000)
> set @sqlcp03 = 'ALTER TABLE CP_'+@cp_file +
> ' ADD j VARCHAR(10), '+
> 'k VARCHAR(10), m VARCHAR(3), n VARCHAR(1), '+
> 'p varchar(2), q varchar(3) '
>
> exec sp_executesql @sqlcp03
>

 >> Stay informed about: Date problem 
Back to top
Login to vote
GW

External


Since: Mar 22, 2010
Posts: 3



(Msg. 3) Posted: Mon Mar 22, 2010 8:19 pm
Post subject: Re: Date problem [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

1. @cp_dt declared twice i.e at 1st line & in @sqlcp01 . I have to put the
declaration in @sqlcp01 (within quote). If not, it failed with error 'Must
declare variable @cp_dt'
2. @cp_dt type is datetime while @sqlcp01 is string. I want to put @cp_dt
outside the quote. If I put it outside, I will get '@cp_dt conversion error'.
In normal query, it will be like this :-

select a, b, c, d, position_date, e, f, g, h, i into moon.dbo.CP_+@cp_file
from glob.dbo.cp where position_date = @cp_dt


"TheSQLGuru" wrote:

> Not sure how you expect us to help you without a single definition of the
> table(s) involved nor sample data for those tables nor expected outcomes...
>
> --
> Kevin G. Boles
> Indicium Resources, Inc.
> SQL Server MVP
> kgboles a earthlink dt net
>
>
> "GW" wrote in message
>
> > Need help.
> >
> > Having problem to bring the date into string. Currently, I have to declare
> > @cp_dt in sql string where @cp_dt is within the quote. I dont want to
> > re-declare @cp_dt (double work). Is there any other way to do the
> > following
> > :-
> >
> >
> > declare @cp_dt datetime,
> > @cp_dt_cv varchar(10),
> > @cp_dt_txt varchar(Cool,
> > @cp_file varchar(6)
> > set @cp_dt = (select top 1 position_date from glob.dbo.cp order by
> > position_date desc)
> > set @cp_dt_cv = convert(varchar(10),@cp_dt,103)
> > set @cp_dt_txt = replace(@cp_dt_cv,'/','')
> > set @cp_file = (select right(ltrim(rtrim(@cp_dt_txt)),6) )
> > declare @sqlcp01 nvarchar(4000)
> > declare @sqlcp02 nvarchar(4000)
> > set @sqlcp02 = 'drop table moon.dbo.CP_'+@cp_file
> > set @sqlcp01 = 'declare @cp_dt datetime '+
> > ' set @cp_dt = (select top 1 position_date from glob.dbo.cp order by
> > position_date desc) ' +
> > 'select a, b, c, d, '+
> > 'position_date, e, f, '+
> > 'g, h, i '+
> > 'into moon.dbo.CP_'+@cp_file+
> > ' from glob.dbo.cp '+
> > 'where position_date = @cp_dt'
> >
> > if exists ( select * from dbo.sysobjects where id =
> > object_id(N'moon.dbo.CP_'+@cp_file)
> > and OBJECTPROPERTY(id, N'IsUserTable') = 1)
> >
> > exec sp_executesql @sqlcp02
> >
> > exec sp_executesql @sqlcp01
> >
> > declare @sqlcp03 nvarchar(4000)
> > set @sqlcp03 = 'ALTER TABLE CP_'+@cp_file +
> > ' ADD j VARCHAR(10), '+
> > 'k VARCHAR(10), m VARCHAR(3), n VARCHAR(1), '+
> > 'p varchar(2), q varchar(3) '
> >
> > exec sp_executesql @sqlcp03
> >
>
>
> .
>
 >> Stay informed about: Date problem 
Back to top
Login to vote
ML

External


Since: Jan 15, 2008
Posts: 380



(Msg. 4) Posted: Tue Mar 23, 2010 2:35 am
Post subject: Re: Date problem [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Erland Sommarskog wrote two must-read articles on dynamic SQL, which I
strongly suggest you read before attempting any kind of work involving
dynamic SQL:

http://www.sommarskog.se/dynamic_sql.html
http://www.sommarskog.se/dyn-search.html

If, for some reason, you're in a hurry, you can start by PRINTing your query
string and reviewing it instead of executing it and then trying to guess
what's wrong with it.

And as Kevin already pointed out, next time, please, provide DDL, sample
data and expected results.


ML

---
Matija Lah, SQL Server MVP
http://milambda.blogspot.com/
 >> Stay informed about: Date problem 
Back to top
Login to vote
ben brugman

External


Since: Aug 08, 2003
Posts: 23



(Msg. 5) Posted: Tue Mar 23, 2010 5:25 am
Post subject: Re: Date problem [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hello GW,

Although this is not 'your' solution it may help you to your solution.

1.
Reduced the number of parameters used for the date to two.
One parameter to hold the date in date time format.
One parameter to hold the string which is used as part of the filename.
2.
All declarations at the beginning of the script.
3.
The strings to be executes are formatted for better reading.
The dateparts in the string are filled with placeholders which are replaced
in the string.

Advise:
Read the site from Tibor on datetime datetypes:
http://www.karaszi.com/sqlserver/info_datetime.asp
Prefere dataformats with yyyy mm dd, (also in filenames and in code).

In your example some elements are missing, examples, but also the table
definitions,
most important, the 'datetime' field from which the cp_dt is picked up from.

The code below does run, but is only an illustration, the prints should be
replaced
by execution statements.

Ben Brugman

---------------------------------------------------------------------------------------------
declare @sqlcp01 nvarchar(4000)
declare @sqlcp02 nvarchar(4000)
declare @sqlcp03 nvarchar(4000)

declare @cp_dt datetime
declare @cp_file varchar(6)

--
-- Pick up the date
-- In a datetime format and a string format for the file.
-- The getdate() should be replaced the the technique to pick up the
'correct' date.
--
---------------------------------------------------------------------------------------------
set @cp_dt =
-- Pick up the date
set @cp_file = SUBSTRING(convert(varchar(10), @cp_dt,126),6,2)+ -- Pick up
mm
SUBSTRING(convert(varchar(10), @cp_dt,126),1,4) -- Pick up
yyyy


--
-- Build the strings which are needed.
-- Strings are represented in readable format.
-- Constants are replaces afterwards to keep the strings readable.
--
---------------------------------------------------------------------------------------------
-- describe here the purpose of this statement.
--
set @sqlcp01 = 'drop table moon.dbo.CP '+@cp_file

---------------------------------------------------------------------------------------------
-- describe here the purpose of this statement.
--
set @sqlcp02 =
'
select a, b, c, d, position_date, e, f, g, h, i
into moon.dbo.CP_DDDD01
from glob.dbo.cp
where position_date = ''DDDD02''
'
set @sqlcp02 = REPLACE (@sqlcp02, 'DDDD01', @cp_file)
set @sqlcp02 = REPLACE (@sqlcp02, 'DDDD02', convert(varchar(10),@cp_dt,121))

---------------------------------------------------------------------------------------------
-- describe here the purpose of this statement.
--
set @sqlcp03 =
'
ALTER TABLE CP_DDDD01
ADD j VARCHAR(10), k VARCHAR(10), m VARCHAR(3), n VARCHAR(1), p
varchar(2), q varchar(3)
'
set @sqlcp03 = REPLACE (@sqlcp03, 'DDDD01', @cp_file)

--
-- Print the strings.
-- This should be replaced by executing the strings.
--
print @sqlcp01
print '-------------------------'
print @sqlcp02
print '-------------------------'
print @sqlcp03
print '-------------------------'






"GW" wrote in message

> 1. @cp_dt declared twice i.e at 1st line & in @sqlcp01 . I have to put the
> declaration in @sqlcp01 (within quote). If not, it failed with error 'Must
> declare variable @cp_dt'
> 2. @cp_dt type is datetime while @sqlcp01 is string. I want to put @cp_dt
> outside the quote. If I put it outside, I will get '@cp_dt conversion
> error'.
> In normal query, it will be like this :-
>
> select a, b, c, d, position_date, e, f, g, h, i into moon.dbo.CP_+@cp_file
> from glob.dbo.cp where position_date = @cp_dt
>
>
> "TheSQLGuru" wrote:
>
>> Not sure how you expect us to help you without a single definition of the
>> table(s) involved nor sample data for those tables nor expected
>> outcomes...
>>
>> --
>> Kevin G. Boles
>> Indicium Resources, Inc.
>> SQL Server MVP
>> kgboles a earthlink dt net
>>
>>
>> "GW" wrote in message
>>
>> > Need help.
>> >
>> > Having problem to bring the date into string. Currently, I have to
>> > declare
>> > @cp_dt in sql string where @cp_dt is within the quote. I dont want to
>> > re-declare @cp_dt (double work). Is there any other way to do the
>> > following
>> > :-
>> >
>> >
>> > declare @cp_dt datetime,
>> > @cp_dt_cv varchar(10),
>> > @cp_dt_txt varchar(Cool,
>> > @cp_file varchar(6)
>> > set @cp_dt = (select top 1 position_date from glob.dbo.cp order by
>> > position_date desc)
>> > set @cp_dt_cv = convert(varchar(10),@cp_dt,103)
>> > set @cp_dt_txt = replace(@cp_dt_cv,'/','')
>> > set @cp_file = (select right(ltrim(rtrim(@cp_dt_txt)),6) )
>> > declare @sqlcp01 nvarchar(4000)
>> > declare @sqlcp02 nvarchar(4000)
>> > set @sqlcp02 = 'drop table moon.dbo.CP_'+@cp_file
>> > set @sqlcp01 = 'declare @cp_dt datetime '+
>> > ' set @cp_dt = (select top 1 position_date from glob.dbo.cp order by
>> > position_date desc) ' +
>> > 'select a, b, c, d, '+
>> > 'position_date, e, f, '+
>> > 'g, h, i '+
>> > 'into moon.dbo.CP_'+@cp_file+
>> > ' from glob.dbo.cp '+
>> > 'where position_date = @cp_dt'
>> >
>> > if exists ( select * from dbo.sysobjects where id =
>> > object_id(N'moon.dbo.CP_'+@cp_file)
>> > and OBJECTPROPERTY(id, N'IsUserTable') = 1)
>> >
>> > exec sp_executesql @sqlcp02
>> >
>> > exec sp_executesql @sqlcp01
>> >
>> > declare @sqlcp03 nvarchar(4000)
>> > set @sqlcp03 = 'ALTER TABLE CP_'+@cp_file +
>> > ' ADD j VARCHAR(10), '+
>> > 'k VARCHAR(10), m VARCHAR(3), n VARCHAR(1), '+
>> > 'p varchar(2), q varchar(3) '
>> >
>> > exec sp_executesql @sqlcp03
>> >
>>
>>
>> .
>>
 >> Stay informed about: Date problem 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Problem with MAX date - I have ArticleNumber, and DateSell I need query which will give me oldest DateSell for each ArticleNumber. How to do that? I try SELECT ArticleNumber,DateSell FROM StavkaArtikla as a WHERE NOT EXISTS (SELECT ArticleNumber, DateSell ..

Date Problem in VB - Hi evreybody, I have a problem with dates. I have a date with this format: dd-mm-yyyy and I have to change it to yyyy-mm-dd. Any ideas how to do it?

problem with date as varchar - Hi! i have following table called test_Date_accident : trans_code varchar 1 nullable claim_number varchar 20 nullable adm_date varchar 8 nullable birth_date varchar 8 nullable date_accident varchar...

Date time Conversion problem - Hi! i m using following query select * from Mytable where convert(datetime,report_date) between '10-Nov-2006' and '11-Nov-2006' it is returning me the error Server: Msg 242, Level 16, State 3, Line 1 The conversion of a char data type to a datetime dat...

SELECT Question: Get each date when I have start und end d.. - Hello, I have this table: startdate enddate price 11/01/06 11/21/06 45 11/24/06 11/31/06 40 How can I make a SELECT from this table to get each date with a price like this: date price 11/01/06 45 11/02/06 45..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]