Table:
Dates (nvarchar)
==
23/02/2009 (dd/mm/yyyy)
24/02/2009
25/08/2009
28/12/2011
....
I want to change the date form like this (yyyymmdd)
Tried query
select cast(dates as datetime) from table1
Showing error
ЮArithemetic expression overflow
select Convert(char(10), dates, 112) from table1
Showing the same value 23/02/2009
The above query is not working.
Expected Output
20090232
20090224
20090825
20111228
...
How to make a query?
Use this article.
select CONVERT(varchar(8),CONVERT(datetime, '23/02/2009',103),112)
Try this simple query:
SELECT DATEPART(yyyy,dates)+DATEPART(m,dates)+DATEPART(d,dates) FROM table1
You can get more information on this link:
sql-server-dates
select convert(varchar, getdate(), 102)[Dates] from tablename
Take a look on this link: http://www.technoreader.com/SQL-Server-Date-Time-Format.aspx
Since these are stored as string data then you might as well just use the string functions to give you what you want.
First to give you a valid date
select
cast(SUBSTRING(dates,4,2) + '/' + left(dates,2) + '/' + right(dates,4) as datetime) NowItsADateTime
from DateTable
And second, what you asked for.
select right(dates,4) + SUBSTRING(dates,4,2) + left(dates,2) as YYYYMMDD
from DateTable
Related
I am converting a date using CONVERT(varchar,DateOfBirth,101) for birthdates.
I want to show these dates with the current year, I've tried REPLACE but you can't use wildcards with it and when I use DATEPART, it doesn't format with the right digits for month and day. I also can't add years because they are wildly different birthdates. Thanks.
If you want to display the date as a string in 101 format for current year, one option uses a direct format():
format(DateOfBirth, 'MM/dd/2020')
You can compute the current date dynamically:
format(DateOfBirth, concat('MM/dd/', year(getdate())))
On the other hand, if you want your result as a date, then you could use datefromparts():
datefromparts(year(getdate()), month(DateOfBirth), day(DateOfBirth))
If it is a datevalue, you can use FORMAT function. If it is a character value, you can use RIGHT and REPLACE.
DECLARE #dateValue DATETIME = '05/12/1999'
DECLARE #dateCharValue VARCHAR(12) = '05/12/1999'
SELECT FORMAT(#dateValue, 'MM/dd/2020')
SELECT REPLACE(#dateCharValue, RIGHT(#dateCharValue,4),2020)
--Result
05/12/2020
This could helped you:
The code CONVERT(varchar(5),GETDATE(),1) return this 05/27 and then just add the year of the date
SELECT CONVERT(varchar(5),GETDATE(),1) + '/' + cast(year(getdate()) as varchar)
Or
SELECT CONVERT(varchar(5),GETDATE(),1) + '/' + convert(varchar,year(getdate()))
The result of both:
05/27/2020 --(This is my current date n.n )
This work but if you use a string something like your example DateOfBirth will be the variable and if this is a string (DateOfBirth = '5/27/1987') you need to convert the string DateOfBirth to Date:
SELECT CONVERT(varchar(5),convert(date,DateOfBirth),1) + '/' + cast(year(GETDATE()) as varchar)
Or
SELECT CONVERT(varchar(5),convert(date,DateOfBirth),1) + '/' + convert(varchar,year(GETDATE()))
The Result of Both :
05/27/2020
I want to so something like the following:
SELECT * FROM TABLE1
WHERE DATE1 LIKE DATE2 + '%'
However, when I try this I get the following error:
"5407: Invalid operation for DateTime or Interval"
I am working in Terdata SQL Assistant
You can't use like to compare dates, like is to compare string (varchar), then you can cast this dates to varchar or better way is cast both dates to the same format, for example:
SELECT * FROM TABLE1
WHERE convert(varchar, DATE1, 103) = convert(varchar, DATE2, 103)
This way cast dates to format: DD/MM/YYYY
If you wanna cast to another formats I let you a link that explain more types: https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
You could potentially just format the dates the same way to compare them.
WHERE FORMAT(DATE1, 'dd/mm/yyyy') = FORMAT(DATE2, 'dd/mm/yyyy')
date2 is less than 1 day later
where datediff(d,date1,date2) < 1
Since your using like ... you could mean within a day before or after.
abs(datediff(d,date1,date2)) < 1
I am using SQL Server 2008R2.
I am trying to get part of a date in an output, and my column is in datetime datatype.
Eg,
If Current date and time is 2016-06-28 17:34:12.060, then I need output as 17:00 only the Hour with :00 mins.
I have tried this until now,
Select DateName(HH,SUBSTRING('2016-06-28 17:34:12.060',12,5)) +':00'
which gives me right output.But when I pass Column Name which is of datetime datatype, then it gives error,
Select DateName(HH,SUBSTRING(TimeInHour,12,5)) +':00'
gives error,
Argument data type time is invalid for argument 1 of substring function.
I know I am using SUBSTRING() at wrong place, But I really don't know how to achieve that output.
A help will be much appreciable.I need output as HH:00, Hour will be anything but 00 mins.
Why would you use substring() at all? The second argument to datename() should be a date/time data type. So, just do:
Select DateName(hour, '2016-06-28 17:34:12.060') + ':00'
Try this:
Select CAST(DATEPART(hour,'2016-06-28 17:34:12.060') AS VARCHAR(2)) +':00'
Below is the code that might be helpful
SELECT CONVERT(VARCHAR(50),DATEPART(YY,'2016-06-28 17:34:12.060')) -- Year
SELECT CONVERT(VARCHAR(50),DATEPART(mm,'2016-06-28 17:34:12.060')) -- Month
SELECT CONVERT(VARCHAR(50),DATEPART(d,'2016-06-28 17:34:12.060')) -- Day
SELECT CONVERT(VARCHAR(50),DATEPART(HH,'2016-06-28 17:34:12.060'))+':00' -- Hour
SELECT CONVERT(VARCHAR(50),DATEPART(mi,'2016-06-28 17:34:12.060'))+':00' -- Minutes
SELECT CONVERT(VARCHAR(50),DATEPART(ss,'2016-06-28 17:34:12.060')) -- Seconds
SELECT CONVERT(VARCHAR(50),DATEPART(ms,'2016-06-28 17:34:12.060')) -- Millisecond
You need to cast your DATETIME type column first, Use CAST function
Select DateName(HH,SUBSTRING(CAST(ColumnName AS VARCHAR(30)),12,5)) +':00'
Or alternative to do is Use LEFT and CONVERT
SELECT LEFT(CONVERT(VARCHAR, ColumnName ,108),2)+':00'
select convert(varchar, datepart(hour, getdate())) + ':' + convert(varchar, datepart(second, getdate()))
I would like to compare today's date with the dates I'm pulling from a DB and select entries accordingly. Those two formats do not match (I assume so) and I get an error. By the way, I am not sure what exact format myDate is stored in. Below is what I would essentially like to achieve.
WHERE (myDate > CURDATE())
you can format both the dates as below .
select * from mytable where convert(char(8), myDate,112) > convert(char(8),myDate,112)
code 112 will case them to be converted in YYYYMMDD format
select * from mytable where
convert(date, myDate) > convert(date,myDate)
in sql server.
or, if that does not work, try formatting the date with substring and then converting/casting/parsing.
select convert(date, (substring(myfield,9,2) + '/' + substring(myfield,6,2) + '/' + substring(myfield,1,4))) from mytable
I have date in column of nchar(255) datatype in this format
mm/dd/yyyy
Now I want to change it into
dd/mm/yyyy
I don't think that CONVERT help me in this as I have tried different queries like below
SELECT CONVERT(NCHAR(20), col1, 103) from table1
SELECT CONVERT(VARCHAR(20), col1, 103) from table1
select Convert(varchar(10),CONVERT(date,col1,101),103) from table1
In my view 103 which is in UK format converts only from yyyy-mm-dd
So I tried to CAST my col1 but I get an error.
Am I missing something? Is there any easy way to do this?
Do this in two expiicit steps. First, convert the string to a date using the 101 format (which is mm/dd/yyyy). Then explicitly convert the date back to a string using 103 (which is dd/mm/yyyy):
select convert(varchar(255), convert(date, datecol, 101), 103)
Two points. First, I don't think the result needs to be nvarchar() (the string only consists of numbers and a slash). Second, always include a length when using varchar() in SQL Server.
Here is a little SQL Fiddle.
You can try this:
SELECT CONVERT(VARCHAR, CONVERT(DATETIME, col1, 101), 103)
Check this:
Select convert(varchar(15), cast('01/26/2015' as datetime), 103) as FormattedDate
Where, '01/26/2015' is your col1
Edited Answer:
Select convert(nchar(255), cast(col1 as datetime), 103) as FormattedDate From table1
Where table1 is your table.
I am answering my own question.(Just in case anyone wants to know what is the solution)
There was no such problem with the query i was using. i.e.
select Convert(varchar(10),CONVERT(date,col1,101),103) from table1
The problem was with my nchar field.
It was having a special character (in my case a space) in every entry which was giving "out-of-range" error when tried to convert or cast.
So removing Special character(a space) solved my problem.