Converting string to date. SQL - sql

I have table with column COL1(char 23) with full date e.g. '2007-11-13 12:34:49:012'. Now I want to change my date to '2007-11-12 59:59:59:999' (last moment in day before).
Why the convert paremeters dont work property? My query:
convert(varchar(10),convert(date,DateAdd(day,-1,COL1),121)) + ' 23:59:59:999'
After
DateAdd(day,-1,COL1)
i get: "Nov 12 2007" and this is my problem.
Finally i get: "Nov 12 200 23:59:59:999"

How about this?
select dateadd(millisecond, -1, dateadd(day, 1, cast(left(col1, 10) as datetime)))

Varchar(10) isn't quite ling enough for the data try varchar(11)

try this:
select cast(dateadd(ms, -1, cast(convert(varchar(8), cast(col1 as datetime), 112) as datetime2)) as varchar(23))

Related

How to Convert nvarchar (including time) into datetime

I have data 20160526094432, and I want to convert into datetime in SQLServer
The result will be 2016-05-26 09:44:32
Is there simple way to do that ?
Thanks
If you use MS SQL Server 2012 or newer then you can enjoy format function.
select cast(format(20160526094432,'####-##-## ##:##:##') as datetime) [date-time]
If your long number is a string then you have to convert it.
declare #d varchar(20)='20160526094432'
select cast(format(cast(#d as bigint),'####-##-## ##:##:##') as datetime) [date-time]
Hmmm. I don't think there is a really clean way, but something like this should work:
select (convert(datetime, left(col, 8) as datetime) +
convert(datetime, convert(time,
stuff(stuff(right(col, 6), 5, 0, ':'), 3, 0, ':')
)
)
)
Maybe you can try in this way, for example Date is the column of your table, 103 is the format of Date you want to convert, google for more details.
CONVERT(datetime, Date, 103)

convert datetime to display full month

I have searched and i cannot seem to find the answer. I have tried using different styles, but none of them give me exactly what i want.
I am trying to convert a datetime, that I have calculated. However I need it to display the full month.
SELECT CONVERT(VARCHAR(25),DATEADD(dd, #codelife, getdate()),107)
the above line works fine, EXCEPT it displays the date like
Feb 27, 2014
I need it to display date like
February 27, 2014 etc.....
any suggestions?
Nathan's answer seems solid, however if you're using SQL Server 2012 you can make use of the FORMAT() function:
SELECT FORMAT(GETDATE(),'MMMM dd, yyyy')
Or in your case:
SELECT FORMAT(DATEADD(dd, #codelife, getdate()),'MMMM dd, yyyy')
I always use http://www.sql-server-helper.com/tips/date-formats.aspx as my reference
SELECT DATENAME(MM, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]
I would declare your date as a variable and do something like:
DECLARE #myDate DATETIME
SET #myDate = DateAdd(dd, #codelife, getdate());
SELECT DATENAME(MM, #myDate) + RIGHT(CONVERT(VARCHAR(12), #myDate, 107), 9) AS [Month DD, YYYY]
You may have to do it in 2 steps.
DECLARE #codelife INT = 120
DECLARE #codedate DATETIME = (SELECT (DATEADD(D, #codelife, GETDATE())))
SELECT DATENAME(MM, #codedate) + SUBSTRING(CONVERT(VARCHAR(25), #codedate, 107), 4, 25)
SELECT DATENAME(MONTH, GETDATE())
+ RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]
OR Date without Comma Between date and year, you can use the following
SELECT DATENAME(MONTH, GETDATE()) + ' ' + CAST(DAY(GETDATE()) AS VARCHAR(2))
+ ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [Month DD YYYY]
I answered the same question a couple of days ago have a look here sql server Get the FULL month name from a date

How do I concatenate numbers to create a custom date?

Here is what I have tried thus far:
select CAST(
DATEPART(month,getDate())+'-'+
DATEPART(day,getDate())+'-'+
2012
as datetime)
I end up with the date: 1905-08-02 00:00:00.0. I was expecting to get today's date. I have rearranged the order and it doesn't seem to change. Can anyone offer as to why it gives me this? For the record, I plan to use other values than 2012 for the year.
Thanks in advance.
CAST() each piece as a varchar first:
select
cast(
cast(DATEPART(month,getDate()) as varchar(2))+'-'+
cast(DATEPART(day,getDate()) as varchar(2))+'-'+
'2012' as datetime)
select CAST ('2012'+
CAST(DATEPART(month,getDate()) as char(2))+
CAST(DATEPART(day,getDate()) as char(2))
as datetime)
You have to concatenate strings. Your code is casting the number 2039 to date.
If the goal with this little exercise is to be able to change the year of a given date you can do like this instead.
declare #NewYear int = 2003
-- with time part
select dateadd(year, #NewYear - year(getdate()), getdate())
-- time part removed
select dateadd(year, #NewYear - year(getdate()), dateadd(day, 0, datediff(day, 0, getdate())))
This code will work, you need to make sure that you are concatenating same data types and use convert with specific DateTime Format:
SELECT CONVERT(DATETIME,
CAST(DATEPART(month,getDate()) AS NVARCHAR(50))
+'-'+CAST(DATEPART(day,getDate()) AS NVARCHAR(50))
+'-2012'
,121)

SQL DATETIME Math

I am trying to do the following:
Format GetDate() to display only the minutes
Format a varchar column to display only the minutes
Subtract the current time HH:MM:SS from GetDate() and the VarChar column
This is what I have
CONVERT(VARCHAR(5), GETDATE(), 108) - substring(convert(varchar(20), ColumnName, 9), 13, 5)
but I am getting this error and need some help please:
Operand data type varchar is invalid for subtract operator.
What you want is datepart(mi).
To get the minutes for getdate():
select datepart(mi, getdate())
To subtract a number of minutes from a datetime:
select dateadd(mi, - <minutes>, <datevalue>)
To remove the time from getdate(), just cast to date (in more recent versions of SQL Server):
select cast(getdate() as date)
To get the difference in minutes, use datediff:
select datediff(mi, <datestart>, <dateend>)
What are you really trying to accomplish?
I think this is what you want:
declare #str varchar(30)
set #str = '2012-08-14 10:12:02.690'
select datediff(minute, cast(#str as datetime), getdate())
Results when getdate() = '2012-08-14 11:21:10.250' is total minutes even over 60:
69

Constructing dates in TSQL

Trying to construct a date:
CAST('9/1/' + YEAR(GETDATE()) AS Datetime) AS test2
But it doesnt work?
Would like to get something like '9/1/2010'?
you can't concatenate the string '9/1' with the number: YEAR(GETDATE()), so try this:
select CAST('9/1/' + CONVERT(varchar(4),YEAR(GETDATE())) AS Datetime) AS test2
SELECT
CAST( '9/1/' + CAST( YEAR(GETDATE()) AS VARCHAR ) AS Datetime) AS test2
You need to cast the YEAR (integer) to a VARCHAR before you can append it.
try this:
Select DateAdd(month,
dateDiff(month, 0, getdate()) + 9 - MONTH(getdate()),
0)
You can use a string formatted YYYYMMDD.