select date in form dd/mm/yyyy in sql query - sql-server

in database table I got :
int day
int month
int year
I want to write a select query to show the date : dd/mm/yyyy
I try this but didn't work :
1-
select (day + '\' + month + '\' + year) as xdate from table1
but give me an error
2-
select (day + '-' + month + '-' + year) as xdate from table1
it return the sum of these field : 2036 instead of 21/1/2014
any solution ?

Try something like this:
select (cast(day as varchar(2)) + '/'
+ cast(month as varchar(2)) + '/'
+ cast(year as varchar(4))) as xdate
from table1

If TSQL / SQL-SERVER, then + only works to concatenate strings... So do some conversions on your numbers in that case:
select
CONVERT(VARCHAR, day) + '\' + CONVERT(VARCHAR, month) + '\' ... etc.

Since you are Casting int to Varchar, so you need to use CAST or CONVERT function
CAST and CONVERT
Try like this
select (Cast(day AS Varchar(10)) + '\' + Cast(month AS Varchar(10)) + '\' + Cast(year AS Varchar(10))) as xdate from table1
(Or)
select (Cast(day AS Varchar(10)) + '-' + Cast(month AS Varchar(10)) + '-' + Cast(year AS Varchar(10))) as xdate from table1

CONVERT(date, , 103) converts a date to a string representation in dd/mm/yyyy form
SELECT CONVERT(date,DATEFROMPARTS([year],[month],[day]),103)

Related

Convert varchar string to datetime

I am having an issue converting below to a DateTime, I am only able to get the date portion of the string. My goal is to get both the date & time.
declare #col varchar(14) = '20220602235900';
select CONVERT(datetime, CAST(#col AS varchar(8)), 121) dtimecre
Steps:
Read the documentation for CONVERT and pick the closest format to use
Do some string manipulation to get the desired format
Convert.
DECLARE #col varchar(14) = '20220602235900';
SELECT
CONVERT(date, SUBSTRING(#col,1,8), 121) [Date Component]
, CONVERT(time, SUBSTRING(#col,9,2) + ':' + SUBSTRING(#col,11,2) + ':' + SUBSTRING(#col,13,2), 8) [Time Component]
, CONVERT(datetime, SUBSTRING(#col,1,4) + '-' + SUBSTRING(#col,5,2) + '-' + SUBSTRING(#col,7,2) + ' ' + SUBSTRING(#col,9,2) + ':' + SUBSTRING(#col,11,2) + ':' + SUBSTRING(#col,13,2), 120) [DateTime Representation];
Returns:
Date Component
Time Component
DateTime Representation
2022-06-02
23:59:00.0000000
2022-06-02 23:59:00.000
And another variant with a smaller amount of SUBSTRING:
DECLARE #col varchar(14) = '20220602235900';
SELECT DateComponent = CAST(DatePartString AS DATE),
TimeComponent = CAST(TimePartString AS TIME),
DateAndTime = CAST(DatePartString + ' ' + TimePartString AS DATETIME)
FROM (VALUES(#col)) AS O (DateColumn)
CROSS APPLY (
SELECT DatePartString = LEFT(DateColumn, 8),
TimePartString = FORMAT(CAST(SUBSTRING(DateColumn, 9, 6) AS INT), '##:##:##')
) AS String;

Query is not grouping

Im filling a table and I need this table get the daily sells, but summing and counting, sells and clients by day respectively.
But is not grouping the sales by day this is the QUERY:
insert into VentaDiariaTienda(
Fecha,
VentaDia,
Tda_Codigo,
NumeroClientes,
PromedioVtaCliente,
EditName,
EditDateTime)
select
DATENAME(dw, T_Fecha) + ', ' +
cast(datepart(dd, T_Fecha) as char(2)) + ' ' +
datename(mm, T_Fecha) + ' ' +
cast(datepart(yyyy,T_Fecha) as char(4)),
sum(T_ImporteTotal/1.16),
[FolTda_Codigo],
count(T_Cliente),
sum(T_ImporteTotal/1.16)/count(T_Cliente),
'Admin',
GETDATE()
from #Tickets
group by T_Fecha,[FolTda_Codigo]
and this is the out of this query
Thanks for your replys.
You will have to "round down" the T_Fecha values to a day, so you might try to insert the following records:
SELECT
DATENAME(dw, dt) + ', ' +
DATENAME(dd, dt) + ' ' +
DATENAME(mm, dt) + ' ' +
DATENAME(yy, dt),
SUM(T_ImporteTotal)/1.16,
FolTda_Codigo,
COUNT(T_Cliente),
SUM(T_ImporteTotal)/1.16/COUNT(T_Cliente),
'Admin',
GETDATE()
FROM #Tickets
CROSS APPLY (
VALUES (DATEADD(day, DATEDIFF(day, 0, T_Fecha), 0))
) d (dt)
GROUP BY dt, FolTda_Codigo
My proposition:
You can use format function (since SQL Server 2012, else use Wolfgang Method)
Be carefull with divide by "Count" function. If you result is 0 (by example if T_Client is always null) you will have an error
Not necessary to use "outer apply" instruction in this case, group on by date and not by date and time
Something like this:
SELECT
FORMAT(T_Fecha, 'dddd, dd MMMM yyyy', 'en-US' ),
SUM(T_ImporteTotal)/1.16,
FolTda_Codigo,
COUNT(T_Cliente),
case when COUNT(T_Cliente)=0 then null else SUM(T_ImporteTotal)/1.16/COUNT(T_Cliente) end,
'Admin',
GETDATE()
FROM #Tickets
GROUP BY cast(T_Fecha as date), FolTda_Codigo
For SQL Server 2008, replace format by
DATENAME(dw, T_Fecha) + ', ' + DATENAME(dd, T_Fecha) + ' ' +
DATENAME(mm, T_Fecha) + ' ' + DATENAME(yy, T_Fecha),

how to check the condition whether the date exists or not

select CAST((CAST(2014 AS VARCHAR) + '-' + CAST(9 AS VARCHAR) + '-' + CAST(31 AS VARCHAR)) AS DATE)
Conversion failed when converting date and/or time from character string is coming.
how to check the condition for above problem...
Use ISDATE() function to check whether string is a valid date
Use the query: using ISDate()
IF ISDATE((CAST(2014 AS VARCHAR) + '-' + CAST(9 AS VARCHAR) + '-'
+ CAST(31 AS VARCHAR))) = 1
SELECT CAST(CAST(2014 AS VARCHAR) + '-' + CAST(9 AS VARCHAR) + '-' +
CAST(31 AS VARCHAR) AS DATETIME)
ELSE
SELECT NULL
Use DateTime.. Also for 9th month no 31st
IF ISDATE(CAST((CAST(2014 AS VARCHAR) + '-' + CAST(9 AS VARCHAR) + '-' + CAST(30 AS VARCHAR)) AS DATETIME)) = 1
PRINT 'VALID'
ELSE
PRINT 'INVALID';

sql server year

DECLARE #FINANCIALYEAR AS varchar(30)
DECLARE #FINALFINANCIALYEAR AS int
SELECT #FINANCIALYEAR=CONVERT(VARCHAR,YEAR(GETDATE())-2) + ', ' +
CONVERT(VARCHAR,YEAR(GETDATE())-1) + ', ' +
CONVERT(VARCHAR,YEAR(GETDATE()))
set #FINALFINANCIALYEAR = CONVERT(int,#FINANCIALYEAR)
print #FINALFINANCIALYEAR
i want final output in int format so iam doing above code but it gives me error plz help
Simple query
SELECT CONVERT(VARCHAR,YEAR(GETDATE())-2) + ', ' +
CONVERT(VARCHAR,YEAR(GETDATE())-1) + ', ' +
CONVERT(VARCHAR,YEAR(GETDATE()))
Select YEAR (GetDate())-2,YEAR (GetDate())-1, YEAR (GetDate())
Just check what your variable is after your SELECT:
DECLARE #FINANCIALYEAR AS varchar(30)
DECLARE #FINALFINANCIALYEAR AS int
SELECT #FINANCIALYEAR=CONVERT(VARCHAR,YEAR(GETDATE())-2) + ', ' +
CONVERT(VARCHAR,YEAR(GETDATE())-1) + ', ' +
CONVERT(VARCHAR,YEAR(GETDATE()))
SELECT #FINANCIALYEAR
The output is:
2008, 2009, 2010
This is clearly NOT a valid INT value - so it's obvious you're getting a conversion error.....
SELECT DATEPART(YEAR, GETDATE()), DATEPART(YEAR, GETDATE()) - 1, DATEPART(YEAR, GETDATE()) - 2
this year is
Select datepart(year,getdate())
or
year(getdate())

Converting DateTime to nvarchar, just month and year

How to convert the datetime value to nvarchar and want to format it "Month, Year" e-g October 1st 2009 value should get converted to "October, 2009"
use this:
select CONVERT(nvarchar(50), DATENAME(m, getdate())
+ ', '
+ DATENAME(yyyy, getdate())
)
OUTPUT:
--------------------------------------------------
October, 2009
(1 row(s) affected)
Try this
DECLARE #DateTime DATETIME
SET #DateTime = '01 Oct 2009'
SELECT #DateTime
SELECT DATENAME(mm, #DateTime) + ', ' + CAST(DATEPART(yy, #DateTime) AS VARCHAR(4))
One way would be to use datename to extract the pieces you needed in name format, so:
select Convert(nvarchar,datename(m,getdate())) + N', ' + Convert(nvarchar,datename
(yy,getdate()))
And replace getdate() with your date variable / field.
The DateName function will provide the formatting you require:
DATENAME(m, date) + ', ' + DATENAME(yyyy, date)
Converting to nvarchar of a specific size can be done through the cast function:
CAST(value AS nvarchar[30])
Try the following query:
Select case Convert(int, day(getdate())) when 1 then '1st' when 2 then '2nd'
when 3 then '3rd' else Convert(varchar, day(getdate()))+'th' end +' '+ Convert(varchar, Datename(m,getdate()))+' ' +Convert(varchar, Datename(yy,getdate())) as Date
You can replace getdate() with any other date.
Please check if it helps.

Resources