Compare dates in MS SQL using Convert - sql-server

I want to do the correct way to get a date from a #parameter that contains the complete datetime, that user gets from a calendar from TFS File.
In the select I would want to use something like CONVERT(varchar(10), #FechaHasta.Value, 120) AS DATE01 and then get the only date, throwing out the time from the parameter.
For the next step, I would want to compare it with another date in WHERE clause, having this code :
Then, I would want to make this work on for looking for between two dates, and the last one, throwing out the time from the datetime.
Thanks.

I am assuming #FetchHasta is a datetime
CONVERT(VARCHAR(10), #FetchHasta, 101)
is what gets you just the date part. i.e. 10/28/2014 1:10 PM would simply become 10/28/2014.
Source
EDIT: Alternatively, How to return the date part only from a SQL Server datetime datatype

Related

T SQL Conversion failed when converting date and/or time from character string from VARCHAR(MAX)

I'm using SQL Server 2014. I have a date stored as varchar(MAX) in the format of:
2019-02-18
However, I want it in the British format dd/mm/yyyy (103).
This is my SQL:
SELECT CONVERT(DATE, DateField, 103) AS "JobStartDate"
FROM tblTest
However, I keep getting this error:
Conversion failed when converting date and/or time from character string.
What am I missing?
Update: The date is initially stored as varchar max as it is coming from a 3rd party system. I have no control over this and I completly understand this is the wrong format, but this is what I have been given.
I have a date stored as varchar(MAX)
There's your problem right there.
Not only you are using the wrong data type to store dates, you are also using max which is a known performance killer.
The solution to the problem is to alter the table and store dates in a Date data type - but first, you must look up all the objects that depends on that column and make sure they will not break or change them as well.
Assuming this can't be done, or as a temporary workaround, you must first convert the data you have to Date, and then convert it back to a string representation of that date using the 103 style to get dd/mm/yyyy.
Since yyyy-mm-dd string format is not culture dependent with the date data type, you can simply do this:
SELECT CONVERT(char(10), TRY_CAST(DateField As Date), 103) As [JobStartDate]
FROM tblTest
Note I've used try_cast and not cast since the database can't stop you from storing values that can't be converted to dates in that column.
You want to format the DateField column and not convert it to date.
So first convert it to DATE and then apply the format:
SELECT FORMAT(CONVERT(DATE, DateField, 21), 'dd/MM/yyyy') AS JobStartDate
See the demo.

How to add 1 day from a user defined date in SQL?

What I understood in SQL is if you add 1 day from today's date, you can use
SELECT #date = CONVERT(VARCHAR(8), DATEADD(DAY, 1, GETDATE()), 112)
but my issue is, I am not getting today's date. I have a user defined field for date which I was the one who set this date.
Example: my column is name is "Return Booked", how shall I add 1 day to every return booked I have set?
See attached: SQL Server result
Also, if I try to use CONVERT (DATEADD, *****) I have an error below:
How to deal with the error: Conversion failed when converting date and/or time from character string.
My current SELECT statement is
SELECT dbo.AdditionalDetailInfo.UserDefined6 AS ReturnBooked
Note that the data type of UserDefined6 is nvarchar. :(
Thank you!
You need to change the GETDATE() in the snippet to your value, and it is not a good practice to keep date values in nvarchar, so you will need some conversion first.
something like that:
SELECT CONVERT(VARCHAR(8), DATEADD(DAY, 1, (CONVERT(DATETIME,UserDefined6))),112) AS ReturnBooked
FROM dbo.AdditionalDetailInfo
As datatype of your column is nvarchar, Dateadd function on nvarchar will fail, so First you need to convert the column value to datetime and then use the DateAdd function, like below
Select DATEADD(DAY, 1, CONVERT(Datetime,ReturnBooked))
After looking at the image of sample data, it seems you have a row with date text in wrong format 08/010/2018. The conversion will fail for this particular row, so recommended to fix such bad data.

Converting Cisco CDR DateTime and Duration in SSRS

I am creating a report in SSRS that pulls data including Call Date and Duration from a table with Cisco CDR data. Both fields store the data as a bigint datatype. I understand that the Date/Time field is Unix Epoch Time.
My challenge is that I need to display the date/time and duration correctly in the report but I am not sure how to create the appropriate expression in SSRS to accomplish this. I will also need to enable the user to run the report by entering Start and End Date parameters. I would greatly appreciate some assistance
You could use the Convert Function to get what you want when pulling it in.
SELECT CONVERT(Datetime, <your date field>, 120)
120 is the date style you want. 120 specifically is yyyy-mm-dd hh:mi:ss(24h)
All the styles are found here https://msdn.microsoft.com/en-us/library/ms187928.aspx
Parameters are easy either in your data set create a stored procedure and add them into the stored procedure. Then they will get created automatically. If you are using plain text query then in your query then:
DECLARE #StartTime DATETIME
DECLARE #EndTime DATETIME
Then be sure to use them in you where clause with a between statement:
WHERE CONVERT(Datetime, <your date field>, 120)
BETWEEN CONVERT(Datetime, #StartTime, 120) AND CONVERT(Datetime, #EndDate, 120)
This way you can be sure to keep all your dates the same format so you get the correct results in your query.
If you are using plain text after this go to the parameters tab right click and create new parameter and then name it #BeginningDate and select DATE\TIME data type then do the same for the seocnd calling it #FinishingDate. After you create those right click on your Data set and go to properties and then go to the parameters tab and under parameter name enter #StartDate and paramter value type [#BeginningDate] this means the value of #BeginningDate and then do the same for #EndDate and #FinishingDate then it should work. Date/time parameters create a date picker automatically in the report.
You can use
DATEADD(s, epoch, '1970-01-01 00:00:00')

How to make SQL Server to save datetime with AM/PM format?

I'm trying to make my SQL Server table datetime columns save datetime with AM/PM. How to make SQL Server to save datetime with AM/PM format?
Right now it saves date like this: 2012-01-23 14:47:00.000
Is it possible to save it 2012-01-23 02:47:00.000 PM ??
Or does SQL Server save the date and time in this format (2012-01-23 14:47:00.000) all the time and I need to convert it just on output and input?
Is it even possible to save it in this format (2012-01-23 02:47:00.000 PM)? Or does SQL Server save datetime in 24 hour format?
thanks indeed for any help. sorry for language. ;)
Internally the date and time are stored as a number.
Whether it's displayed in a 12 or 24 hour clock is up to the program formatting it for display.
As Andrew said, Datetime format is stored not as string. so, you can use CONVERT function to get the datetime value in approprate format. for example,
SELECT CONVERT(VARCHAR(20), GETDATE(), 100)
to learn more about datetime formatting, see this article
AM/PM serves only for visualization, if you need to display them, use CONVERT keyword:
SELECT CONVERT(varchar, YourDateTimeField, 109)
FROM YourTable
If you need to store AM/PM - it is makes no sense for datetime type, use varchar type instead.
You can simply use CONVERT function as following:
select CONVERT(VARCHAR,GETDATE(),108)
http://www.fmsinc.com/free/NewTips/SQL/AM_PM_time_format_in_SQL.asp
http://msdn.microsoft.com/en-us/library/Aa226054
http://blogs.msdn.com/b/kathykam/archive/2006/09/29/773041.aspx
Depending on the accuracy of the datetime you are storing you might be able to clean it up with
REPLACE(CONVERT (varchar, YourDateTimeField, 109), ':00.0000000', ' ')
This will not work if your date field is populated with GETDATE() as that means it will contain seconds and milliseconds but it will work if the field is populated by a user and seconds and milliseconds are all zeros

SQL Server 2008 script - how to to acquire current date from system and store it into a date column

Hey fellas, I'm having difficulty obtaining only the date from the system and inserting it into a column, is there a built-in function that can acquire it?
On top of that, how do I add years to the current date?
I know I'm pushing it right now, but I'm also wondering what's the format for the date datatype?
Because sometimes I'd like to manually insert values into a column with that type in mind.
Any help would greatly be appreciated.
Thanks.
To get date only (SQL Server 2008 only) CAST to date type
SELECT CAST(GETDATE() AS date)
To add years, use DATEADD
SELECT DATEADD(year, 2, CAST(GETDATE() AS date))
Formats: use yyyymmdd or ISO yyyy-mm-dd (for newer datetime types) for safety.
Read this for everything about date+time in SQL Server
To add a year to the current date, look at the dateadd() function.
To just get the date from sql w/o the time, you can do this:
DECLARE #Date DATETIME
SELECT #Date = CONVERT(VARCHAR, GETDATE(), 101)
SELECT #Date
Sql will implicity convert the VARCHAR back to DATETIME. Look up the CONVERT function in BOL and it will give you all kinds of different styles for the 3rd parameter.
Bender

Resources