SQL Server date conversion not is formatting the output - sql

My SQL statement is supposed to add 5 days to the date column and reformat the output to MM/DD/YYYY.
The calculation is working, but the format won't change, no matter what value I use.
The date column is defined as varchar(128) (I've used datetime and varchar(128) in the convert statement).
What am I doing wrong?
SELECT
FSI_Date, DATEADD("d", +5, CONVERT(DATE, FSI_Date, 101)) AS NEWDAY,
FROM
DB_test
FSI_Date NEWDAY
---------------------------
12/12/2015 2015-12-17

SQL Server will convert mm/dd/yyyy to a date, so, you just need the dateadd() and then convert back to your desired format.
Remove the final convert(...,101) if you want a natural date.
Example
Select FSI_Date
,NewDay = convert(varchar(10),DateAdd(DAY,5,#FSI_Date),101)
From DB_Test
Returns
FSI_Date NewDay
12/12/2015 12/17/2015

Related

Convert decimal to date format SQL

I am trying to convert a column FC_FROM_DATE containing decimal values (ex. 20,200,721) into a date format 2020/07/21.
I have tried this code
SELECT TO_DATE(CHAR(CAST(FC_FROM_DATE AS DECIMAL(8,0))), 'YYYY/MM/DD')
FROM MARKETS.FORECAST
I get an error
Argument for chr should be between 0 and 127
Would much appreciate your help!
If you are using Oracle then you can use the following query:
SELECT TO_DATE(CAST(FC_FROM_DATE AS VARCHAR(8), 'YYYY/MM/DD') FROM Table
Seeing as the persisted format is YYYYMMDD you could convert the value to a varchar(8) and then use CONVERT to get a Date instance.
SELECT CONVERT(DATE, CAST(FC_FROM_DATE AS VARCHAR(8))) FROM MARKETS.FORECAST
Ideally Dates are stored as Date and a Date with a time component is stored as DATETIME2 (or equivalent if not Sql Server).
Test of the code above
DECLARE #FC_FROM_DATE decimal(8,0) = 20200721
SELECT CONVERT(DATE, CAST(#FC_FROM_DATE AS VARCHAR(8)))
2020-07-21
Disclaimer: This works in MS Sql Server.

MM/dd/yyyy datetime data type resulted in an out-of-range value

I am inputting 2 date values in order to filter out from SQL query.
EXEC [Report].[usp_EmployeeReport_Detail] '01-01-2017','31-08-2019'
I am inputting the date as MM/dd/yyyy and the
WHERE clause contains
(j.StartDate BETWEEN #BegDate AND #EndDate)
I m getting the below error when executing the query.
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
I tried to convert the datetime result using the below way as well.
SELECT CONVERT(datetime,01-31-2017,101)
This returns 1894-05-25 00:00:00.000 as the response. How its possible?
As #Damien_The_Unbeliever has said,01-31-2017 using the Numerical expression, which evaluates to -2047.
-2047 means the day minus 2047 days from 1900-01-01, so result of date will be 1894-05-25 00:00:00.000.
So your query SELECT CONVERT(datetime,01-31-2017,101) same as SELECT CONVERT(datetime,-2047,101)
sqlfiddle
You can use the ANSI compliant format YYYYMMDD
SELECT CONVERT(datetime,'20170131',101);
instead of
DD-MM-YYYY 01-31-2017.
Or just add ' to contain the date '01-31-2017' like #fa06 answered.
Try this: u've missed the quote in date:
SELECT CONVERT(datetime,'01-31-2017',101)
try below way
SELECT CONVERT(CHAR,GETDATE(),1 )
it returns 08/13/18
in your case you missed the quote as a result output that type shown

Convert TEXT to Date in SQL Server 2012

I have a TEXT in this format 31/10/15.
How do I convert this into a DATE format?
As I need to let the user search from data using a date range.
example: From 15/7/13 to 31/10/15
Or is there a way to so without converting to date?
You can use CONVERT() for this:
DECLARE #d VARCHAR(50) = '31/10/50'
SELECT CONVERT(DATE, #d,3)
Note that with a 2-digit year SQL Server will make the year start with '19' for 50 and up, and 49 and below will be '20'
Storing as a DATE field will allow easier comparisons, otherwise you'll have to perform this conversion at each step.
Use CONVERT; example:
SELECT [Date] = CONVERT(date, '31/10/15', 3);
And yes, it's possible to search dates in the same format as the examples you provide, but don't do that – use the proper data types in both your queries and your table columns.

Datetime interpretation issue

I have the following data:
StartDate FinishDate Details
09/10/2013 11/10/2013 xxx
14/10/2013 13/10/2014 Taking a year off
Whilst editing this data I which to check the date ranges do not overlap.
I am running an SQL query from access via ado to do this; I am putting the dates entered into database format (ie 'mm/dd/yyyy'); This is the query I've got:
SELECT Count(*)
FROM MarkerAbsence
WHERE PerID = 718 AND
('10/09/2013' BETWEEN StartDate AND FinishDate OR
'10/11/2013' BETWEEN StartDate AND FinishDate)
If the data is valid, it should return zero records; however it doesnt it returns 1 (being the second listed record above) and therefore seems to be interpreting '10/11/2013' as dd/mm/yyyy instead of mm/dd/yyyy.
Yet if I do this in SMO:
DECLARE #datevar datetime2 = '31/12/2008';
SELECT #datevar;
I get:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
While
DECLARE #datevar datetime2 = '12/31/2008';
SELECT #datevar;
returns
2008-12-31 00:00:00.0000000
So why am I having this problem and how do I fix it?
If you're running a query using MS Access, you need to delimit dates with # symbols, i.e.: #12/31/2008#. If this won't work for whatever reason, it is best to use string dates in the 'yyyy-mm-dd' format, as it will be recognized and is unambiguous.
You're probably getting this problem as MS is a US company, and the US uses mm/dd/yyyy format, so MS has defaulted much of their older software to treat dates as being in this format if at all possible, whereas you're probably in a country that uses - and have your PC's locality set to use - dd/mm/yyy format. Since not all of MS' software follows this rule, you have this problem.
The solution is to use a string date format that is unambiguous, such as: yyyy-mm-dd, mmm/dd/yyyy, or dd/mmm/yyyy (where mmm returns a three-letter month such as Dec).
You are using dd/mm/yyyy formats for your date strings. By default, without an explicit conversion, SQL is expecting date strings in the mm/dd/yyyy or yyyy-mm-dd format. So either change your strings to match one of these formats or do this:
SELECT Count(*)
FROM MarkerAbsence
WHERE PerID = 718 AND
(CONVERT(DATETIME, '10/09/2013', 103) BETWEEN StartDate AND FinishDate OR
CONVERT(DATETIME, '10/11/2013', 103) BETWEEN StartDate AND FinishDate)

how to convert nvarchar to time ,not datetime?

DECLARE #DateNow smalldatetime
SET #DateNow='12:30'
select #DateNow
-------------------------------------OR--------------------------------------
select CAST( '12:30' as datetime )
Result: 1900-01-01 12:30:00.000 (i don't want this)
But i need this result in time format not string not datetime?
Result: 12:30 (i want this)
Like José said, you can use CONVERT to display a datetime as date. MSDN has a list of all possible formats. For example format 8 is hh:mi:ss:
select convert(varchar(32),getdate(),8)
12:51:21
Now, you can cut the seconds off by specifying less characters:
select convert(varchar(5),getdate(),8)
12:51
Another often used format is 121, yyyy-mm-dd hh:mi:ss.mmm(24h):
select convert(varchar(32),getdate(),121)
2009-05-08 12:51:21.987
Where you can pick the time part like:
select substring(convert(varchar(32),getdate(),121),12,5)
12:51
Or to combine the string trickeries:
select right(convert(varchar(16),getdate(),121),5)
12:51
Right? Right!
There's a TIME type in SQL Server 2008, but in previous versions, you can represent it as a varchar for display.
This is how you can retrieve the time portion of a DATETIME field in the format you want.
DECLARE #DateNow smalldatetime
SET #DateNow = GETDATE() -- 2009-05-08 12:58:02.680
SELECT CONVERT(CHAR(5), #DateNow, 8)
-- this returns the time portion as 12:58
You can use the CONVERT function.
By the way, there's no "TIME" datatype in SQL Server. So, the converted result will always be a STRING.
EDIT: There's no "TIME" datatype in SQL Server versions < SQL Server 2008.
If you need to compare, add and/or subtract time (only) values, think about the possibility to use a INT to store the "TIME" value and then to CONVERT it to CHAR when displaying the result.