Why am I not getting time with am or pm
DECLARE #inputDate varchar(25)
SELECT #inputDate = '3/13/2012 13:00'
-- Declare the return variable here
DECLARE #Result DATETIME
DECLARE #toReturn NVARCHAR(25)
-- Add the T-SQL statements to compute the return value here
SET #inputDate = REPLACE(#inputDate, '24:00', '00:00')
SET #Result = null
SET #toReturn = null
IF (ISDATE(#inputDate)=1)
BEGIN
DECLARE #utcOffset int
SET #utcOffset = -(DATEDIFF(HH, GETUTCDATE(), GETDATE()))
SET #Result = DATEADD(HH, #utcOffset, #inputDate)
SET #toReturn = CONVERT(NVARCHAR, #Result, 101)
END
-- Return the result of the function
SELECT #toReturn
Returns only date portion?
You will need to change CONVERT(NVARCHAR, #Result, 101) to something else. 101 returns a mm/dd/yyyy format. See here for more details: http://msdn.microsoft.com/en-us/library/ms187928.aspx
You are using 101 in your last convert statement and that is date only.
Have a look here for other options. CAST and CONVERT (Transact-SQL)
Try another date format instead of 101 in CONVERT operator.
Because your final conversion uses a format that only includes dates - no time component.
Try the following, instead:
SET #toReturn = CONVERT(NVARCHAR, #Result, 121)
Related
I'm trying to get a set of dates into a particular format (ddmmyy) so that they can be run against a number of scripts that I have.
I have managed to convert the date into the correct format, but when I try to store this as a variable it just returns as null or the un-formatted date.
DECLARE #CurrentDate SMALLDATETIME
SELECT #CurrentDate = getdate()
SELECT #CurrentDate = DATEADD(day, -1, #CurrentDate)
SELECT #CurrentDate = STR_REPLACE(CONVERT(varchar,#CurrentDate,3),'/',null)
--Returns this:
20-Mar-2002 00:00:00
DECLARE #CurrentDate SMALLDATETIME
SELECT #CurrentDate = getdate()
SELECT #CurrentDate = DATEADD(day, -1, #CurrentDate)
SELECT STR_REPLACE(CONVERT(varchar,#CurrentDate,3),'/',null)
--Returns this:
020320
I believe the problem comes from the fact that my declared variable is a smalldatetime object but I'm not sure of how to convert it correctly into a string that can be stored as a variable?
I've tried having a second variable and declaring it as a varchar and then storing my date as the varchar but this isn't working either:
DECLARE #CurrentDate SMALLDATETIME
DECLARE #CurrentDateFinal VARCHAR
SELECT #CurrentDate = getdate()
SELECT #CurrentDate = DATEADD(day, -1, #CurrentDate)
SELECT #CurrentDateFinal = CAST(STR_REPLACE(#CurrentDate,'/',null) AS VARCHAR)
--Returns this:
03-Mar-2020 00:00:00
You can do the current date amendment with the dateadd all in one line - there's no need to do two lines. The below gives you the DDMMYY output although I wouldn't use that format personally as you can come unstuck with regional differences (e.g. US prefer MMDDYY and UK tends to be DDMMYY). Also always use 4 digit years IMO.
DECLARE #FirstDate SMALLDATETIME
DECLARE #FinalDate varchar(20)
SELECT #FirstDate = DATEADD(day, -1,getdate())
set #FinalDate = STR_REPLACE(CONVERT(varchar,#FirstDate,3),'/',null)
SELECT #FinalDate
--------------------
030320
(1 row affected)
I want to select field opendate mmddyy data type decimal(6,0) to dd-mon-yyyy. How do I modify this statement?
select convert(varchar(10),M.opendate, 106)
FROM All.Customer M
The above results in the following error:
[SQL0204] CONVERT in *LIBL type *N not found
You can try this:
DECLARE #InputDate VARCHAR(8)
SET #InputDate = '073019'
DECLARE #InputYear VARCHAR(4)
DECLARE #InputMonth VARCHAR(2)
DECLARE #InputDay VARCHAR(2)
SET #InputYear = '20' + SUBSTRING(#InputDate, 5, 2)
SET #InputMonth = LEFT(#InputDate, 2)
SET #InputDay = SUBSTRING(#InputDate, 3, 2)
DECLARE #OutputDate DATE
SET #OutputDate = #InputYear +'-'+ #InputMonth +'-'+ #InputDay
SELECT CONVERT(VARCHAR(15), #OutputDate, 106)
Still you should know how to define the first 2 digits of the year. I have used '20' as default.
I have a varchar which is coming through as the following format: yyyy-dd-MM hh:mm:ss
I need to be able to convert that to a valid datetime, what will be the best method?
Edit for example:
DECLARE #d varchar = '2014-17-01 12:00:00'
SELECT CONVERT(DATETIME, #d, 103);
returns
Msg 241, Level 16, State 1, Line 3
Conversion failed when converting date and/or time from character string.
Via CONVERT?
SELECT CONVERT(DATETIME, '2014-31-01 17:00:00', 103 /* British */);
(No column name)
2014-01-31 17:00:00.000
You can use CONVERT as:
DECLARE #TodayDate datetime
SET #TodayDate = GETDATE()
SELECT CONVERT(VARCHAR, #TodayDate, 110) AS TodayDate
Output:
07-04-2014
Hope it helps !
EDITED to add SET DATEFORMAT. This resolved the error for me and ensures everything gets into the correct bucket.
SET DATEFORMAT ydm
DECLARE #d VARCHAR(20) = '2014-17-01 12:00:00';
DECLARE #targetDate DATETIME = GETDATE(); -- Control Date
DECLARE #passedDate DATETIME = CAST(#d AS DATETIME);
SELECT #targetDate, #passedDate;
Try to interchange day and month and then PARSE new string to datetime.
DECLARE #d varchar(50) = '2014-17-01 12:00:00'
DECLARE #year varchar(10) = SUBSTRING(#d,1,4)
DECLARE #day varchar(10) = SUBSTRING(#d,6,2)
DECLARE #month varchar(10) = SUBSTRING(#d,9,2)
DECLARE #time varchar(10) = SUBSTRING(#d,12,8)
DECLARE #new_d varchar(50) = #year+'-'+#month+'-'+#day+' '+#time
SELECT PARSE(#new_d AS datetime USING 'en-US') as Result
I need to add 1 day to the current date and have the output in the format yyyymmdd.
The code needs to be written on a stored procedure on the sql server.
currently my code is as follows:
DECLARE #dat DATE
select #dat = dateadd(DD, 1, getdate())
SELECT #dat =LEFT(CONVERT(VARCHAR(8), #dat, 112),10)
However, it seems like im doing something wrong as my output on the sql table is in the format yyyy-mm-dd. I need to get rid of the hyphens.
any suggestions guys? thanks in advance.
The issue is that you are assigning it back to a date object. You need to assign it to a varchar.
I did the following in SQL Server 2005:
DECLARE #dat DATETIME
DECLARE #string varchar(8)
SET #dat = GetUtcDate()
select #dat = dateadd(DD, 1, getdate())
SELECT #string =CONVERT(VARCHAR(8), #dat, 112)
PRINT #string
Change the declaration of #dat to a STRING
Try this one -
DECLARE #date VARCHAR(8)
SELECT #date = CONVERT(VARCHAR(8), DATEADD(DAY, 1, GETDATE()), 112)
SELECT #date
select #dat = dateadd(DD, 1, getdate())
DECLARE #datCus varchar(8)
select #datCus=LEFT(CONVERT(VARCHAR(8), #dat, 112),10)
The problem was that i assigned #dat to the insert statements values. However having a varchar variable to handle the converting part solved the problem (in this case #datCus).
I have the following function below.
It returns a date like Feb 29 2012 10:00PM. Is there a way to make it return the format as 2/29/2012 10:00PM
CREATE FUNCTION scrubDateString
(
-- Add the parameters for the function here
#inputDate varchar(150)
)
RETURNS DATETIME
AS
BEGIN
-- Declare the return variable here
DECLARE #Result DATETIME
-- Add the T-SQL statements to compute the return value here
DECLARE #tmpDate datetime
SET #Result = null
IF (ISDATE(#inputDate)=1)
BEGIN
SET #Result = DATEADD(HH, 5, #inputDate)
END
-- Return the result of the function
RETURN #Result
END
The function itself is returning a Date and time, "Feb 29 2012 10:00PM" and "2/29/2012 10:00AM" are merely string representations of that date (I am assuming that the AM and PM switch are not relevant to the question and are just a mistake). IF you want to format the date as a particular string look at the CONVERT function. e.g:
DECLARE #Date DATETIME
SET #Date = scrubDateString(#YourString)
SELECT CONVERT(VARCHAR, #Date, 101) + ' ' + RIGHT(CONVERT(VARCHAR, #Date, 0), 7) [Date]
Although I'd strongly advise leaving your date as a date, and doing any formatting on the application side if at all possible. If I have assumed wrong about the AM PM switch you could alter the code above to the following:
DECLARE #Date DATETIME
SET #Date = scrubDateString(#YourString)
IF (DATEPART(HOUR, #Date) > 12)
BEGIN
SET #Date = DATEADD(HOUR, -12, #Date)
END
SELECT CONVERT(VARCHAR, #Date, 101) + ' ' + RIGHT(CONVERT(VARCHAR, #Date, 0), 7) [Date]
If you want to format a DateTime you will need to return it as VARCHAR instead of as a date. If you declare a FUNCTION to return DateTime then it will always return a DateTime formatted in the standard SQL way.
You can return it as a string by using the sql CONVERT function:
RETURN CONVERT(varchar(500), #Result, 101)
A list of formats can be found on msdn. If you want a different format then you need to do something like what's discussed in this question
just change\add this:
SET #Result = DATEADD(HH, 5, #inputDate)
SET #Result = CONVERT(VARCHAR(10), #Result , 101) + ' ' + RIGHT(CONVERT(VARCHAR, GETDATE(), 100), 7)
you can make it on one line, of course, just did on 2 for clarity
forgot to add that you need to change the return variable to varchar, instead of datetime.
If you cant do that, maybe you can add the convert to the places that are calling the function (which would be worst, for sure)