I am trying to make a master calendar in SQL Server. I am facing the problem with this syntax:
[dateid] as DATEPART(YEAR, [date]) & DATEPART(day, format( [date],'DD')) & DATEPART(month, format([date], 'MM'))
[dateid] as convert(varchar(4), DATEPART(YEAR, [date])) + convert(varchar(2), DATEPART(day, format( [date], 'DD'))) + convert(varchar(2), DATEPART(month, format([date], 'MM')))
[dateid] as cast(DATEPART(YEAR, [date]) as varchar(4)) + cast(DATEPART(day, format( [date], 'DD')) as varchar(2)) + cast(DATEPART(month, format([date], 'MM')) as varchar(2))
These statements are under create table tablename (...).
I have tried above syntax and It is showing:
Conversion failed when converting date and/or time from character string
or
The data types varchar and varchar are incompatible in the '&' operator.
Can someone help me with this, I want to show date like (YearDayMonth)-20160111 - how to concatenate these three ?
Why can't you just use
SELECT FORMAT(SYSDATETIME(), 'yyyyddMM')
or in your CREATE TABLE statement:
CREATE TABLE dbo.YourTable
(
.....
SomeDate DATE,
DateId AS FORMAT(SomeDate, 'yyyyddMM'),
......
)
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))
I am trying to produce a varchar(20) which is 4 character year + 2 character month. I would like to then use this as a parameter in my stored procedure. Im unable to concatenate 2 varchars together to produce this, any ideas?
SELECT
GETDATE() AS CurrentDateTime,
CONVERT(VARCHAR(20), YEAR(GETDATE())) AS CurrentYear,
CONVERT(VARCHAR(20), MONTH(GETDATE())) AS CurrentMonth,
CurrentYear + CurrentMonth AS YearMonth
The simplest approach would be to use this:
SELECT Format(GetDate(), 'yyyyMM') as YearMonth
The function Format() was introduced with Sql Server 2012.
why not simply
SELECT GETDATE() AS CurrentDateTime
, Convert(VARCHAR(20),YEAR(GETDATE())) AS CurrentYear
, Convert(VARCHAR(20),MONTH(GETDATE())) AS CurrentMonth
, Convert(varchar(6), GETDATE(), 112) AS YearMonth
SELECT GETDATE() AS CurrentDateTime
, Convert(VARCHAR(20),YEAR(GETDATE())) +
Convert(VARCHAR(20),MONTH(GETDATE()))
Column aliases can't be selected from, so the above would do.
Like this one?
SELECT GETDATE() AS CurrentDateTime
, Convert(VARCHAR(20),YEAR(GETDATE())) AS CurrentYear
, Convert(VARCHAR(20),MONTH(GETDATE())) AS CurrentMonth
, Convert(VARCHAR(20),YEAR(GETDATE())) + Convert(VARCHAR(20),MONTH(GETDATE())) AS YearMonth
Also you can
Year(GetDate()) * 100 + Month(GateDate()) AS YearMonth
or use construction which offered by users below
Convert(varchar(6), GETDATE(), 112) AS YearMonth
I want to select getdate() in the format yyyy/M. I tried to write a query
SELECT FORMAT(GETDATE(), 'yyyy/M')
but it is throwing an error.
I am a beginner in SQL. How do I get the yyyy/m format if there is only single digit month? E.g. the query should return 2016/1 when there is only one digit month (it should not return 2016/01) and should return 2016/10 when the month has two digits
How about getting the YEAR and MONTH part of the date and just concatenate them:
SELECT
CAST(DATEPART(YEAR, GETDATE()) AS VARCHAR(4)) + '/' +
CAST(DATEPART(MONTH, GETDATE()) AS VARCHAR(2))
Try this:
SELECT FORMAT(GETDATE(),'yyyy/MM')
SELECT FORMAT(CAST('2015-11-15' AS smalldatetime),'yyyy/M'),
FORMAT(CAST('2015-01-15' AS smalldatetime),'yyyy/M')
Gives:
2015/11 | 2015/1
SELECT CONVERT(VARCHAR(7), GETDATE(), 111) AS [YYYY/MM]
OR
SELECT CONVERT(VARCHAR(7), GETDATE(), 111)
I have a view in my SQL Server database named PictureCount. I am getting data according the following query-
with result as
(SELECT CAST(Createddate AS DATE) AS StartDate, COUNT(ServicePictureID) AS TotalPicture
FROM PictureCount
WHERE ProjectID='11' AND
CAST(CreatedDate As DATE) BETWEEN '2014-10-31' AND '2014-12-05'
GROUP BY CAST(Createddate AS DATE)
)
SELECT
(CAST(DATEPART(YYYY, CAST(StartDate AS DATE)) as varchar) + '-' + CAST(DATEPART(MONTH ,CAST(StartDate AS DATE)) AS varchar)) AS StartDate,
TotalPicture
FROM result
Data is appearing in the following format-
But I want data with group by StartDate. I am not able to apply GROUP BY clause. Please tell me if anyone knows about it.
Thanks in advance.
Try this query :
SELECT Cast(Datepart(YEAR, StartDate) AS VARCHAR(4))
+ '-'
+ Cast(Datepart(MONTH, StartDate) AS VARCHAR(2)) AS StartDate,
Count(ServicePictureID) AS TotalPicture
FROM PictureCount
WHERE ProjectID = '11'
AND CreatedDate BETWEEN '2014-10-31' AND '2014-12-06'
GROUP BY Datepart(YEAR, StartDate),Datepart(MONTH, StartDate)
How about this, if you can show data in yyyyMM format rather than yyyy-MM. This query has less no of functions, conversions.
SELECT Datepart(YEAR, StartDate)*100 + Datepart(MONTH, StartDate) as yyyyMM
, Count(ServicePictureID) AS TotalPicture
FROM PictureCount
WHERE ProjectID = '11'
AND CreatedDate BETWEEN '2014-10-31' AND '2014-12-05'
GROUP BY Datepart(YEAR, StartDate)*100 + Datepart(MONTH, StartDate)
I am working with a query where I fetch date with a sql query. And wondering how to use the CONVERT function to not show todays date, but convert the date saved in the database which is named as: routines.date. The goal is to sort it as D-M-Y and now its saved as Y-M-D
This didn't work:
CONVERT(date,routines.date,105) as Date
Try this
SELECT CONVERT(VARCHAR(10), CAST(routines.date AS DATETIME), 105)
OR
SELECT CONVERT(varchar, DATEPART(yyyy, #routines.date)) + '-' + CONVERT(varchar, DATEPART(mm, #routines.date)) + '-' + CONVERT(varchar, DATEPART(dd, #routines.date))