I am trying to create a function that converts a given date string to the desired DateTime format.
The code is:
Alter function dbo.getDateValue1(#inputdate varchar)
returns varchar
as
begin
declare #inputDateTransformed datetime = CAST(#inputdate AS datetime2)
declare #setDatevalue varchar = convert(varchar,#inputDateTransformed ,112)
return #setDatevalue
end
And I am calling the function as below:
SELECT dbo.getDateValue1('2022-01-01 18:15:15.600' )
Which gives me the error:
Conversion failed when converting date and/or time from character string.
Any help would be appreciated.
Sample:
Create function [dbo].[getDateValue1](#input_date datetime2)
returns varchar(100)
begin
declare
#v_ret varchar(100)
set #v_ret = CONVERT(varchar, #input_date, 112)
return #v_ret
end
SELECT dbo.getDateValue1('2022-01-01 18:15:15.600')
-- Result:
20220101
Related
How can we pass the date (data type DateTime) in the execution command to run a stored procedure?
Here is the code snippet.
ALTER PROCEDURE [dbo].[datefiltered]
#months_margin int,
#oDate datetime
AS
BEGIN
SELECT *
FROM dbo.table20
WHERE date = oDate
-- more code...
END
I am trying to execute this stored procedure using GETDATE() function or even pass date and time as a string but it's not working.
exec datefiltered 23 getDate()
As you want to apply date filter, you don't need to pass datetime value. You can convert to DATE datatype for equality.
select * from dbo.table20
where date = CAST(#oDate AS DATE)
Also, if you are just passing GETDATE() as default, you can keep getdate as default value, as given below:
Alter procedure [dbo].[datefiltered]
#months_margin int,
#oDate datetime = null
AS
Begin
IF #oDate IS NULL
BEGIN
SET #oDate = CAST(GETDATE() AS DATE)
END
When you call the procedure with default value, you don't need to pass parameter value for it.
exec datefiltered 23 -- getdate() filter is applied automatically
ALTER PROCEDURE [dbo].[datefiltered]
#months_margin int,
#oDate datetime
AS
BEGIN
SELECT *
FROM dbo.table20
WHERE date = CAST(#oDate AS DATE)
-- more code...
END
Executing this stored procedure:
exec datefiltered 23, '2010-02-30'
On a table I have a bigint column that stores a timestamp with a microsecond precision like:
636453251396217655
636453251398405201
636453251592389899
636453251668326820
I have to build a script that, if that date is older than a week, the row must moved to another table.
I tried to convert to date using:
CREATE FUNCTION [dbo].[UNIXToDateTime] (#timestamp bigint)
RETURNS datetime
AS
BEGIN
DECLARE #ret datetime
SELECT #ret = DATEADD(second, #timestamp, '1970/01/01 00:00:00')
RETURN #ret
END
and used like:
select dbo.UNIXToDateTime(636453251396217655)
but because of the bigint my script crash because:
Arithmetic overflow error during expression conversion in int data
type
I can lose precision, the important is the date part that is the main part of the sql filter.
Demo: http://sqlfiddle.com/#!6/24f05/1
There's an answer here for converting with epoch values:
CREATE FUNCTION [dbo].[fn_EpochToDatetime] (#Epoch BIGINT)
RETURNS DATETIME
AS
BEGIN
DECLARE #Days AS INT, #MilliSeconds AS INT
SET #Days = #Epoch / (1000*60*60*24)
SET #MilliSeconds = #Epoch % (1000*60*60*24)
RETURN (SELECT DATEADD(MILLISECOND, #MilliSeconds, DATEADD(DAY, #Days, '1/1/1970')))
END;
You can use that function but simply divide your epoch values. As you are fine with the loss of fidelity, this will suit your needs perfectly. For example:
DECLARE #epoch BIGINT = 636453251396217655
SELECT dbo.[fn_EpochToDatetime](#epoch/100000)
Function 1:
I need to create an sql server function named FirstDayInQtr to return the first day in the respective quarter of year when a date is input. This function should be defined with the following header.
CREATE FUNCTION FirstDayInQtr(#InputDate datetime) RETURNS datetime AS…
Should return the date of first day in the respective quarter. like 1/1/2016
Function 2:
A function to check if an input string consists of UPPERCASE characters.
This function should be defined with the following header.
CREATE FUNCTION CheckStringOfUpperAlphaOK(#String varchar(MAX)) RETURNS varchar(6) AS…
Should return "okay" if true and "not okay" if false
simply create function in this way
Function 1 for getting first day in quarter
CREATE FUNCTION FirstDayInQtr(#InputDate datetime)
RETURNS datetime
AS
BEGIN
DECLARE #day datetime
SELECT #day = DATEADD(qq, DATEDIFF(qq ,0, #InputDate),0)
Return #day
END
Function2 For checking capital character as
CREATE FUNCTION CheckStringOfUpperAlphaOK(#String varchar(MAX))
Returns VarChar(6)
AS
Begin
Declare #KeepValues as varchar(50)
Set #KeepValues = '%[^ ][A-Z]%'
While PatIndex(#KeepValues collate Latin1_General_Bin, #Temp) > 0
Set #Temp = Stuff(#Temp, PatIndex(#KeepValues collate Latin1_General_Bin, #Temp) + 1, 0, ' ')
Return #Temp
End
This will get you the First Day of the Current Quater. I think thats what you are after
CREATE FUNCTION FirstDayInQtr(#InputDate DATETIME)
RETURNS DATETIME
AS
BEGIN
DECLARE #firstDayOfCurrentQuater DATETIME
SELECT #firstDayOfCurrentQuater = DATEADD(qq, DATEDIFF(qq ,0, #InputDate),0)
RETURN #firstDayOfCurrentQuater;
END
I created the following sfunction
CREATE FUNCTION sf_GetTrainingDate (#ID_ITEM int,#EMPLOYEE VARCHAR)
RETURNS DATETIME AS
BEGIN
--(1)
DECLARE #RESULT DATETIME
DECLARE #DATE DATETIME
DECLARE #REPEAT_INTERVAL INT
SET #DATE = (/*subquery returning a date in datetime format*/)
SET #REPEAT_INTERVAL = (/*subquery returning a datetime */)
WHILE #DATE <= getdate()
BEGIN
SET #DATE=dateadd(month,#REPEAT_INTERVAL,#DATE)
IF #DATE > GETDATE()
BREAK
ELSE
CONTINUE
END
SET #RESULT = #DATE
RETURN #RESULT
--(2)
END
The code works correctly, I mean if I run only the part from (1) to (2) using some PRINT commands to read the value of #DATE all is fine, but as i use the stored function it returns NULL instead of a date.
Could you give me an hand?
hey guys,
i want to convert datetime from string, i have string which contains 'GETDATE' now i want to convert the same in datetime, the string in sql server is been defined as varchar(max)
Please reply how can i type cast the GETDATE() function from string to datetieme.
Regards
Abbas Electricwala
I guess that you mean that you have a string that contains the text GetDate() like this.
declare #S varchar(50)
set #S = 'getdate()'
And you want to turn that into a date variable executing getdate().
You could do this since you know what what getdate() means.
declare #S varchar(50)
set #S = 'getdate()'
declare #D datetime
set #D = (select
case #S when 'getdate()'
then getdate()
else null
end)
Does not really make sense, there probably is more to this than you are telling.
If you have just date string you can do it like this:
SELECT CONVERT(DATETIME, '2009-02-25 15:31:17.888')
But... GETDATE() returns a datetime value, not a string value. Just assign the GETDATE() result to your variable.
Sample (may be inacurate):
declare #mydate as datetime
set #mydate = GETDATE()