I am trying to execute the code but it throws me an error. I need the days to be between 7 and 10
I have already tried running it but it is not working so far
select * from hive.entity.bookings_base_fact where car_number in('S424BFK',
'S275BCK',
'S257CAN',
'BANDYS',
'722YKE',
'427YNZ',
'042XUK',
'161YKD',
'029YHY',
'894VIF',
'856XHF',
'ALG16',
'364TGB',
'452YTB',
'171WSN',
'148YIC',
'599XPY',
'ZAG386',
'1LX5FU',
'1PC7PW',
'VISNOI',
'1BO1RQ',
'ZCI076',
'1LX6DH',
'ZSF986',
'1EIR205',
'1GKX072',
'RAKHRA',
'1GRK465',
'1EQO445',
'1EKU643',
'1DXD359',
'1EXA622',
'1DSJ769',
'SINGH001',
'A77027',
'1GOV986',
'1GBY431',
'1EDN031',
'1DUH537',
'1DIY218',
'WAN813',
'1DYR270',
'1ERI764',
'1DVS953',
'1GTW217',
'1GFD951',
'1ETD570',
'1GJG518',
'1GJI460',
'MIGLANI',
'1GHI708',
'1GPQ808',
'1GPT618',
'1EGF910',
'1ELU000',
'1GPP664',
'1GSF834',
'WA23600',
'1EPO234',
'1DYQ191',
'1EFG503',
'1EGK697',
'1EWY165',
'1EIX170',
'1GDP522',
'1GTP444',
'1DGF516',
'1DPC526',
'1EQM261',
'1EVC862',
'1GBX209',
'1GKZ876',
'1GNG683',
'1EEZ674',
'1GPO491',
'1EVB911',
'1GUP612',
'BV31RJ',
'CRQ71X',
'CT99PU',
'EAW37E',
'CYN07E',
'BWN62F',
'DNW45H',
'DFS57K',
'DVS85L',
'CL43PE',
'BNO46F',
'CZL21R',
'BH60VR',
'CM16EE',
'YJB08D',
'CO10RJ',
'DYW67H',
'DXL39P',
'CA10BZ',
'DWE07J',
'BV73JU',
'DPK94X',
'CJJ39S',
'CJH53E',
'CO33KZ',
'CU14BL',
'DZK23F',
'CD68JB',
'CVA62V',
'DKR25C',
'CP61CE',
'CQ55MZ',
'YCP92Y',
'DVQ94B',
'BWD26H',
'DWG35Q',
'YIV33C',
'DSN50W',
'CQ92MT',
'DXV29V',
'CHT08L',
'EAV95U',
'BN99CL',
'PTI786',
'CQ74YA',
'CK62MQ',
'CA70CH',
'CQ78UB',
'CN46RX',
'CL83WY',
'CM05DI',
'BU30HV',
'CGT93H',
'CM06TN',
'DVZ41R',
'CS35QM',
'CT03AY',
'YCP71P',
'BPI07J',
'CL43SH',
'DXV62T',
'CA99ME',
'YIU86L',
'YDE45J',
'CO96PR',
'EBN83B',
'BX19FX',
'YJK59F',
'DZB28E',
'BW50MZ',
'CK80MR',
) and year=2019 and month=06 and day between 7 and 10
I suggest you to store dates in a date column with a DATE format, so you can use native functions and do your query like this :
WHERE YEAR(date) = 2019
AND MONTH(date) = 6
AND DAY(date) BETWEEN 7 AND 10
But in your case, I think your columns use strings, you can do this :
WHERE CAST(year AS INT) = 2019
AND CAST(month AS INT) = 6
AND CAST(day AS INT) BETWEEN 7 AND 10
Try to convert columns to datatype integer in the following:
and cast(year as int) = 2019
and cast(month as int) = 6
and cast(day as int) between 7 and 10
The use of the leading zeros in the integers suggests that the columns are actually strings. If this is the case, then the comparisons should be as strings:
year = '2019' and month = '06' and
day between '07' and '10'
Related
I have an issue where I am trying to add a leading 0 to run an output.
SELECT
CASE
WHEN LEN(t.trans_time) = 5
THEN CONCAT(0, [trans_time])
ELSE T.[trans_time]
END AS [TransactionTime]
,RIGHT(CONCAT(0,trans_time),6) AS trans_time
,LEN(T.trans_Time)
,t.trans_time
Why does the case statement not return the leading 0 whereas using:
,RIGHT(CONCAT(0,trans_time),6) AS trans_time
Works no problem.
Case expression return only one type, whereas concat() would return different type & i am assuming trans_time has INT type.
So, you would need to do type conversations :
SELECT (CASE WHEN LEN(t.trans_time) = 5
THEN CONCAT(0, [trans_time])
ELSE CAST(T.[trans_time] AS VARCHAR(255))
END) AS [TransactionTime],
. . .
Another way to do this is to use the format function, wich is available from sql server 2012.
It not only makes the code more readable but will also perform better.
declare #t table (id int)
insert into #t values (90113), (90204), (90207), (90235), (90302), (90318), (90324)
select format(id, '000000') as TransactionTime from #t
this will return
TransactionTime
---------------
090113
090204
090207
090235
090302
090318
090324
First, I have read about similar posts and have read the comments that this isn't an ideal solution and I get it but the boss (ie client) wants it this way. The parameters are as follows (for various reasons too bizarre to go into but trust me):
1. SQL Server Mgmt Studio 2016
2. NO parameters or pass throughs or temp tables. All has to be within contained code.
So here we go:
I need to create column headings that reflect dates:
1. Current date
2. Most recent quarter end prior to current date
3. Most recent quarter end prior to #2
4. Most recent quarter end prior to #3
5. Most recent quarter end prior to #4
6. Most recent quarter end prior to #5
So if using today's date, my column names would be as follows
12/18/2016 9/30/2016 6/30/2016 3/31/2016 12/31/2016 9/30/2015
I can easily do it in SAS but can't in SQL given the requirements stated above.
Help please with same code.
Thank you
Paula
Seems like a long way to go for something which really belongs in the presentation layer. That said, consider the following:
Let's assume you maintain a naming convention for your calculated fields, for example [CurrentDay], [QtrMinus1], [QtrMinus2], [QtrMinus3], [QtrMinus4],[QtrMinus5]. Then we can wrap your complicated query in some dynamic SQL.
Just as an illustration, let's assume your current query results looks like this
After the "wrap", the results will then look like so:
The code - Since you did NOT exclude Dynamic SQL.
Declare #S varchar(max)='
Select [CustName]
,['+convert(varchar(10),GetDate(),101)+'] = [CurrentDay]
,['+Convert(varchar(10),EOMonth(DateFromParts(Year(DateAdd(QQ,-1,GetDate())),DatePart(QQ,DateAdd(QQ,-1,GetDate()))*3,1)),101)+'] = [QtrMinus1]
,['+Convert(varchar(10),EOMonth(DateFromParts(Year(DateAdd(QQ,-2,GetDate())),DatePart(QQ,DateAdd(QQ,-2,GetDate()))*3,1)),101)+'] = [QtrMinus2]
,['+Convert(varchar(10),EOMonth(DateFromParts(Year(DateAdd(QQ,-3,GetDate())),DatePart(QQ,DateAdd(QQ,-3,GetDate()))*3,1)),101)+'] = [QtrMinus3]
,['+Convert(varchar(10),EOMonth(DateFromParts(Year(DateAdd(QQ,-4,GetDate())),DatePart(QQ,DateAdd(QQ,-4,GetDate()))*3,1)),101)+'] = [QtrMinus4]
,['+Convert(varchar(10),EOMonth(DateFromParts(Year(DateAdd(QQ,-5,GetDate())),DatePart(QQ,DateAdd(QQ,-5,GetDate()))*3,1)),101)+'] = [QtrMinus5]
From (
-- Your Complicated Query --
Select * from YourTable
) A
'
Exec(#S)
If it helps the visualization, the generated SQL is as follows:
Select [CustName]
,[12/18/2016] = [CurrentDay]
,[09/30/2016] = [QtrMinus1]
,[06/30/2016] = [QtrMinus2]
,[03/31/2016] = [QtrMinus3]
,[12/31/2015] = [QtrMinus4]
,[09/30/2015] = [QtrMinus5]
From (
-- Your Complicated Query --
Select * from YourTable
) A
Here is one way using dynamic query
DECLARE #prior_quarters INT = 4,
#int INT =1,
#col_list VARCHAR(max)=Quotename(CONVERT(VARCHAR(20), Getdate(), 101))
WHILE #int <= #prior_quarters
BEGIN
SELECT #col_list += Concat(',', Quotename(CONVERT(VARCHAR(20), Eomonth(Getdate(), ( ( ( ( Month(Getdate()) - 1 ) % 3 ) + 1 ) * -1 ) * #int), 101)))
SET #int+=1
END
--SELECT #col_list -- for debugging
EXEC ('select '+#col_list+' from yourtable')
I am running this in sql server but there is an error on the date compare as the column cadodt is a numeric. Is there a quick and easy way to convert that col to numeric? It's date is in YYYYMMDD but I cannot compare against a date field like current date.
select * from openquery(IBSIBM, 'select CABANO, CABCRC, CABCTR, CABCUR, CABKCO, CACLVL, CACOTI,
CACRDT, CACTTP, CADODT, CADOTY, CAEXRT, CAIDNO, CAJONO, CANCRX,
CAPERI, CAREFX, CARSCD, CASCRC, CASCTR, CASTAT, CASTMT, CASYID,
CATIML, CATREF, CATTXT, CATYPC, CAUSER, CAVADT, CAVODT, CAVONO,
CAVOTY ,NANAME from CA1665AFCV.SROCBA,
CA1665AFCV.srolta,
CA1665AFCV.sronam
WHERE CADOTY = ''CHK'' and cajono=ctjono and cadodt = CURRENT DATE()
and caperi=ctperi and caidno=ctidno and ctveno=nanum
and CASTAT = ''Y''')
Assuming cadodt is actually an integer or numeric field and not a character string, try this:
select * from openquery(IBSIBM,
'select CABANO, CABCRC, CABCTR, CABCUR, CABKCO, CACLVL, CACOTI,
CACRDT, CACTTP, CADODT, CADOTY, CAEXRT, CAIDNO, CAJONO, CANCRX,
CAPERI, CAREFX, CARSCD, CASCRC, CASCTR, CASTAT, CASTMT, CASYID,
CATIML, CATREF, CATTXT, CATYPC, CAUSER, CAVADT, CAVODT, CAVONO,
CAVOTY, NANAME
from CA1665AFCV.SROCBA, CA1665AFCV.srolta, CA1665AFCV.sronam
WHERE CADOTY = ''CHK''
and cajono = ctjono
and cadodt = YEAR(CURRENT DATE) * 10000 + MONTH(CURRENT DATE) * 100 + DAY(CURRENT DATE)
and caperi = ctperi
and caidno = ctidno
and ctveno = nanum
and CASTAT = ''Y''')
Aside: I would strongly recommend against using comma joins. The longer ANSI 92 join syntax is much easier to read and maintain. I'd also strongly recommend using qualified column names. This schema is not the easiest to follow as it is. No need to make it even more arcane.
You have these options
convert cadodt from string to date
convert cadodt from intger to date
convert current date to YYYYMMDD string
convert current date to YYYYMMDD integer
1 and 3 assume cadodt is a string
2 and 4 assume cadodt is an intger
3 and 4 are the more efficient approaches as you only change one value.
e.g.
(3) if cadodt is a string in YYYMMDD format:
AND cadodt = VARCHAR_FORMAT(CURRENT DATE, 'YYYYMMDD')
(4) if cadodt is an integer representing YYYYMMDD
AND cadodt = YEAR(CURRENT DATE) * 10000 + MONTH(CURRENT DATE) * 100 + DAY(CURRENT DATE)
the CURRENT DATE() may be causing you some issues use CURRENT_DATE()
Try this:
select * from openquery(IBSIBM, 'select CABANO, CABCRC, CABCTR, CABCUR, CABKCO, CACLVL, CACOTI,
CACRDT, CACTTP, CADODT, CADOTY, CAEXRT, CAIDNO, CAJONO, CANCRX,
CAPERI, CAREFX, CARSCD, CASCRC, CASCTR, CASTAT, CASTMT, CASYID,
CATIML, CATREF, CATTXT, CATYPC, CAUSER, CAVADT, CAVODT, CAVONO,
CAVOTY ,NANAME from CA1665AFCV.SROCBA,
CA1665AFCV.srolta,
CA1665AFCV.sronam
WHERE CADOTY = ''CHK'' and cajono=ctjono and cadodt = CURRENT_DATE()
and caperi=ctperi and caidno=ctidno and ctveno=nanum
and CASTAT = ''Y''')
Replace CADODT = CURRENT DATE() with:
date(substr(char(CADODT), 1, 4) || '-'|| substr(char(CADODT),5, 2) || '-'|| substr(char(CADODT), 7, 2)) = CURRENT DATE
That should convert it from an integer to a date.
I have table following this format :
Id BillNo: Voucher No:
1 W2015-16/0001 W2015-16/0001
2 W2015-16/0002 W2015-16/0002
3 W2015-16/0003 W2015-16/0003
4 W2015-16/0004 W2015-16/0004
5 W2015-16/0005 W2015-16/0005
6 W2015-16/0006 W2015-16/0006
7 W2015-16/0007 W2015-16/0007
8 W2015-16/0008 W2015-16/0008
9 W2015-16/0009 W2015-16/0009
10 W2015-16/0010 W2015-16/0010
But Instead of this Format:
Now I want to Update All "Voucher No" and "Bill No" below on this format :
Id BillNo: Voucher No:
1 W0001/2015-16 W0001/2015-16
2 W0002/2015-16 W0002/2015-16
3 W0003/2015-16 W0003/2015-16
4 W0004/2015-16 W0004/2015-16
5 W0005/2015-16 W0005/2015-16
6 W0006/2015-16 W0006/2015-16
7 W0007/2015-16 W0007/2015-16
8 W0008/2015-16 W0008/2015-16
9 W0009/2015-16 W0009/2015-16
10 W0010/2015-16 W0010/2015-16
11 W0011/2015-16 W0011/2015-16
like this i have a 1000+ records i should update , But i don't know is this possible to do in Lesser time, Kindly give your suggestion , i am new to SQL
Thanks Advance
Assuming everything is in a fixed format, then you would just use update and string manipulations:
update table t
set billno = left(billno, 1) + right(billno, 4) + '/' + substring(billno, 2, 7),
VoucherNo = left(VoucherNo, 1) + right(VoucherNo, 4) + '/' + substring(VoucherNo, 2, 7);
UPDATE YourTable
SET BillNo = SUBSTRING(BillNo,1,1) + SUBSTRING(BillNo,10,4) + '/' + SUBSTRING(BillNo,2,7)
, VoucherNo = SUBSTRING(VoucherNo,1,1) + SUBSTRING(VoucherNo,10,4) + '/' + SUBSTRING(VoucherNo,2,7)
Test:
DECLARE #a NVARCHAR(max) = 'W2015-16/0001'
SELECT SUBSTRING(#a,1,1) +
SUBSTRING(#a,10,4) + '/' + SUBSTRING(#a,2,7)
There is no straight forward way to achieve this. You may need to define some patterns in SQL to replace and play with all string functions. Here is
declare #name varchar(50) ='W2015-16/0001';
select SUBSTRING(STUFF(#name,2,0,(SUBSTRING(#name,CHARINDEX('/',#name) + 1,len(#name)) + '/')),0,14)
--W0001/2015-16
This gives you the result that you are looking for. So you need to set this into your update statement by joining with the table by ID. This would work only if the pattern you have given is correct.
There is a table TABLE which stores values for various intervals of time in a given day. The day can be divided in to 24 intervals of an hour each or 96 intervals of 15 mins duration each. TABLE contains the following columns
DATE
INTERVALCOUNT
INT001
INT002
.
.
INT096
I need to display the values in the table in the following format where based on the INTERVALCOUNT value the time is calculated and its associated value is displayed
SAMPLE OUTPUT
DATE TIME INTERVALCOUNT VALUE
2013-04-11 00:00:00.0000000 96 23.43
2013-04-11 00:15:00.0000000 96 26.91
2013-04-11 00:30:00.0000000 96 28.1999999999
2013-04-11 00:45:00.0000000 96 28.77
2013-04-23 00:00:00.0000000 24 18.3099999999
2013-04-23 01:00:00.0000000 24 20.94
CODE
SELECT [DATE],
(CASE
WHEN [INTERVALCOUNT]=96 THEN CAST(DATEADD(MINUTE,CAST(SUBSTRING([INTERVAL],4,3) AS INT)*15, [DATE]) AS Time)
WHEN [INTERVALCOUNT]=24 THEN CAST(DATEADD(HOUR,CAST(SUBSTRING([INTERVAL],4,3) AS INT), [DATE]) AS Time)
END) AS [TIME],
[INTERVALCOUNT], --24/96
[VALUE]
FROM ( SELECT [DATE],
[INTERVALCOUNT], --24/96
[INT001], [INT002], [INT003], [INT004], [INT005], [INT006], [INT007], [INT008],
[INT009], [INT010], [INT011], [INT012], [INT013], [INT014], [INT015], [INT016],
[INT017], [INT018], [INT019], [INT020], [INT021], [INT022], [INT023], [INT024],
[INT025], [INT026], [INT027], [INT028], [INT029], [INT030], [INT031], [INT032],
[INT033], [INT034], [INT035], [INT036], [INT037], [INT038], [INT039], [INT040],
[INT041], [INT042], [INT043], [INT044], [INT045], [INT046], [INT047], [INT048],
[INT049], [INT050], [INT051], [INT052], [INT053], [INT054], [INT055], [INT056],
[INT057], [INT058], [INT059], [INT060], [INT061], [INT062], [INT063], [INT064],
[INT065], [INT066], [INT067], [INT068], [INT069], [INT070], [INT071], [INT072],
[INT073], [INT074], [INT075], [INT076], [INT077], [INT078], [INT079], [INT080],
[INT081], [INT082], [INT083], [INT084], [INT085], [INT086], [INT087], [INT088],
[INT089], [INT090], [INT091], [INT092], [INT093], [INT094], [INT095], [INT096]
FROM [TABLE] ) [Source]
UNPIVOT ([VALUE] FOR [INTERVAL] IN
([INT001], [INT002], [INT003], [INT004], [INT005], [INT006], [INT007], [INT008],
[INT009], [INT010], [INT011], [INT012], [INT013], [INT014], [INT015], [INT016],
[INT017], [INT018], [INT019], [INT020], [INT021], [INT022], [INT023], [INT024],
[INT025], [INT026], [INT027], [INT028], [INT029], [INT030], [INT031], [INT032],
[INT033], [INT034], [INT035], [INT036], [INT037], [INT038], [INT039], [INT040],
[INT041], [INT042], [INT043], [INT044], [INT045], [INT046], [INT047], [INT048],
[INT049], [INT050], [INT051], [INT052], [INT053], [INT054], [INT055], [INT056],
[INT057], [INT058], [INT059], [INT060], [INT061], [INT062], [INT063], [INT064],
[INT065], [INT066], [INT067], [INT068], [INT069], [INT070], [INT071], [INT072],
[INT073], [INT074], [INT075], [INT076], [INT077], [INT078], [INT079], [INT080],
[INT081], [INT082], [INT083], [INT084], [INT085], [INT086], [INT087], [INT088],
[INT089], [INT090], [INT091], [INT092], [INT093], [INT094], [INT095], [INT096]) ) [Unpivot]
I have achieved it by using UNPIVOT and for displaying the time is use CAST along with SUBSTRING. Is there a better way of doing this? especially the part where I convert the intervals to time.
EDIT The table design can't be changed
Is there a better way of doing this?
Depends on what you mean by better. For instance, the following expression for TIME calculation is "better" in terms of the amount of code:
DATEADD(
MINUTE,
CAST(SUBSTRING([INTERVAL],4,3) AS INT) * 1440 / INTERVALCOUNT,
CAST('00:00' AS time)
) AS [TIME]
You might also find it more scalable (in fact, actually scalable): if you needed to add support for, say, half-hour intervals, you would only need to start using INTERVALCOUNT = 48 with your data.
At the same time, the intent may be conveyed in a less clear way with the above code. Therefore, if 24 and 96 are the only possible values you can ever need for INTERVALCOUNT, you will probably not need any more flexibility than what you've got already for that part of your code.
You haven't mentioned what the purpose of the output is but I assume it's some sort of report?
I don't think there is a better way of doing this. What you have there is a classic use case for UNPIVOT and your solution seems appropriate to me, given that you can't change the underlying data structure.
Try this one -
DECLARE
#cols NVARCHAR(MAX)
, #SQL NVARCHAR(MAX)
SELECT #cols = STUFF((
SELECT ', [' + c.name + ']'
FROM sys.columns c
WHERE c.name LIKE 'INT%'
AND c.[object_id] = OBJECT_ID('dbo.TABLE')
FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 2, '')
SELECT #SQL = '
SELECT
[DATE]
, [TIME] = CASE
WHEN [INTERVALCOUNT] = 96
THEN CAST(DATEADD(MINUTE, CAST(SUBSTRING([INTERVAL],4,3) AS INT)*15, [DATE]) AS Time)
WHEN [INTERVALCOUNT] = 24
THEN CAST(DATEADD(HOUR, CAST(SUBSTRING([INTERVAL],4,3) AS INT), [DATE]) AS Time)
END
, [INTERVALCOUNT]
, [VALUE]
FROM (
SELECT [DATE], [INTERVALCOUNT], ' + #cols + '
FROM [TABLE]
) s
UNPIVOT ([VALUE] FOR [INTERVAL] IN (' + #cols + ') ) up
'
PRINT #SQL
EXEC sys.sp_executesql #SQL