how to compare date using where statement? - sql-server

i have a table called dbo.reminder in that i'm having a column name rdate i have declared that as
NVARCHAR(50)
i want to sort the table using where condition with the present date how can i do that?
i have tried this query
SELECT rdetail,
rid,
rdate = CONVERT(VARCHAR, CONVERT(DATETIME, rdate, 103), 103)
FROM dbo.reminder
where rdate='23/01/2017'
but its not sorting please help me regarding this ?

Do not store date as NVARCHAR. Change your rate datatype to date and try this way
SELECT rdetail,rid,rdate
FROM dbo.reminder
where rdate = convert(date,'23/01/2017' ,103)
Convert the varchar date input to date and check with your rate column.
Better to pass imput in YYYYMMDD or YYYY-MM-DD format which is universal and does not require any conversion
If you cannot change the datatype then
SELECT rdetail,rid,rdate
FROM dbo.reminder
where convert(date,rdate,103) = convert(date,'23/01/2017' ,103)

Related

sql CONVERT to datetime does not change printed date format

I have a column in a table updatedDate - which is a datetime data type.
Data sample:
2017-10-15 18:08:22.000
2017-10-15 18:07:44.000
2017-10-15 18:07:17.000
2017-10-15 18:07:10.000
2017-10-14 18:00:54.000
2017-10-13 17:59:23.000
2017-10-13 17:59:13.000
I would like to display a list of DISTINCT dates, in the format of dd/mm/yyyy, but for the life of me... I can't get it. I would think it should be:
SELECT DISTINCT convert(datetime,updatedDate,103)
FROM [tblStudentCourses]
ORDER BY updatedDate DESC
But it does not actually convert to the 103 format... it just gives it to me as the full date and time format as originally, without any CONVERT.
What I want to get would be:
15/10/2017
14/10/2017
13/10/2017
What am i doing wrong?
Thanks!
To display the date in DD/MM/YYYY format, you can use CONVERT(VARCHAR(10), DateColumn, 103).
To maintain the proper ordering, you can wrap it all in a subquery. For example:
SELECT DisplayDate
FROM (
SELECT DISTINCT
DisplayDate = CONVERT(VARCHAR(10), CAST(UpdatedDate AS DATE), 103),
ActualDate = CAST(UpdatedDate AS DATE)
FROM [tblStudentCourses]
) AS T
ORDER BY ActualDate;
Note: Cast the date column to date if it's datetime like your sample data.
SELECT DISTINCT convert(VARCHAR,updatedDate,103)
FROM [tblStudentCourses]
ORDER BY updatedDate DESC
You should use convert to varchar for formating to various date formats

How to search by Date in this DD/MM/YYYY format from TimeStamp column in SQL Server?

I have a table called Transaction. One of the column name is Time,datatype is TimeStamp. So the data is looking like this 2015-01-14 23:22:11.000.
Now I want to search by Date in where clause in this format DD/MM/YYYY
for example
select *
FROM Transaction
WHERE Date='25/12/2016' // DD/MM/YYYY
Thanks
Your question is not completely clear, but it appears that you have the need to compare date data in the format DD/MM/YYYY against a timestamp column in your table. One option is to use the CONVERT function to convert both the input and your timestamp column to a common DATETIME format, and then do the comparison.
SELECT *
FROM Transaction
WHERE CONVERT(DATETIME, DATEDIFF(DAY, 0, TIME)) = CONVERT(DATETIME, '25/12/2016', 103)
This will return all records whose date components are '2016-12-25'. Note that both sides of the comparison would have a time component set to midnight of that day.
select *
FROM Transaction
WHERE Date=CONVERT(NVARCHAR(50), '25/12/2016', 103) DD/MM/YYYY
Use canonical format (YYYY-MM-DD) on date columns:
SELECT *
FROM Transaction
WHERE Date=convert(datetime, '2016-12-25')
You can also use time ranges with canonical format YYYY-MM-DD HH:MI:SS:
SELECT *
FROM Transaction
WHERE Date BETWEEN convert(datetime, '2016-12-25 00:00:00')
AND convert(datetime, '2016-12-25 23:59:59')
You have to pass date in dd/mm/yyyy format, take parameter datatype as varchar and convert date like in the following SQL statement
SELECT CONVERT(varchar(25),CreateDate,103) as CreateDate, [UserID],[FirstName]+' '+[LastName] AS 'Name',[NomineeName],[City],[UserDocument]
FROM [dbo].[UserMaster]
WHERE CONVERT(varchar(25),[CreateDate],103) between convert(varchar(25),#datefrom,103) and convert(varchar(25),#dateto,103)

Convert and sort varchar Date dd-MMM-yyyy

I'm using a table that has the date as varchar. example dateformat:
17-Jun-2015
I have tried the following ways to convert and sort the date(dd-MMM-yyyy) to dateTime.
SELECT date, name, author
FROM sometable
ORDER BY CONVERT(DATETIME, date, 106) DESC
I have also tried converting the date in the select statement. doesn't work. The error is
Conversion failed when converting date and/or time from character string.
The conversion types through some similar questions but I have not found any solutions to the format I have. Is there some way of selecting the delimiter between the day month and year??
I also had a browse through this link which has the formats for datetime formats. 106 was the closest to my varchar date. Only my date in the table has '-' between day month and year.
https://msdn.microsoft.com/en-us/library/ms187928.aspx
Appreciate any help.
It should be absolutely fine to use 106 to convert your date format.
But I guess your table contains some of the invalid values in the column causes the error, try to spot them out by TRY_CONVERT:
SELECT date, name, author, TRY_CONVERT(datetime, date, 106) AS convertresult
FROM sometable
WHERE TRY_CONVERT(datetime, date, 106) IS NULL AND date IS NOT NULL
I would use date style 113 to convert to datetime format like this:
DECLARE #date VARCHAR(20) = '17-Jun-2015';
SELECT CONVERT(VARCHAR(20),CAST(#date AS DATETIME),113)

how to cast or convert numeric/varchar to date data type field in sql and use in update statement?

Good day!
I have 2 questions how to update a date data type column field using varchar and numeric column field
1.)mydate varchar(8)--> varchar column field
SELECT mydate from mytable
Result: 20141120
my question is how can I update my date column field using my varchar column field using cast or convert
update table2
set date = (select mydate from mytable)
which I get an error!!! and I'm stuck.
2.)mydate numeric(8) --> numeric column field
SELECT mydate from mytable
Result:
20101015
20140910
etc.......
update table2
set date = (select mydate from mytable a, mytable2 b
where a.id=b.id)
my question is how can I update my date column field using my numeric column field using cast or convert
I used different CAST and CONVERT but still I'm getting error!
What is the correct syntax for this?
Thank your for your help!
To convert a string to a date field you will need to use the CONVERT function:
CONVERT(datetime, mydate, 101)
This is expecting a string field, so if your mydate field is really a numeric field then you will need to CAST that to a string, so the CONVERT command will then look like:
CONVERT(datetime, CAST(mydate as VarChar), 101)
The third parameter of the function is determined by the format of the date in the previous parameter, you can find the full list on MSDN at http://msdn.microsoft.com/en-us/library/ms187928.aspx

SQL Server 2008 - date format issue while inserting

I am trying to insert date into a table but the date and format of inserted date is messed up. The datatype in the table is Date. My insert script is as below.
insert into Trans(ID, TDate, Description)
values(1, CONVERT(datetime, 25-02-2012, 101), 'Opening')
I am trying to insert in dd/MM/yyyy format and I want it in the same format in my table. But in my table the date is 1894-07-22 !!
I want the date to be inserted exactly as the format I wish and I want to see the inserted date as 25-02-2012 in the table.
What is wrong here ? Can somebody help ?
Try CONVERT(datetime ,'25-02-2012', 103)
use some single quotes around the value
choose your format accordingly (101 is US, meaning mm/dd/yyyy)
see http://msdn.microsoft.com/en-us/library/ms187928(v=sql.100).aspx for more details
you should use single quotes around your date. If you want date format of dd/mm/yyyy format then you will want to use the convert(datetime, '25-02-2012', 103)
insert into Trans(ID,TDate,Description)
values(1,CONVERT(datetime,'25-02-2012',103),'Opening')
if you use convert(varchar, getdate(), 101) the format of the date will be mm/dd/yyyy.
There are several helpful links to use as a reference for datetime conversions:
How to format datetime & date
Tips & tricks SQL Server Date formats
insert into emp
(EMPNO,ENAME,DEPTNO,JOB,SAL,COMM,MGR,HIREDATE)values
(7839,'KING',10,'PRESIDENT',5000,NULL,NULL,'17-11-81')

Resources