I've got a query that get's a date of a field in a program. This date has to be modified with 10 years.
The query I made is
SELECT DATEADD(yy, +10, '"+thisfield.value+"')
where '"+thisfield.value+"' is coming from the program and is filled in like 01-08-2012.
The result of the query is 2022-07-31 00:00:00.000. The problem I have is that I just need 2022-08-01 but in the format of 01-08-2022 so that I can automatically fill an other field with this result.
In SQL Server 2005 the date function doesn't work only the datetime function and I just don't need that.
I hope this is clear (first time i post something). Can anyone help me?
You can either truncate the result, or cast it to DATE
CAST(DATEADD(yy, +10, '"+thisfield.value+"') AS DATE)"
CONVERT(VARCHAR, DATEADD(yy, +10, '"+thisfield.value+"'), 101)"
CONVERT using style 101 is in the format mm/dd/yyyy, which happens to be the format you want. Keep in mind that you can format the result however you want in your application for display purposes which is better than returning strings from SQL Server.
Also note that you should look into parameterized queries and/or stored procedures.
Related
Im using vb.net code with Sql Server 2008 R2.
I'm trying to get result by using a SQL query to get all rows that have a value between 2 dates:
Here is my the where clause of my statement:
Where (CONVERT(varchar(10), visit_nextVisitDate, 103) between '02/04/2017' AND '15/05/2017')"
but I always get all rows for the same month (month 4).
I tried this:
WHERE (CAST(dbo.Visits.visit_date AS date) BETWEEN '24/04/2017' AND '02/05/2017')
but I got an error cause my date fields are saved in format yyyy/mm/dd
How can I change the SQL date format to dd/mm/yyyy?
Why would you do date comparisons using strings? That is just wrong, wrong, wrong. (If you do it, use ANSI standard formats, YYYY-MM-DD so the comparisons are correct.)
Just do this using dates:
Where visit_nextVisitDate between '2017-04-02' AND '2017-05-02'
Actually, it is a bad idea to use between with dates. Aaron Bertrand has a very good blog on this subject.
I recommend:
Where visit_nextVisitDate >= '2017-04-02' AND
visit_nextVisitDate < '2017-05-03'
I am bit confusing here?
declare #date1 datetime = '2016-01-21 14:10:47.183'
I want to convert '2016-01-21 14:10:47.183' To '21-01-2016'
when I tried: select convert(date,#date1,105)
I am getting: 2016-01-21
But with: select convert(varchar(10),#date1,105)
I am getting: 21-01-2016
Why I am not having same results with above code?
Why should I convert to varchar?
Thanks in advance
This is just presentation matter and should be done in application layer. If you cannot do it in application you could use FORMAT (SQL Server 2012+):
declare #date1 datetime = '2016-01-21 14:10:47.183'
SELECT FORMAT(#date1, 'dd-mm-yyyy');
LiveDemo
Why I am not having same results with above code?
select convert(date,#date1,105)
-- DATETIME -> DATE
-- vs
select convert(varchar(10),#date1,105)
-- DATETIME -> VARCHAR(10) using specific style
If you only to skip time part use SELECT CAST(#date1 AS DATE) and do not bother how it is presented. It is still DATE.
To sum up: in SQL query use DATE as date, in application display it with desired format.
The reason why is because once you put a value in a datetime column (or date or any of the other variations on date-time datatypes) in SQL Server. SQL Server ceases to think of that date as having any particular format. It translates it into numbers, and stores it that way internally.
So when you select a date from a date time column, SQL Server displays it in the default format that you have selected based on your environment/local settings.
If you want to display it in any other format, you have to first convert it to a string, because as far as SQL Server is concerned, dates don't have formats. They are just numbers. The 21st day of March is the 21st day of March, whether you write it as 3/21 or 21/3.
So when you try to convert a date to a date with a different format, SQL Server just ignores you because dates don't have formats. However, if you want to convert that date to a string, SQL Server will be happy to help you display that string in any format you like.
Hope this helps, but sounds like some further research into how SQL Server stores dates would help your understanding.
How can I display data type TIME in SQL as 09:00 or 09:00AM instead of 09:00:00.000000
For instance, I have a column start_time and its type is TIME
When I insert data in it, INSERT INTO Time(start_time) VALUES('09:00:00')
It displays it as: 09:00:00.0000000
You need to format dates as per your requirement.
have a look at
http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
or
http://www.w3schools.com/sql/func_convert.asp
sample
SELECT CONVERT(VARCHAR(10), SYSDATETIME(), 101) AS [MM/DD/YYYY]
SELECT CONVERT(VARCHAR(10), SYSDATETIME(), 103) AS [DD/MM/YYYY]
..........
You need to take a look at the Format function in T-SQL. This will allow you to format date and time values (and another values) any way you want to. The link I provided is for SQL 2014. Format was added in version 2012. You have your question tagged as 2008, so I'm not sure this answer will apply to you. If not, take a look at Cast and/or Convert here.
By the way, you may not know this but how you enter a date/time value into SQL Server has no relationship to how it is displayed. And, generally, the displaying of date and time values is done in some UI tier of an app, and not at the SQL Sever level, even though T-SQL provides methods for formatting dates and times.
I have an SSRS report that you can click on to drill down and see more details about a cell. To do this, when you click a cell it passes parameters that populate a SQL statement in another report.
One of the parameters that it passes is a short date, mm/dd/yyyy. My problem is when month or day do not have double digits. So....
Short date = 1/1/2013
SQL 101 format = 01/01/2013
When the short date is passed, it doesn't match the SQL version exactly so it doesn't find any records when it should.
This returns nothing:
where convert(char(10),dt.DATE_CREATED, 101) like '1/1/2013'
BUT, this returns all of the rows I need:
where convert(char(10),dt.DATE_CREATED, 101) like '01/01/2013'
My problem is I need to somehow do this conversion of adding these zeros within SSRS...
You should be able to use the VB function DateTime.Parse
DateTime.Parse(Fields!yourValue.Value)
This fixed my problem:
convert(char(10),dt.DATE_CREATED, 101) = Convert(datetime, #day)
I have an ASP page which will fetch records from a SQL server DB table. The table "order_master" has a field called order_date. I want to frame a select query to fetch order date > a date entered by user(ex : 07/01/2008)
I tried with convert and cast, but both are not working. The sample data in order_date column is 4/10/2008 8:27:41 PM. Actually, I dont know what type it is (varchar/datetime).
Is there any way to do that?
I'd check to make sure that the SQL datatype is a DateTime or SmallDateTime first, then I'd check to make sure that you're passing in a Date/DateTime value from the page.
If those are both correct, then you'd probably be better off following Joel's advice and explicitly convert both values to dates before trying the comparison. Also, check the precision of the time values that you're looking at; it seems obvious, but 1/1/2008 12:00:00.001 AM will not be equal to 1/1/2008 12:00:00.000 AM. Yes, I am speaking from experience. :P
the 07/01/2008 date is the British/French annotation, so all you need to do is:
SELECT myColumn FROM myTable WHERE myDateField >= convert(datetime, '07/01/2008 00:00:00', 103)
this code will get all rows where myDateField has the date 7th of January 2008, since 00:00:00 (hh:mm:ss) so, the first second on that day... in simple words, the entire day.
for more info, check Books online on MSDN
You could create a stored procedure like this
CREATE PROCEDURE GetOrders
#OrderDate DATETIME
AS
SELECT
*
FROM order_master
WHERE Order_Date > #OrderDate
GO
Then you can just convert the users input to a date before calling the stored procedure via your ASP code.
Edit
I just noticed the remark about the column type, you can run this command
sp_help order_master
to get column information to find the data type of order_date.
Have you tried CONVERT()'ing both values to a datetime type?
Remember that when comparing dates, 4/10/2008 8:27:41 PM is not equal to 4/10/2008. SQL Server will interpret 4/10/2008 to mean 4/10/2008 12:00:00 AM, and do an exact comparison down to the second. Therefore 4/10/2008 is LESS THAN 4/10/2008 8:27:41 PM
You state you don't know the field type. That would be the first problem to solve, find out. You can do that with:-
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Order_Master' AND
COLUMN_NAME = 'Order_Date'
If its not one of the datetime types it should be converted, if that isn't your responsibility then get on to someone who does have the responsibility.
The fact that you are concerned about the 'format' of the date indicates that you may be building the SQL using concatenation. If so stop doing that. Use a command with a parameter and pass in the date as date type.
Now your issue is one of how the date is entered at the client end and getting it into an unambigous format that can be parsed as a date in the ASP code.
If that is not something you have solved add a comment to this answer and I'll expand this answer.