I'm having a hard time in assigning values that came from my database,
Value to be retrieved :
AppointmentTime
Value = 11:00:00
Data Type = time(0)
I use VS 2010 to assign that value from SQL Server 2008 with a DateTimePicker..
DateTimePicker1
Format = HH:mm
MinDate = 1/1/1770
DateTimePicker1.Value = Appointment Time
It gives me an error of 'Value of 1/1/0001 11:00:00AM is not valid for Value. Value should be between MinDate and MaxDate..
I wonder why SQL Server 2008 gives a value of Date, in my Time(0) Column?
I try this query
declare #x time(0);
set #x = getdate();
print cast(#x as datetime);
It will be give you the time with the date 01/01/1900.
So, try to cast Appointment Time to DateTime before set the DateTimePicker1 value.
Related
I have two function.
fn_validate_date
fn_validation
fn_validate_date code:
CREATE DEFINER=`root`#`localhost` FUNCTION `fn_validate_date`(
`dt_date` DATE
)
RETURNS date
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT 'Returns the associated value of given attribute for given employee for a particular date.'
BEGIN
SET dt_date = IF(dt_date IS NULL OR dt_date ='', CURRENT_DATE, dt_date);
RETURN dt_date;
END
fn_validation code:
CREATE DEFINER=`root`#`localhost` FUNCTION `fn_validation`(
`dt_date` DATE
)
RETURNS date
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
RETURN fn_validate_date(dt_date);
END
Now when I am calling fn_validate_date as below
SELECT `fn_validate_date`(null);
It's working well but when I calling fn_validation it's giving me an error.
SELECT `fn_validation`(null);
My question is why I didn't get error while calling fn_validate_date?
In fn_validate_date, the dt_date-parameter is type of date and you are comparing it to a string datatype. No need for that. Date datatype cannot contain ''. It either is NULL or has a date value in it.
So instead of:
SET dt_date = IF(dt_date IS NULL OR dt_date ='', CURRENT_DATE, dt_date);
You can simply use:
return ifnull( dt_date, current_date() );
I disable strict mode and it's working well.
SET sql_mode =''
To disable strict mode in MySQL. I am not sure why MySql short-circuited IF condition as I am passing NULL in the input parameter of fn_validation.
How to write below code in in SQL server 2008 r2
DECLARE #date date
DECLARE #datetime datetime
SELECT #datetime = GETDATE()
select #date = GETDATE()
select format(cast(min(#date + #datetime) as datetime), 'hh:mm:ss tt')
this piece of code doesn't work in SQL Server 2008 r
Your code is confusing. You are trying to add two dates which doesn't make sense. Anyway, here's how to convert a datetime to output the hh:mm:ss format.
DECLARE #datetime datetime
SELECT #datetime = GETDATE()
select
DT = convert(char(10),#datetime,120)
,TimeOnly = left(cast(#datetime as time),8)
Looking to convert datetime format
20150416 12:29:20:845
to
20150416 12:29:20:84.
I have tried a number of date time conversion but no luck.
DECLARE #Date varchar(22)
SET #Date = CONVERT(Varchar(23), GETDATE(),121) --replace getdate with your value
SELECT #DATE --2015-04-17 10:56:55.29 format you need
SELECT GETDATE() --2015-04-17 10:56:55.297 actual value
SELECT CAST(#DATE as datetime) --2015-04-17 10:56:55.290 if u convert it back
Assuming that you already converted your date as varchar, you can use this query:
DECLARE #V VARCHAR(30) = '20150416 12:29:20:845'
SELECT LEFT(#V,20),STUFF(#V,21,1,'.')
I have a strange bug that I cannot resolve (with a one line function).
This code works:
DECLARE #TestDate datetime = '2013-05-01 23:15:11'
select IsNull(convert(varchar(max), #TestDate, 120), 'null') as 'test1'
Displays: 2013-05-01 23:15:11
CREATE FUNCTION [dbo].[DateOrNullToChar] (#InputDate date)
RETURNS VARCHAR(40)
BEGIN
return ISNULL(convert(varchar(40),#InputDate, 120),'null');
END
select dbo.DateOrNullToChar('2013-05-01 23:15:11') as 'result'
Returns: 2013-05-01 (no time)
I have also tried varchar(max).
The purpose of the function is for something like this:
Set #ErrorMessage = ' #ArrivalDate=' + dbo.DateOrNullToChar(#ArrivalDate) +
' #DepartureDate=' + dbo.DateOrNullToChar(#DepartureDate);
If any one value is null, the whole value becomes null. So I want to see the string 'null' when a date has a null value.
#InputDate should be datetime or datetime2 if you want time to be shown
The clues are in the code...
#TestDate datetime
#InputDate date
You need to make the parameter type to be datetime instead of date:
CREATE FUNCTION [dbo].[DateOrNullToChar] (#InputDate datetime)
It's silently converting the string to your date parameter type and thus dropping the time portion.
I am writing a SQL query using SQL Server Management Studio and there are some NVARCHAR type values in ISO date format (example: 20130302T164800). I need to convert them to a DATETIME
I tried the Convert() function but it causes an exception:
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value
Is there a way I can do this?
The problem is that your string is not an accepted SQL Server datetime format. SQL Server recognises the ISO8601 format, which is:
yyyy-mm-ddThh:mi:ss.mmm
Which would be 2013-03-02T16:48:00 for your date above.
See Date And Time Styles section.
So the following statement will fail:
declare #date nvarchar(max) = '20130302T164800'
select convertedDate = cast(#date as datetime)
If you convert the string to the ISO8601 format, the statement will work:
declare #date nvarchar(max) = '2013-03-02T16:48:00'
select convertedDate = cast(#date as datetime)
SQL Fiddle with demo.
You can update your format to one SQL Server recognises and cast the string to a datetime in one statement:
declare #date nvarchar(max) = '20130302T164800'
select cast(left(#date, 4)
+ '-' + substring(#date,5,2)
+ '-' + substring(#date,7,5)
+ ':' + substring(#date,12,2)
+ ':' + substring(#date,14,2) as datetime)
SQL Fiddle with demo.
This is just an example, you could convert it to any format recognised by SQL Server, but this converts it to ISO8601. Basically, convert it to a different format to allow the conversion to work.