I would like to ask how to get MIN DatePart from a DateTime column in a SQL Server Select query.
This is my query:
SELECT
MIN(CAST(DATEPART(DD, DateCreated) AS NVARCHAR) +
N'/' +
CAST(DATEPART(MM, DateCreated) AS NVARCHAR) +
N'/' +
CAST(DATEPART(YYYY, DateCreated) AS NVARCHAR)) AS DateFrom
FROM
[User]
The result should be 04/04.2013 but right now, the result is 01/04/2014
Please help ....
SELECT MIN(CAST(DATEPART(DD,DateCreated) AS NVARCHAR) +
N'/' +
CAST(DATEPART(MM,DateCreated) AS NVARCHAR) +
N'.' +
CAST(DATEPART(YYYY,DateCreated) AS NVARCHAR)) AS DateFrom
FROM [User]
use the aggregation query like below one
select min(date) from tablename
Related
I have an existing SQL Server table Employees with 3 columns month, year and day as date parts.
Now I have added a new column createdate where I want to insert date as a combination of all 3 columns from the current row.
UPDATE Employees
SET createdate = TRY_PARSE(CAST([year] AS VARCHAR(4)) + '/' + CAST([month] AS VARCHAR(2)) + '/' + CAST([day] AS VARCHAR(2)) AS DATETIME)
If you have invalid data in Month Or Year Or Day Colunm, then above query will update with NULL Value.
NOTE: Try_Parse will work from Sql Server Version 2012 onwards.
You can as the below:
UPDATE Employees
SET createdate = CAST(CAST(year AS VARCHAR(4)) + '.' + CAST(month AS VARCHAR(2)) + '.' + CAST(day AS VARCHAR(2)) AS DATETIME)
You can use DATEFROMPARTS very simple to form date
UPDATE Employees SET CreateDate =DATEFROMPARTS ( year, month, day )
Try this, it's update for all rows :
UPDATE Employees SET CreateDate = [day] +'/'+ [Month] +'/'+ [year]
1)...UPDATE Employees
set createdate =
CAST(
CAST(year AS VARCHAR(4)) +
RIGHT('0' + CAST(month AS VARCHAR(2)), 2) +
RIGHT('0' + CAST(day AS VARCHAR(2)), 2)
AS DATETIME)
2)...SELECT
DATEADD(year, [year]-1900, DATEADD(month, [month]-1, DATEADD(day, [day]-1, 0)))
FROM
dbo.Table
UPDATE emp SET CreateDate =( SELECT CONVERT(DATE,CAST([Year] AS VARCHAR(4))+'-'+
CAST([Month] AS VARCHAR(2))+'-'+
CAST([Day] AS VARCHAR(2))))
or
you can also use this code:
UPDATE empl SET CreateDate =( SELECT CONVERT(varchar(10),
CAST([Month] AS VARCHAR(2))+'-'+
CAST([Day] AS VARCHAR(2))+'-'+CAST([Year] AS VARCHAR(4)),101))
Our ERP system holds year, month and day in separate int columns. I want to combine those 3 int columns into one DateTime, How can I convert it in the SQL Server.
Year | Month | Day
--------------------
2016 | 1 | 23
You could use DATEFROMPARTS(SQL Server 2012+):
SELECT DATEFROMPARTS([Year], [Month], [Day]) AS DateTim
FROM table_name
SQL Server 2008 (assuming that year is 4-digit):
SELECT CAST(CAST([year] AS VARCHAR(4)) + '-' +
CAST([month] AS VARCHAR(2)) + '-' +
CAST([day] AS VARCHAR(2))
AS DATE) AS DateTim
FROM table_name;
LiveDemo
As Gordon Linoff proposed in comment to utilize YYYYMMDD as INT to convert to DATE:
SELECT CAST(CAST([year] * 10000 + [month]*100 + [day] AS VARCHAR(8))
AS DATE) AS DateTim
FROM table_name;
LiveDemo2
Addendum
To address ypercubeᵀᴹ concerns about dateformat we could utilize ISO-8601 which is not affected by DATEFORMAT or LANGUAGE settings:
yyyy-mm-ddThh:mm:ss[.mmm]
SELECT CAST(RIGHT('000'+ CAST([year] AS VARCHAR(4)),4) + '-' +
RIGHT('0' + CAST([month] AS VARCHAR(2)),2) + '-' +
RIGHT('0' + CAST([day] AS VARCHAR(2)),2) + 'T00:00:00' AS DATE)
FROM table_name
LiveDemo3
To handle years before 1000 should be padded with zeros.
We can use SQL's CONVERT function to get Datetime value :
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(2), Day) + '/' + CONVERT(VARCHAR(2), Month) + '/' + CONVERT(VARCHAR(4), Year),103) FROM Table_Name
I want to concatenate two TIME columns and show as one column.
Example:
FromTime: 9:00
ToTime: 12:00
Result should be:
9:00-12:00
Generic SQL:
-- hh:mm:ss
SELECT 'result:' + CONVERT(CHAR(6), FromTime, 8) + '-' + CONVERT(CHAR(6), ToTime)
FROM yourTable
MySQL:
-- hh:mm
SELECT 'result:' + DATE_FORMAT(FromTime, '%H:%i') + '-' + DATE_FORMAT(ToTime, '%H:%i')
FROM yourTable
SQL Server:
-- hh:mm
SELECT 'result:' + convert(char(2), DATEPART(hh, FromTime)) + ':' +
CONVERT(CHAR(2), DATEPART(mm, FromTime)) + '-' +
CONVERT(CHAR(2), DATEPART(hh, ToTime)) + ':' +
CONVERT(CHAR(2), DATEPART(mm, ToTime))
FROM yourTable
declare #FromTime time
declare #ToTime time
set #FromTime='9:00'
set #ToTime='12:00'
select cast(#FromTime as varchar(10))+ '-' + cast(#ToTime as varchar(10)) as result
sql demo
You can use convert
select convert(VARCHAR(5),getdate(),108) + ' - ' + convert(VARCHAR(5),getdate()-1,108)
This statement will correctly merge 2 columns ('DATE' and 'TIME'):
update AllBW1 set sUserTime =
CAST(
(
STR( YEAR( [DATE] ) ) + '/' +
STR( MONTH( [DATE] ) ) + '/' +
STR( DAY( [DATE] ) ) + ' ' +
(select DATENAME(hour, [TIME]))+ ':' +
(select DATENAME(minute, [TIME])) + ':' +
(select DATENAME(SECOND, [TIME]))
) as DATETIME)
where sUserTime is null
I'd like to refine the above so as to replace the default timezone with my own (GMT-6).
I've tried a few variations:
CAST((select DATEADD(hour, -6, DATENAME(hour, [TIME]))) as smalldatetime) + ':' +
and
(select CAST(DATEADD(hour, -6, DATENAME(hour, [TIME]))) as datetime) + ':' +
and have achieved no joy.
thx
The logfile as parsed into the SQL table by LogParser 2.2 has separate fields for Date and Time but, since both are formatted as datatime fields they end up looking like:
2012-01-04 00:00:00.000 for date (all time fields are zeroed)
2012-01-01 06:04:41.000 for time (all date field = first day of current year)
That's the reason the query is parsing each element the way it is. Thanks to Dems comment I simplified everything down. I've no doubt this can be optimized by for the volumes I'm dealing with this is adaquate:
update myTable set sUserTime =
(
DATENAME(YEAR, [DATE] ) + '/' +
DATENAME(MONTH, [DATE] ) + '/' +
DATENAME(DAY, [DATE] ) + ' ' +
DATENAME(hour, (dateadd(hh, -6, [time])))+ ':' +
DATENAME(minute, [TIME]) + ':' +
DATENAME(SECOND, [TIME])
)
where sUserTime is null
I have table Proccesses that consist of Date Time Field.
Proccesses ( Id as int , RunDate as DateTime)
I need to run Distinct by RunDate as Time Without seconds
For Example
ID RunDate
1 2011-12-13 12:36:26.483
2 2011-12-12 12:37:22.421
3 2011-12-11 12:36:44.421
I need to receive in output
Output
12:36
12:37
I tried to use DatePart but did not manage
Please help
This should work:
SELECT DISTINCT DATENAME(hour, RunDate) + ':' + DATENAME(minute, RunDate) AS Output...
For SQL Server
select CAST(DATEPART(hour, RunDate) as varchar) + ':' + CAST(DATEPART(minute, RunDate) as varchar)
Try
for hours: DATEPART(hh, RunDate)
for minutes: DATEPART(mi, RunDate)
select CONVERT(varchar,RunDate,103) + ' ' + CONVERT(varchar(5),RunDate,108)
(in Sql Server)