Showing posts with label smalldatetime. Show all posts
Showing posts with label smalldatetime. Show all posts

Friday, March 23, 2012

forcing data format mask without modifting code

I have statement which is comparing a smalldatetime column to literal string as follows:

sales_date ='21-9-2004 0:0:0.000'

when I run the statement in query analyzer it bombs out with:

Server: Msg 296, Level 16, State 3, Line 1
The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value.

If I alter the format of the date literal to '2004-09-21 00:00:00' the statement works.

Is there anyway of forcing the statement to treat '21-9-2004 0:0:0.000' as '2004-09-21 00:00:00' without modifying the statement itself ?There might be a global setting for how datetime fields are treated by default, I've never been tempted to go look for it. I'd rather do a CONVERT instead. There's also a 'SET DATEFORMAT' that might work for you.

What's wrong with changing the statement?|||Where is the data coming from? Is it always in that format? Can you use SUBSTRING?|||Unfortunately the data is in a liternal string 'DD-MM-YYYY' when in fact I require it be in 'MM-DD-YYYY' format.|||Couldn't you do something like...

cast(day(sales_date()) as varchar(2)) + '-' +
cast(month(sales_date()) as varchar(2)) + '-' +
cast(year(sales_date()) as char(4)) + ' 0:0:0.000'

??

Friday, February 24, 2012

For Loop help

Dear Friends,
I am getting problem in the following statement.
Please help.
ALTER PROCEDURE dbo.Defaulters
(@.Stdt smalldatetime)
AS
declare @.CNT int
select @.cnt = 1
FOR @.cnt <= 7
Select dbo.Employee_Master.Employee_Name
@.stdt =@.stdt +1
@.cnt = @.cnt + 1
Next
Best regards
SharadIs this a CLR stored procedure written in VB.NET?
"Shailesh" wrote:

> Dear Friends,
> I am getting problem in the following statement.
> Please help.
> ALTER PROCEDURE dbo.Defaulters
> (@.Stdt smalldatetime)
> AS
> declare @.CNT int
> select @.cnt = 1
> FOR @.cnt <= 7
>
> Select dbo.Employee_Master.Employee_Name
> @.stdt =@.stdt +1
> @.cnt = @.cnt + 1
> Next
>
> Best regards
> Sharad
>
>|||Shailesh (shailesh_gothal@.hotmail.com) writes:
> I am getting problem in the following statement.
> Please help.
> ALTER PROCEDURE dbo.Defaulters
> (@.Stdt smalldatetime) AS
> declare @.CNT int
> select @.cnt = 1
> FOR @.cnt <= 7
>
> Select dbo.Employee_Master.Employee_Name
> @.stdt =@.stdt +1
> @.cnt = @.cnt + 1
> Next
I'm sorry, but I'm afraid that a newsgroup is not the right venue for you
to help. A newsgroup is good when you have a specific question. But it's
not a very efficient place to learn the subject from the bottom.
If all you want help with is to write a control loop, I refer you to
the topic "Control-of-Flow Language" in the Transact-SQL Reference in
Books Online. The reason I give you "read-the-manual" answer, is that I
think that are better served by learing to use the manual. While it may
be with some resistence in the beginning, it pays off in the long run.
Now, even with the proper syntax the procedure would not make much sense.
You would select all rows in the table Employee_Name seven times. (Provided
that you have a database called dbo, that is!) This does not look like a
useful operation.
I would suggest that you should find some SQL Server training locally, our
get a book like Richard Waymires "SQL Server 2000 in 21 days". Playing
around and inventing your own syntax will only be frustrating and
ineffecient.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx