I've been working this for a while. Kind of new to SQL Server
functions and not seeing what I am doing wrong. I have this function
CREATE FUNCTION dbo.test (@Group varchar(50))
RETURNS smalldatetime AS
BEGIN
Declare @retVal varchar(10)
(SELECT @retVal= MIN([date]) FROM dbo.t_master_schedules WHERE
(event_id = 13) AND (group_ =@Group))
return convert(smalldatetime, @retVal, 1)
END
The error I get is
Server: Msg 296, Level 16, State 3, Procedure test, Line 6
The conversion of char data type to smalldatetime data type resulted in
an out-of-range smalldatetime value.
1) I tried declaring @retVal as a smalldatetime and get the error "Must
declare the variable '@retVal'.'
2) If I run that same query in query analyzer (manually inserting the
parm) it returns 11/14/2006. That's what I want.
If I change the function to this and run it
CREATE FUNCTION dbo.test (@Group varchar(50))
RETURNS varchar(50) AS
BEGIN
Declare @retVal varchar(50)
(SELECT @retVal= MIN([date]) FROM dbo.t_master_schedules WHERE
(event_id = 13) AND (group_ =@Group))
return convert(smalldatetime, @retVal, 1)
END
It now works but the return value is Nov 14 2006 12:00AM
What am I doing wrong?
TIA 6 7588
SQL Server (al*********@gmail.com) writes: I've been working this for a while. Kind of new to SQL Server functions and not seeing what I am doing wrong. I have this function
CREATE FUNCTION dbo.test (@Group varchar(50)) RETURNS smalldatetime AS BEGIN Declare @retVal varchar(10) (SELECT @retVal= MIN([date]) FROM dbo.t_master_schedules WHERE (event_id = 13) AND (group_ =@Group)) return convert(smalldatetime, @retVal, 1) END
The error I get is Server: Msg 296, Level 16, State 3, Procedure test, Line 6 The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value.
1) I tried declaring @retVal as a smalldatetime and get the error "Must declare the variable '@retVal'.' 2) If I run that same query in query analyzer (manually inserting the parm) it returns 11/14/2006. That's what I want.
What data type is t_master_schedules.date? If it is varchar(10), and
it returns 11/14/2006, the query looks, eh, funny to me. First,
11/14/2006 does not look like a date to me. :-) But even if I assume
that 11 is supposed to be a month, it seems strange that you consider
2006-11-14 to be less than 2004-12-12. Shouldn't your query read
MIN(convert(smalldatetime, [date], 101) in such case?
Alternatively, the column is datetime or smalldatetime, but in such
there is no need to incolve varchar at all.
Anyway, when I try:
select convert(smalldatetime, '11/14/2006', 1)
I get:
Server: Msg 295, Level 16, State 3, Line 1
Syntax error converting character string to smalldatetime data type.
Whereas
select convert(smalldatetime, '11/14/2006', 101)
returns 2006-11-14.
If I change the function to this and run it CREATE FUNCTION dbo.test (@Group varchar(50)) RETURNS varchar(50) AS BEGIN Declare @retVal varchar(50) (SELECT @retVal= MIN([date]) FROM dbo.t_master_schedules WHERE (event_id = 13) AND (group_ =@Group)) return convert(smalldatetime, @retVal, 1) END
It now works but the return value is Nov 14 2006 12:00AM
Here you are first converting to smalldatetime, and then convert
back to varchar without any format specification, why you get this
default format.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx
On 6 Jun 2006 01:50:03 -0700, SQL Server wrote:
(snip) 1) I tried declaring @retVal as a smalldatetime and get the error "Must declare the variable '@retVal'.'
Hi SQL Server,
And yet, that is exactly what you should do. Never convert unless you
have to.
The error message you got is not a result of declaring @retVal as a
smalldatetime, but a result of "something" that was off in the code when
you tried that. Unfortunately, you didn't post that version of the code,
so I can't tell you what went wrong. Maybe, if you still have tat
version archived, you could post it here?
Meanwhile, try if this works:
CREATE FUNCTION dbo.test (@Group varchar(50))
RETURNS smalldatetime
AS
BEGIN
DECLARE @retVal smalldatetime
SELECT @retVal = MIN([date])
FROM dbo.t_master_schedules
WHERE event_id = 13
AND group_ = @Group
RETURN @retVal
END
--
Hugo Kornelis, SQL Server MVP
Hugo Kornelis wrote: The error message you got is not a result of declaring @retVal as a smalldatetime, but a result of "something" that was off in the code when you tried that. Unfortunately, you didn't post that version of the code, so I can't tell you what went wrong. Maybe, if you still have tat version archived, you could post it here? -- Hugo Kornelis, SQL Server MVP
This is okay
CREATE FUNCTION dbo.test (@Group varchar(50))
RETURNS varchar(50) AS
BEGIN
Declare @retVal varchar(50)
(SELECT @retVal= MIN([date]) FROM dbo.t_master_schedules WHERE
(event_id = 13) AND (group_ =@Group))
return convert(smalldatetime, @retVal, 1)
END
This is okay too (change Returns from varchar(50) to datetime)
CREATE FUNCTION dbo.test (@Group varchar(50))
RETURNS datetime AS
BEGIN
Declare @retVal varchar(50)
(SELECT @retVal= MIN([date]) FROM dbo.t_master_schedules WHERE
(event_id = 13) AND (group_ =@Group))
return convert(smalldatetime, @retVal, 1)
END
But change it to this
This is okay too (change Returns from varchar(50) to datetime)
CREATE FUNCTION dbo.test (@Group varchar(50))
RETURNS datetime AS
BEGIN
Declare @retVal datetime
(SELECT @retVal= MIN([date]) FROM dbo.t_master_schedules WHERE
(event_id = 13) AND (group_ =@Group))
return convert(smalldatetime, @retVal, 1)
END
Here is a link to a screen capture of the error. http://i12.photobucket.com/albums/a2...rran/error.jpg
the column [date] in the table t_master_schedules is a datetime.
I actually do want @retVal to be a varchar because the end result
should be a string that shows the first date for a particular group and
the last date in a particular group. So I would be running a select
with a Max([date]) and returning a string
11/14/2006 and 02/03/2007
The problem is that I am not able to get the date formated into the
mm/dd/yyyy format that I want.
SQL Server (al*********@gmail.com) writes: CREATE FUNCTION dbo.test (@Group varchar(50)) RETURNS datetime AS BEGIN Declare @retVal datetime (SELECT @retVal= MIN([date]) FROM dbo.t_master_schedules WHERE (event_id = 13) AND (group_ =@Group)) return convert(smalldatetime, @retVal, 1) END ... the column [date] in the table t_master_schedules is a datetime.
I actually do want @retVal to be a varchar because the end result should be a string that shows the first date for a particular group and the last date in a particular group. So I would be running a select with a Max([date]) and returning a string
11/14/2006 and 02/03/2007
The problem is that I am not able to get the date formated into the mm/dd/yyyy format that I want.
If you want a string back, why do you then insist on converting to
smalldatetime? Should you not convert to char(10) and return char(10)?
Anyway, I would suggest that you scrap the function entirely. I don't
know where you use this function, but data access from scalar functions
should be avoided, as it can affect performance considerably if
you stick into a query. This is because the query more or less get
converted to a cursor behind the scenes. So it is much better to
integrate the logic in the main query.
As for the date formatting, you should avoid formatting dates in
SQL Server, but format them client side, so the the client's
regional settings are respected.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx
Erland Sommarskog wrote: If you want a string back, why do you then insist on converting to smalldatetime? Should you not convert to char(10) and return char(10)?
.. -- Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx
All I want to know is how to return
08/29/2006
from
'2006-08-29 00:00:00.000'
Looking at the SQL Server Books Online help resource it appears to me
that the convert function should be able to do this. But this doesn't
work. Why not and how can I format that date the way I want in the
output. In VB I'd just use the format function. Is there something
similar in T-SQL?
print convert(datetime, '2006-08-29 00:00:00.000', 101)
SQL Server (al*********@gmail.com) writes: All I want to know is how to return 08/29/2006
from '2006-08-29 00:00:00.000'
Looking at the SQL Server Books Online help resource it appears to me that the convert function should be able to do this. But this doesn't work. Why not and how can I format that date the way I want in the output. In VB I'd just use the format function. Is there something similar in T-SQL? print convert(datetime, '2006-08-29 00:00:00.000', 101)
That converts a string value to datetime. You want to convert a datetime
value to a string.
A datetime value is a internally a numeric value and does not have any
format. The format code in the above example tells SQL Server how to
interpret the string.
But as I said, while you can format date values to string in your SQL code,
you should avoid doing so. This should be done client-side, so that the
client's regional settings can be respected. I can tell you that if you
give me an app that spits out strings like 08/29/2006, you will have a bug
report back in ten seconds, because that is not a date as far as I'm
concerned.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se
Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Asif |
last post by:
Hi there,
I have been trying to understand the behaviour of char (*pfn)(null)
for a couple of days. can some body help me understand the behaviour
of char (*pfn)(null) in Visual C++ environment?...
|
by: Alex |
last post by:
My table is laid out as such:
ID (int) What (varchar 20) TimeStamp (smalldatetime)
------- ------------- ---------------
73 Start ...
|
by: garydevstore |
last post by:
Hi,
I have 2 tables, Mail_subject and Mail_Usage.
Mail_Subject contains the subject, body and some other bits of info.
CREATE TABLE (
NOT NULL ,
(50) COLLATE...
|
by: Marc Pelletier |
last post by:
Hello all,
I have a library of datetime routines that make things simpler for me.
Before I discovered DateTime.MinValue I had a function that looked like
public static DateTime DayZero { get {...
|
by: .Net Sports |
last post by:
I'm using these to assign a variable to a smalldatetime object in sql
server:
dim todnews = DateTime.Today.ToString ( "d" )
'connection string to server is on this line
Dim strSQL2 as string...
|
by: Dimitri Furman |
last post by:
This looks like a bug - hopefully somebody can explain what is actually
happening. Using SQL Server 2000 SP4.
Here's a repro script with comments:
/* repro table */
CREATE TABLE dbo.T (
ID...
|
by: jobo |
last post by:
If I have the function:
int f(int (*h)(int)) {
return (*h)(13);
}
What exactly does (int (*h)(int)) do? So it's taking a pointer but
what's with the two ints? Thanks.
|
by: justin |
last post by:
Hello all:
I have a FormView that has it's DataSourceID set to a SqlDataSource
that uses stored procedures for it's select, update, insert, and
delete. I want the FormView to have an "autosave"...
|
by: vincibleman |
last post by:
Howdy all,
Working my way into SQL from Access. Think I might have the hang of the basics, but would really appreciate a sanity check. The stored procedure listed below works, but I can't help...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |